-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathBlog.tsx
More file actions
78 lines (65 loc) · 2.06 KB
/
Blog.tsx
File metadata and controls
78 lines (65 loc) · 2.06 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
75
76
77
78
import { useTranslations } from 'next-intl';
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('/') as [
unknown,
unknown,
BlogCategory,
unknown,
number,
];
const { posts, pagination } = getBlogData(category, Number(page));
return {
category,
posts,
pagination,
page: Number(page),
};
};
const BlogLayout: FC = () => {
const t = useTranslations();
const { pathname } = getClientContext();
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',
'migrations',
'events',
])}
/>
</main>
</div>
<WithFooter />
</>
);
};
export default BlogLayout;