Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: some code review changes
  • Loading branch information
ovflowd committed Jun 27, 2023
commit 3c21f7f5b66f09a37ad7117ba9df0a888342e87f
20 changes: 16 additions & 4 deletions hooks/useBlogData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ export const useBlogData = () => {
[pagination]
);

const currentCategory = useMemo(
() => asPath.split('/')[3] || new Date().getFullYear().toString(),
[asPath]
);
const currentCategory = useMemo(() => {
// We split the pathname to retrieve the blog category from it
// since the URL is usually /{languageCode}/blog/{category}
// the third path piece is usually the category name
const [, , pathname, category] = asPath.split('/');

if (pathname === 'blog' && category && category.length) {
return category;
}

// if either the pathname does not match to a blog page
// which should not happen (as this hook should only be used in blog pages)
// or if there is no category in the URL we return the current year as category name
// which is always the default category (for example, the blog index)
return new Date().getFullYear().toString();
}, [asPath]);

return {
posts,
Expand Down
2 changes: 1 addition & 1 deletion next.locales.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const getCurrentTranslations = locale => ({
});

export {
defaultLocale,
availableLocales,
defaultLocale,
getCurrentLocale,
getCurrentTranslations,
};