tailwind-nextjs-blog/app/blog/page/[page]/page.tsx

37 lines
1021 B
TypeScript
Raw Normal View History

2023-08-16 23:55:57 +08:00
import ListLayout from '@/layouts/ListLayoutWithTags';
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer';
import { allBlogs } from 'contentlayer/generated';
2023-08-16 23:55:57 +08:00
const POSTS_PER_PAGE = 5;
export const generateStaticParams = async () => {
2023-08-16 23:55:57 +08:00
const totalPages = Math.ceil(allBlogs.length / POSTS_PER_PAGE);
const paths = Array.from({ length: totalPages }, (_, i) => ({
page: (i + 1).toString(),
}));
2023-08-16 23:55:57 +08:00
return paths;
};
export default function Page({ params }: { params: { page: string } }) {
2023-08-16 23:55:57 +08:00
const posts = allCoreContent(sortPosts(allBlogs));
const pageNumber = parseInt(params.page as string);
const initialDisplayPosts = posts.slice(
POSTS_PER_PAGE * (pageNumber - 1),
2023-08-16 23:55:57 +08:00
POSTS_PER_PAGE * pageNumber,
);
const pagination = {
currentPage: pageNumber,
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
2023-08-16 23:55:57 +08:00
};
return (
<ListLayout
posts={posts}
initialDisplayPosts={initialDisplayPosts}
pagination={pagination}
title="All Posts"
/>
2023-08-16 23:55:57 +08:00
);
}