tailwind-nextjs-blog/pages/blog.tsx

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-10-17 23:37:01 +08:00
import { getAllFilesFrontMatter } from '@/lib/mdx';
import siteMetadata from '@/data/siteMetadata';
import ListLayout from '@/layouts/ListLayout';
import { PageSEO } from '@/components/SEO';
import { GetStaticProps, InferGetStaticPropsType } from 'next';
import { ComponentProps } from 'react';
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
export const POSTS_PER_PAGE = 5;
2022-07-17 21:40:41 +08:00
export const getStaticProps: GetStaticProps<{
2022-10-17 23:37:01 +08:00
posts: ComponentProps<typeof ListLayout>['posts'];
initialDisplayPosts: ComponentProps<typeof ListLayout>['initialDisplayPosts'];
pagination: ComponentProps<typeof ListLayout>['pagination'];
}> = async () => {
2022-10-17 23:37:01 +08:00
const posts = await getAllFilesFrontMatter('blog');
const initialDisplayPosts = posts.slice(0, POSTS_PER_PAGE);
2022-07-17 21:40:41 +08:00
const pagination = {
currentPage: 1,
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
2022-10-17 23:37:01 +08:00
};
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
return { props: { initialDisplayPosts, posts, pagination } };
};
2022-07-17 21:40:41 +08:00
export default function Blog({
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={`Blog - ${siteMetadata.author}`}
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
}