2022-10-17 23:37:01 +08:00
|
|
|
import { PageSEO } from '@/components/SEO';
|
|
|
|
import siteMetadata from '@/data/siteMetadata';
|
|
|
|
import { getAllFilesFrontMatter } from '@/lib/mdx';
|
|
|
|
import ListLayout from '@/layouts/ListLayout';
|
|
|
|
import { POSTS_PER_PAGE } from '../../blog';
|
|
|
|
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next';
|
|
|
|
import { PostFrontMatter } from 'types/PostFrontMatter';
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
export const getStaticPaths: GetStaticPaths<{ page: string }> = async () => {
|
2022-10-17 23:37:01 +08:00
|
|
|
const totalPosts = await getAllFilesFrontMatter('blog');
|
|
|
|
const totalPages = Math.ceil(totalPosts.length / POSTS_PER_PAGE);
|
2022-07-17 21:40:41 +08:00
|
|
|
const paths = Array.from({ length: totalPages }, (_, i) => ({
|
|
|
|
params: { page: (i + 1).toString() },
|
2022-10-17 23:37:01 +08:00
|
|
|
}));
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
paths,
|
|
|
|
fallback: false,
|
2022-10-17 23:37:01 +08:00
|
|
|
};
|
|
|
|
};
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
export const getStaticProps: GetStaticProps<{
|
2022-10-17 23:37:01 +08:00
|
|
|
posts: PostFrontMatter[];
|
|
|
|
initialDisplayPosts: PostFrontMatter[];
|
|
|
|
pagination: { currentPage: number; totalPages: number };
|
2022-10-07 13:55:39 +08:00
|
|
|
}> = async (context) => {
|
2022-07-17 21:40:41 +08:00
|
|
|
const {
|
|
|
|
params: { page },
|
2022-10-17 23:37:01 +08:00
|
|
|
} = context;
|
|
|
|
const posts = await getAllFilesFrontMatter('blog');
|
|
|
|
const pageNumber = parseInt(page as string);
|
2022-07-17 21:40:41 +08:00
|
|
|
const initialDisplayPosts = posts.slice(
|
|
|
|
POSTS_PER_PAGE * (pageNumber - 1),
|
|
|
|
POSTS_PER_PAGE * pageNumber
|
2022-10-17 23:37:01 +08:00
|
|
|
);
|
2022-07-17 21:40:41 +08:00
|
|
|
const pagination = {
|
|
|
|
currentPage: pageNumber,
|
|
|
|
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
|
2022-10-17 23:37:01 +08:00
|
|
|
};
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
posts,
|
|
|
|
initialDisplayPosts,
|
|
|
|
pagination,
|
|
|
|
},
|
2022-10-17 23:37:01 +08:00
|
|
|
};
|
|
|
|
};
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
export default function PostPage({
|
|
|
|
posts,
|
|
|
|
initialDisplayPosts,
|
|
|
|
pagination,
|
|
|
|
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
2022-07-17 21:40:41 +08:00
|
|
|
return (
|
|
|
|
<>
|
2022-10-17 23:37:01 +08:00
|
|
|
<PageSEO
|
|
|
|
title={siteMetadata.title}
|
|
|
|
description={siteMetadata.description}
|
|
|
|
/>
|
2022-07-17 21:40:41 +08:00
|
|
|
<ListLayout
|
|
|
|
posts={posts}
|
|
|
|
initialDisplayPosts={initialDisplayPosts}
|
|
|
|
pagination={pagination}
|
|
|
|
title="All Posts"
|
|
|
|
/>
|
|
|
|
</>
|
2022-10-17 23:37:01 +08:00
|
|
|
);
|
2022-07-17 21:40:41 +08:00
|
|
|
}
|