forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlog.tsx
More file actions
74 lines (61 loc) · 2.04 KB
/
Blog.tsx
File metadata and controls
74 lines (61 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { getTranslations } from 'next-intl/server';
import type { FC } from 'react';
import { getClientContext } from '#site/client-context';
import BlogHeader from '#site/components/Blog/BlogHeader';
import WithBlogCategories from '#site/components/withBlogCategories';
import WithFooter from '#site/components/withFooter';
import WithNavBar from '#site/components/withNavBar';
import getBlogData from '#site/next-data/blogData';
import type { BlogCategory } from '#site/types';
import styles from './layouts.module.css';
const getBlogCategory = (pathname: string) => {
// pathname format can either be: /en/blog/{category}
// or /en/blog/{category}/page/{page}
// hence we attempt to interpolate the full /en/blog/{category}/page/{page}
// and in case of course no page argument is provided we define it to 1
// note that malformed routes can't happen as they are all statically generated
const [, , category = 'all', , page = 1] = pathname.split('/');
const { posts, pagination } = getBlogData(
category as BlogCategory,
Number(page)
);
return {
category: category,
posts: posts,
pagination: pagination,
page: Number(page),
};
};
const BlogLayout: FC = async () => {
const { pathname } = getClientContext();
const t = await getTranslations();
const mapCategoriesToTabs = (categories: Array<BlogCategory>) =>
categories.map(category => ({
key: category,
label: t(`layouts.blog.categories.${category}`),
link: `/blog/${category}`,
}));
const blogData = getBlogCategory(pathname);
return (
<>
<WithNavBar />
<div className={styles.blogLayout}>
<main>
<BlogHeader category={blogData.category} />
<WithBlogCategories
blogData={blogData}
categories={mapCategoriesToTabs([
'all',
'announcements',
'release',
'vulnerability',
'events',
])}
/>
</main>
</div>
<WithFooter />
</>
);
};
export default BlogLayout;