1- import { useState } from 'react' ;
21import rawItemData from '../assets/items.json'
32import rawBlogData from '../assets/blogs.json'
43import type { Blog } from '../App' ;
54import { Link } from 'react-router' ;
65import { resolveAvatar } from '../services/avatarService'
76
8- import { Card , Button } from '@radix-ui/themes' ;
7+ import { Card } from '@radix-ui/themes' ;
8+ import { usePagination } from '../hooks/usePagination' ;
9+ import { Pagination } from './Pagination' ;
910
1011interface Item {
1112 blog_id : string ;
@@ -26,35 +27,15 @@ const blogs: Blog[] = rawBlogData as Blog[];
2627const blogMap : Record < string , Blog > = { } ;
2728
2829function News ( ) {
29- const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
30- const itemsPerPage = 20 ;
30+ blogs . forEach ( blog => { blogMap [ blog . name ] = blog ; } ) ;
3131
32- blogs . forEach ( blog => {
33- blogMap [ blog . name ] = blog ;
34- } ) ;
35-
36- // 计算总页数
37- const totalPages = Math . ceil ( items . length / itemsPerPage ) ;
38-
39- // 计算当前页的数据范围
40- const startIndex = ( currentPage - 1 ) * itemsPerPage ;
41- const endIndex = startIndex + itemsPerPage ;
42- const currentItems = items . slice ( startIndex , endIndex ) ;
43-
44- const handlePageChange = ( page : number ) => {
45- setCurrentPage ( page ) ;
46- // 滚动到页面顶部
47- window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
48- } ;
32+ const { currentItems, currentPage, totalPages, startIndex, endIndex, totalItems, setPage } = usePagination ( items , 20 ) ;
4933
5034 return (
5135 < div className = 'container mx-auto flex flex-col gap-4' >
52- { /* 页面信息 */ }
5336 < div className = 'text-center text-sm text-gray-500 mb-4' >
54- 显示第 { startIndex + 1 } - { Math . min ( endIndex , items . length ) } 项,共 { items . length } 项
37+ 显示第 { startIndex + 1 } - { endIndex } 项,共 { totalItems } 项
5538 </ div >
56-
57- { /* 博客列表 */ }
5839 { currentItems . map ( item => (
5940 < Card key = { item . item_url } >
6041 < div className = 'flex flex-row gap-2' >
@@ -72,57 +53,7 @@ function News() {
7253 </ div >
7354 </ Card >
7455 ) ) }
75-
76- { /* 分页导航 */ }
77- < div className = "flex justify-center items-center gap-2 mt-8 mb-4" >
78- { /* 上一页按钮,所有屏幕都显示 */ }
79- < Button
80- variant = "outline"
81- disabled = { currentPage === 1 }
82- onClick = { ( ) => handlePageChange ( currentPage - 1 ) }
83- >
84- 上一页
85- </ Button >
86- { /* 只在大屏显示页码按钮 */ }
87- < div className = "hidden sm:flex items-center gap-2" >
88- { /* 显示前几页 */ }
89- { currentPage > 3 && (
90- < >
91- < Button variant = "outline" onClick = { ( ) => handlePageChange ( 1 ) } > 1</ Button >
92- { currentPage > 4 && < span className = 'text-gray-500' > ...</ span > }
93- </ >
94- ) }
95- { /* 显示当前页周围的页码 */ }
96- { Array . from ( { length : Math . min ( 5 , totalPages ) } , ( _ , i ) => {
97- const page = Math . max ( 1 , Math . min ( totalPages - 4 , currentPage - 2 ) ) + i ;
98- if ( page > totalPages ) return null ;
99- return (
100- < Button
101- key = { page }
102- variant = { page === currentPage ? "solid" : "outline" }
103- onClick = { ( ) => handlePageChange ( page ) }
104- >
105- { page }
106- </ Button >
107- ) ;
108- } ) }
109- { /* 显示后几页 */ }
110- { currentPage < totalPages - 2 && (
111- < >
112- { currentPage < totalPages - 3 && < span className = 'text-gray-500' > ...</ span > }
113- < Button variant = "outline" onClick = { ( ) => handlePageChange ( totalPages ) } > { totalPages } </ Button >
114- </ >
115- ) }
116- </ div >
117- { /* 下一页按钮,所有屏幕都显示 */ }
118- < Button
119- variant = "outline"
120- disabled = { currentPage === totalPages }
121- onClick = { ( ) => handlePageChange ( currentPage + 1 ) }
122- >
123- 下一页
124- </ Button >
125- </ div >
56+ < Pagination currentPage = { currentPage } totalPages = { totalPages } onChange = { setPage } />
12657 </ div >
12758 ) ;
12859}
0 commit comments