'use client'; import { useState, useCallback, useEffect } from 'react'; import { useTranslation } from '../lib/i18n/useTranslation'; import { languages } from '../lib/i18n/config'; // ===== 类型定义 ===== export interface NewsItem { id: string; title: string; title_zh_CN?: string; title_zh_TW?: string; summary: string; summary_zh_CN?: string; summary_zh_TW?: string; category: string; date: string; readTime: string; tags: string[]; tags_zh_CN?: string[]; tags_zh_TW?: string[]; image?: string; } interface NewsPageProps { initialLanguage?: string; newsList?: NewsItem[]; } // ===== 常量定义 ===== const newsCategories = ['all', 'company', 'product', 'industry', 'technology']; const categoryIcons = { company: '🏢', product: '📦', industry: '🏭', technology: '💻', all: '📰', }; const newsImages = [ 'https://images.unsplash.com/photo-1504711434969-e33886168f5c?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1586953208448-b95a79798f07?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1552664730-d307ca884978?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1460925895917-afdab827c52f?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1519389950473-47ba0277781c?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=300&fit=crop', ]; // ===== 工具函数 ===== const scrollToTop = () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }; // ===== 组件主体 ===== export default function NewsPage({ initialLanguage, newsList = [] }: NewsPageProps) { // ===== 国际化与状态 ===== const { language, t, changeLanguage, getLocalizedPath, isInitialized } = useTranslation(initialLanguage); const [selectedCategory, setSelectedCategory] = useState('all'); const [isMenuOpen, setIsMenuOpen] = useState(false); const [searchTerm, setSearchTerm] = useState(''); const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid'); const [sortBy, setSortBy] = useState<'date' | 'title' | 'category'>('date'); const [showScrollTop, setShowScrollTop] = useState(false); // ===== 事件处理 ===== const handleLanguageChange = useCallback( (newLanguage: string) => { changeLanguage(newLanguage); }, [changeLanguage], ); // ===== 本地化内容辅助函数 ===== const getLocalizedNewsData = (news: NewsItem) => { switch (language) { case 'zh-CN': return { title: news.title_zh_CN || news.title, summary: news.summary_zh_CN || news.summary, tags: news.tags_zh_CN || news.tags, }; case 'zh-TW': return { title: news.title_zh_TW || news.title, summary: news.summary_zh_TW || news.summary, tags: news.tags_zh_TW || news.tags, }; default: return { title: news.title, summary: news.summary, tags: news.tags, }; } }; // ===== 搜索和筛选逻辑 ===== const filteredNews = (newsList || []) .filter((news: NewsItem) => { const matchesCategory = selectedCategory === 'all' || news.category === selectedCategory; const matchesSearch = searchTerm === '' || (news.title && news.title.toLowerCase().includes(searchTerm.toLowerCase())) || (news.summary && news.summary.toLowerCase().includes(searchTerm.toLowerCase())) || (news.tags && news.tags.some((tag: string) => tag.toLowerCase().includes(searchTerm.toLowerCase()), )); return matchesCategory && matchesSearch; }) .sort((a: NewsItem, b: NewsItem) => { switch (sortBy) { case 'date': return new Date(b.date).getTime() - new Date(a.date).getTime(); case 'title': return (a.title || '').localeCompare(b.title || ''); case 'category': return (a.category || '').localeCompare(b.category || ''); default: return 0; } }); // ===== 滚动监听 ===== useEffect(() => { const handleScroll = () => { setShowScrollTop(window.scrollY > 300); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, []); // ===== 日期格式化 ===== const formatDate = (dateString: string) => { const date = new Date(dateString); if (language === 'en') { return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', }); } else { return date.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric', }); } }; // ===== 加载状态 ===== if (!isInitialized) { return (

Loading...

); } // ===== 导航项 ===== const navItems = [ { key: 'home', label: t.nav.home, href: getLocalizedPath('/') }, { key: 'products', label: t.nav.products, href: getLocalizedPath('/products') }, { key: 'news', label: t.nav.news, href: getLocalizedPath('/news') }, { key: 'support', label: t.nav.support, href: getLocalizedPath('/support') }, { key: 'about', label: t.nav.about, href: getLocalizedPath('/about') }, ]; // ===== 渲染部分 ===== return (
{/* Navigation */} {/* Mobile Navigation */} {isMenuOpen && (
{navItems.map((item) => ( {item.label} ))}
)} {/* Hero Section */}

{language === 'en' ? 'News & Insights' : language === 'zh-CN' ? '新闻资讯' : '新聞資訊'}

{language === 'en' ? 'Stay updated with the latest news, insights, and developments from TechCorp and the industry.' : language === 'zh-CN' ? '了解 TechCorp 和行业的最新新闻、见解和发展动态。' : '了解 TechCorp 和行業的最新新聞、見解和發展動態。'}

{/* 搜索栏 */}
setSearchTerm(e.target.value)} className="block w-full pl-10 pr-3 py-4 border border-gray-300 rounded-full leading-5 bg-white/80 backdrop-blur-sm placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-lg" data-oid="u:i8n-v" />
{/* 统计信息 */}
{newsList.length}
{language === 'en' ? 'Articles' : language === 'zh-CN' ? '文章' : '文章'}
4
{language === 'en' ? 'Categories' : language === 'zh-CN' ? '分类' : '分類'}
{language === 'en' ? 'Weekly' : language === 'zh-CN' ? '每周' : '每週'}
{language === 'en' ? 'Updates' : language === 'zh-CN' ? '更新' : '更新'}
2024
{language === 'en' ? 'Latest' : language === 'zh-CN' ? '最新' : '最新'}
{/* 筛选和排序控制 */}
{/* 分类筛选 */}
{newsCategories.map((category) => ( ))}
{/* 工具栏 */}
{/* 结果计数 */} {language === 'en' ? `${filteredNews.length} articles found` : language === 'zh-CN' ? `找到 ${filteredNews.length} 篇文章` : `找到 ${filteredNews.length} 篇文章`}
{/* 排序选择 */} {/* 视图切换 */}
{/* News Display */}
{filteredNews.length === 0 ? ( /* 无结果状态 */
📰

{language === 'en' ? 'No articles found' : language === 'zh-CN' ? '未找到文章' : '未找到文章'}

{language === 'en' ? 'Try adjusting your search terms or clearing the filters to see more results.' : language === 'zh-CN' ? '尝试调整您的搜索词或清除筛选条件以查看更多结果。' : '嘗試調整您的搜尋詞或清除篩選條件以查看更多結果。'}

) : (
{filteredNews.map((news, index) => { const localizedData = getLocalizedNewsData(news); console.log(news.id); if (viewMode === 'grid') { return ( /* 网格视图 */
{/* 新闻图片区域 */}
{localizedData.title} {/* 分类标签 */}
{ categoryIcons[ news.category as keyof typeof categoryIcons ] } {language === 'en' ? (news.category || '').charAt(0).toUpperCase() + (news.category || '').slice(1) : news.category === 'company' ? language === 'zh-CN' ? '公司' : '公司' : news.category === 'product' ? language === 'zh-CN' ? '产品' : '產品' : news.category === 'industry' ? language === 'zh-CN' ? '行业' : '行業' : news.category === 'technology' ? language === 'zh-CN' ? '技术' : '技術' : news.category}
{/* 阅读时间 */}
📖 {news.readTime}
{/* 悬停叠加层 */}
{/* 新闻内容区域 */}
); } else { return ( /* 列表视图 */
{/* 图片区域 */}
{localizedData.title} {/* 分类标签 */}
{ categoryIcons[ news.category as keyof typeof categoryIcons ] } {language === 'en' ? (news.category || '').charAt(0).toUpperCase() + (news.category || '').slice(1) : news.category === 'company' ? language === 'zh-CN' ? '公司' : '公司' : news.category === 'product' ? language === 'zh-CN' ? '产品' : '產品' : news.category === 'industry' ? language === 'zh-CN' ? '行业' : '行業' : news.category === 'technology' ? language === 'zh-CN' ? '技术' : '技術' : news.category}
{/* 阅读时间 */}
📖 {news.readTime}
{/* 内容区域 */}
); } })}
)}
{/* CTA Section */}

{language === 'en' ? 'Stay Updated' : language === 'zh-CN' ? '保持更新' : '保持更新'}

{language === 'en' ? 'Subscribe to our newsletter and never miss important updates and insights.' : language === 'zh-CN' ? '订阅我们的新闻通讯,不要错过重要的更新和见解。' : '訂閱我們的新聞通訊,不要錯過重要的更新和見解。'}

{/* 回到顶部按钮 */} {showScrollTop && ( )} {/* Footer */}
); }