()
| 22 | } |
| 23 | |
| 24 | export default function HomePage() { |
| 25 | const { t } = useApp(); |
| 26 | const [articles, setArticles] = useState<Article[]>([]); |
| 27 | const [categories, setCategories] = useState<string[]>([]); |
| 28 | const [selectedCategory, setSelectedCategory] = useState<string>('全部'); |
| 29 | const [searchTerm, setSearchTerm] = useState(''); |
| 30 | const [loading, setLoading] = useState(true); |
| 31 | const [error, setError] = useState<string | null>(null); |
| 32 | const [currentPage, setCurrentPage] = useState(1); |
| 33 | const [hasMore, setHasMore] = useState(true); |
| 34 | |
| 35 | // 获取文章数据 |
| 36 | const fetchArticles = async (page: number = 1, append: boolean = false) => { |
| 37 | try { |
| 38 | const params = new URLSearchParams({ |
| 39 | page: page.toString(), |
| 40 | per_page: '12' |
| 41 | }); |
| 42 | |
| 43 | if (selectedCategory !== '全部') { |
| 44 | params.append('category', selectedCategory); |
| 45 | } |
| 46 | |
| 47 | if (searchTerm) { |
| 48 | params.append('search', searchTerm); |
| 49 | } |
| 50 | |
| 51 | const response = await fetch(`/api/articles?${params}`); |
| 52 | const data = await response.json(); |
| 53 | console.log('Articles API response:', data); |
| 54 | |
| 55 | // 检查是否有错误信息 |
| 56 | if (data.error) { |
| 57 | console.error('API returned error:', data.error); |
| 58 | setError(data.error); |
| 59 | setLoading(false); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // 确保数据格式正确 |
| 64 | const articles = Array.isArray(data.articles) ? data.articles : []; |
| 65 | const pagination = data.pagination || { hasNext: false }; |
| 66 | console.log('Processed articles:', articles.length, 'pagination:', pagination); |
| 67 | |
| 68 | if (append) { |
| 69 | setArticles(prev => [...prev, ...articles]); |
| 70 | } else { |
| 71 | setArticles(articles); |
| 72 | } |
| 73 | |
| 74 | setHasMore(pagination.hasNext); |
| 75 | setLoading(false); |
| 76 | } catch (err) { |
| 77 | console.error('Failed to fetch articles:', err); |
| 78 | setError('网络连接失败,请检查网络后重试'); |
| 79 | setLoading(false); |
| 80 | } |
| 81 | }; |
nothing calls this directly
no test coverage detected