import React, { useState, useEffect } from 'react'; import ReactMasonryCss from 'react-masonry-css'; interface DoubanItem { imageUrl: string; title: string; subtitle: string; link: string; intro: string; rating: number; date: string; } interface Pagination { current: number; total: number; hasNext: boolean; hasPrev: boolean; } interface DoubanCollectionProps { type: 'movie' | 'book'; } const DoubanCollection: React.FC = ({ type }) => { const [items, setItems] = useState([]); const [pagination, setPagination] = useState({ current: 1, total: 1, hasNext: false, hasPrev: false }); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [isPageChanging, setIsPageChanging] = useState(false); const fetchData = async (start = 0) => { setLoading(true); const params = new URLSearchParams(); params.append('type', type); params.append('start', start.toString()); const url = `/api/douban?${params.toString()}`; try { const response = await fetch(url); if (!response.ok) { throw new Error('获取数据失败'); } const data = await response.json(); setItems(data.items); setPagination(data.pagination); } catch (err) { setError(err instanceof Error ? err.message : '未知错误'); } finally { setLoading(false); } }; useEffect(() => { fetchData(); }, [type]); const handlePageChange = (page: number) => { const start = (page - 1) * 15; // 手动更新分页状态,不等待API响应 setPagination(prev => ({ ...prev, current: page })); // 重置当前状态,显示加载中 setItems([]); setLoading(true); fetchData(start); }; const renderStars = (rating: number) => { return (
{[1, 2, 3, 4, 5].map((star) => ( ))}
); }; const breakpointColumnsObj = { default: 3, 1100: 2, 700: 1 }; if (loading && items.length === 0) { return
加载中...
; } if (error) { return
错误: {error}
; } return (

{type === 'movie' ? '观影记录' : '读书记录'}

{items.map((item, index) => ( ))} {/* 分页 */} {pagination.total > 1 && (
{pagination.current} / {pagination.total}
)}
); }; export default DoubanCollection;