-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathwithBlogCrossLinks.tsx
More file actions
51 lines (42 loc) · 1.25 KB
/
withBlogCrossLinks.tsx
File metadata and controls
51 lines (42 loc) · 1.25 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
import type { FC } from 'react';
import { getClientContext } from '#site/client-context';
import CrossLink from '#site/components/Common/CrossLink';
import getBlogData from '#site/next-data/blogData';
import type { BlogCategory } from '#site/types';
const WithBlogCrossLinks: FC = () => {
const { pathname } = getClientContext();
// Extracts from the static URL the components used for the Blog Post slug
const [, , category, postname] = pathname.split('/') as [
unknown,
unknown,
BlogCategory,
string,
];
const { posts } = getBlogData(category);
const currentItem = posts.findIndex(
({ slug }) => slug === `/blog/${category}/${postname}`
);
const [previousCrossLink, nextCrossLink] = [
posts[currentItem - 1],
posts[currentItem + 1],
];
return (
<div className="max-xs:grid-cols-1 mt-4 grid w-full grid-cols-2 gap-4">
{(previousCrossLink && (
<CrossLink
type="previous"
text={previousCrossLink.title}
link={previousCrossLink.slug}
/>
)) || <div />}
{nextCrossLink && (
<CrossLink
type="next"
text={nextCrossLink.title}
link={nextCrossLink.slug}
/>
)}
</div>
);
};
export default WithBlogCrossLinks;