@@ -9,15 +9,43 @@ import { filterBlogsBySearchTerm } from "../../utils/blogFilters";
99
1010import "./blogs-new.css" ;
1111
12+ const POSTS_PER_PAGE = 12 ;
13+
1214export default function Blogs ( ) {
1315 const { siteConfig } = useDocusaurusContext ( ) ;
1416 const [ searchInput , setSearchInput ] = React . useState ( "" ) ;
1517 const [ searchTerm , setSearchTerm ] = React . useState ( "" ) ;
18+ const [ currentPage , setCurrentPage ] = React . useState ( 1 ) ;
1619 const filteredBlogs = React . useMemo (
1720 ( ) => filterBlogsBySearchTerm ( blogs , searchTerm ) ,
1821 [ searchTerm ] ,
1922 ) ;
2023
24+ React . useEffect ( ( ) => {
25+ setCurrentPage ( 1 ) ;
26+ } , [ searchTerm ] ) ;
27+
28+ const totalPages = Math . max ( 1 , Math . ceil ( filteredBlogs . length / POSTS_PER_PAGE ) ) ;
29+ const showingStart = filteredBlogs . length === 0 ? 0 : ( currentPage - 1 ) * POSTS_PER_PAGE + 1 ;
30+ const showingEnd = Math . min ( currentPage * POSTS_PER_PAGE , filteredBlogs . length ) ;
31+ const paginatedBlogs = filteredBlogs . slice (
32+ ( currentPage - 1 ) * POSTS_PER_PAGE ,
33+ currentPage * POSTS_PER_PAGE ,
34+ ) ;
35+ const visiblePages = ( ( ) => {
36+ const pages : number [ ] = [ ] ;
37+ const maxVisible = 5 ;
38+ let start = Math . max ( 1 , currentPage - Math . floor ( maxVisible / 2 ) ) ;
39+ let end = start + maxVisible - 1 ;
40+ if ( end > totalPages ) {
41+ end = totalPages ;
42+ start = Math . max ( 1 , end - maxVisible + 1 ) ;
43+ }
44+ for ( let i = start ; i <= end ; i ++ ) pages . push ( i ) ;
45+ return pages ;
46+ } ) ( ) ;
47+ const showLastPage = visiblePages [ visiblePages . length - 1 ] < totalPages ;
48+
2149 const handleSearchChange = ( e : { target : { value : string } } ) => {
2250 setSearchInput ( e . target . value ) ;
2351 } ;
@@ -140,8 +168,8 @@ export default function Blogs() {
140168 ) }
141169
142170 < div className = "articles-grid" >
143- { filteredBlogs . length > 0 ? (
144- filteredBlogs . map ( ( blog ) => (
171+ { paginatedBlogs . length > 0 ? (
172+ paginatedBlogs . map ( ( blog ) => (
145173 < React . Fragment key = { blog . id ?? blog . slug } >
146174 < BlogCard blog = { blog } />
147175 </ React . Fragment >
0 commit comments