"use client"; import Link from "next/link"; import { useEffect, useState } from "react"; import type { NewsItem } from "@/data/news"; import { getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n"; const ITEMS_PER_PAGE = 12; export function NewsList({ catalog }: { catalog: Record }) { const [activeCategory, setActiveCategory] = useState(null); const [locale, setLocale] = useState("zh-CN"); const [currentPage, setCurrentPage] = useState(1); const messages = getLocaleMessages(locale); const news = catalog[locale]; useEffect(() => { const syncLocale = () => setLocale(getInitialLocale()); syncLocale(); window.addEventListener("kr-locale-change", syncLocale); window.addEventListener("storage", syncLocale); return () => { window.removeEventListener("kr-locale-change", syncLocale); window.removeEventListener("storage", syncLocale); }; }, []); useEffect(() => { setCurrentPage(1); }, [activeCategory]); const categories = Array.from(new Set(news.map((item) => item.category).filter(Boolean))); const visibleNews = activeCategory ? news.filter((item) => item.category === activeCategory) : news; const totalPages = Math.ceil(visibleNews.length / ITEMS_PER_PAGE); const startIndex = (currentPage - 1) * ITEMS_PER_PAGE; const endIndex = startIndex + ITEMS_PER_PAGE; const paginatedNews = visibleNews.slice(startIndex, endIndex); const handlePrevious = () => { if (currentPage > 1) { setCurrentPage(currentPage - 1); window.scrollTo({ top: 0, behavior: "smooth" }); } }; const handleNext = () => { if (currentPage < totalPages) { setCurrentPage(currentPage + 1); window.scrollTo({ top: 0, behavior: "smooth" }); } }; const handlePageClick = (page: number) => { setCurrentPage(page); window.scrollTo({ top: 0, behavior: "smooth" }); }; return ( <>
{categories.map((category) => ( ))}
{paginatedNews.map((item) => (
{item.category}

{item.title}

))}
{totalPages > 1 && (
{Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => ( ))}
)} ); }