Skip to content

Commit

Permalink
refactor: clean up code formatting and improve consistency across uti…
Browse files Browse the repository at this point in the history
…lity functions
  • Loading branch information
JeelRajodiya committed Dec 30, 2024
1 parent 4215898 commit 74a6afa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
10 changes: 5 additions & 5 deletions utils/getStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import i18nextConfig from '../next-i18next.config.cjs';
export const getI18nPaths = () =>
i18nextConfig.i18n.locales.map((lng) => ({
params: {
lang: lng,
},
lang: lng
}
}));

/**
Expand All @@ -19,7 +19,7 @@ export const getI18nPaths = () =>
*/
export const getStaticPaths = () => ({
fallback: false,
paths: getI18nPaths(),
paths: getI18nPaths()
});

/**
Expand All @@ -31,7 +31,7 @@ export const getStaticPaths = () => ({
export async function getI18nProps(ctx: any, ns = ['common']) {
const locale = ctx?.params?.lang ? ctx.params.lang : 'en';
const props = {
...(await serverSideTranslations(locale, ns)),
...(await serverSideTranslations(locale, ns))
};

return props;
Expand All @@ -45,7 +45,7 @@ export async function getI18nProps(ctx: any, ns = ['common']) {
export function makeStaticProps(ns = {}) {
return async function getStaticProps(ctx: any) {
return {
props: await getI18nProps(ctx, ns as any),
props: await getI18nProps(ctx, ns as any)
};
};
}
21 changes: 13 additions & 8 deletions utils/getUniqueCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import Expenses from '../config/finance/json-data/Expenses.json';
* @returns {string[]} An array of unique expense categories.
*/
export const getUniqueCategories = (): string[] => {
const allCategories: string[] = [];
for (const month in Expenses) {
Expenses[month as keyof typeof Expenses].forEach((entry: { Category: string }) => {
if (!allCategories.includes(entry.Category)) {
allCategories.push(entry.Category);
}
});
const allCategories: string[] = [];

// eslint-disable-next-line no-restricted-syntax
for (const month in Expenses) {
if (Object.prototype.hasOwnProperty.call(Expenses, month)) {
Expenses[month as keyof typeof Expenses].forEach((entry: { Category: string }) => {
if (!allCategories.includes(entry.Category)) {
allCategories.push(entry.Category);
}
});
}
return allCategories;
}

return allCategories;
};
2 changes: 1 addition & 1 deletion utils/languageDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import i18nextConfig from '../next-i18next.config.cjs';

export default languageDetector({
supportedLngs: i18nextConfig.i18n.locales,
fallbackLng: i18nextConfig.i18n.defaultLocale,
fallbackLng: i18nextConfig.i18n.defaultLocale
});
37 changes: 19 additions & 18 deletions utils/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ import languageDetector from './languageDetector';
* @returns null
*/
export function useRedirect(to: string | undefined): any {
const router = useRouter();
const router = useRouter();

const toUrl = to || router.asPath;
const toUrl = to || router.asPath;

// language detection
useEffect(() => {
const detectedLng = languageDetector.detect();
// language detection
useEffect(() => {
const detectedLng = languageDetector.detect();

if (toUrl.startsWith(`/${detectedLng}`) && router.route === '/404') { // prevent endless loop
router.replace(`/${detectedLng}${router.route}`);
if (toUrl.startsWith(`/${detectedLng}`) && router.route === '/404') {
// prevent endless loop
router.replace(`/${detectedLng}${router.route}`);

return;
}
return;
}

languageDetector.cache!(detectedLng!);
router.replace(`/${detectedLng}${toUrl}`);
});
languageDetector.cache!(detectedLng!);
router.replace(`/${detectedLng}${toUrl}`);
});

return null;
};
return null;
}

/**
* Component that redirects the user to the current URL with a language prefix.
* @returns null
*/
export const Redirect = () => {
useRedirect(undefined);
useRedirect(undefined);

return null;
return null;
};

/**
Expand All @@ -46,7 +47,7 @@ export const Redirect = () => {
* @returns A component that redirects the user to the specified URL.
*/
export const getRedirect = (to: string) => () => {
useRedirect(to);
useRedirect(to);

return null;
return null;
};
4 changes: 2 additions & 2 deletions utils/staticHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export const generateCaseStudyContent = (data: any) => {
]
},
{
title: "Production-use AsyncAPI document",
content: fullExample,
title: 'Production-use AsyncAPI document',
content: fullExample
}
];
};

0 comments on commit 74a6afa

Please sign in to comment.