diff --git a/pages/blog/index.page.tsx b/pages/blog/index.page.tsx index b46ce6a13..fca113153 100644 --- a/pages/blog/index.page.tsx +++ b/pages/blog/index.page.tsx @@ -102,6 +102,7 @@ export default function StaticMarkdownPage({ .split(',') .filter(isValidCategory); setCurrentFilterTags(tags.length ? tags : ['All']); + setCurrentPage(1); } }, [router.query]); @@ -190,6 +191,23 @@ export default function StaticMarkdownPage({ }); const allTags = ['All', ...Array.from(allTagsSet)]; + // pagination implement + const POSTS_PER_PAGE = 10; + const [currentPage, setCurrentPage] = useState(1); + + const totalPages = Math.ceil(sortedFilteredPosts.length / POSTS_PER_PAGE); + + useEffect(() => { + if (currentPage > totalPages) { + setCurrentPage(1); + } + }, [totalPages]); + + const currentPagePosts = sortedFilteredPosts.slice( + (currentPage - 1) * POSTS_PER_PAGE, + currentPage * POSTS_PER_PAGE, + ); + return ( // @ts-ignore @@ -299,8 +317,8 @@ export default function StaticMarkdownPage({ {/* Blog Posts Grid */} -
- {sortedFilteredPosts.map((blogPost: any, idx: number) => { +
+ {currentPagePosts.map((blogPost: any, idx: number) => { const { frontmatter, content } = blogPost; const date = new Date(frontmatter.date); const postTimeToRead = Math.ceil(readingTime(content).minutes); @@ -430,6 +448,34 @@ export default function StaticMarkdownPage({ ); })}
+ {/* pagination control */} +
+ + + Page {currentPage} of {totalPages} + + +
);