import { useState, useEffect, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { getAgentHistory, mapHistoryMessagesToItems, type HistoryItem } from '../lib/api'; import Icon from './Icon'; interface Props { locale: string; dashboard: { brandName: string; navHome: string; navStore: string; navDivination: string; navManual: string; navAuto: string; navHistory: string; navLanguage: string; navSettings: string; logout: string }; history: { title: string; statTotal: string; statLatest: string; filters: string; filterAll: string; filterCareer: string; filterLove: string; filterWealth: string; noResults: string }; } const ALL_CATEGORIES = ['career', 'love', 'wealth', 'fortune', 'dream', 'health', 'study', 'search', 'other'] as const; type CategoryKey = typeof ALL_CATEGORIES[number]; const CATEGORY_COLORS: Record = { 'career': 'bg-blue-50 text-blue-500', 'love': 'bg-pink-50 text-pink-500', 'wealth': 'bg-amber-50 text-amber-600', 'fortune': 'bg-purple-50 text-purple-500', 'dream': 'bg-indigo-50 text-indigo-500', 'health': 'bg-red-50 text-red-500', 'study': 'bg-green-50 text-green-600', 'search': 'bg-cyan-50 text-cyan-600', 'other': 'bg-slate-100 text-slate-500', }; const CATEGORY_LABELS_ZH: Record = { 'career': '事业', 'love': '感情', 'wealth': '财富', 'fortune': '运势', 'dream': '解梦', 'health': '健康', 'study': '学业', 'search': '寻物', 'other': '其他', }; const CATEGORY_LABELS_EN: Record = { 'career': 'Career', 'love': 'Love', 'wealth': 'Wealth', 'fortune': 'Fortune', 'dream': 'Dream', 'health': 'Health', 'study': 'Study', 'search': 'Search', 'other': 'Other', }; const RATING_COLORS: Record = { '上上签': 'bg-amber-50 text-amber-500', '上签': 'bg-amber-50 text-amber-500', '中上签': 'bg-violet-50 text-violet-600', '中签': 'bg-slate-100 text-slate-500', '下签': 'bg-red-50 text-red-500', }; export default function HistoryListPage({ locale, history: i18n }: Props) { const navigate = useNavigate(); const [allItems, setAllItems] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const [selectedId, setSelectedId] = useState(null); const [activeFilter, setActiveFilter] = useState('all'); const [searchQuery, setSearchQuery] = useState(''); // 获取历史数据 useEffect(() => { let alive = true; setLoading(true); setError(''); getAgentHistory() .then((data) => { if (!alive) return; setAllItems(mapHistoryMessagesToItems(data.messages)); }) .catch((err) => { if (!alive) return; setError(err instanceof Error ? err.message : 'Failed to load history'); }) .finally(() => { if (alive) setLoading(false); }); return () => { alive = false; }; }, []); const stats = useMemo(() => { const total = allItems.length; const latest = allItems.length > 0 ? allItems[0].created_at : null; return { total, latest }; }, [allItems]); // 过滤后的列表 const filteredItems = useMemo(() => { let items = allItems; // 分类筛选 if (activeFilter !== 'all') { items = items.filter((item) => item.category === activeFilter); } // 搜索筛选 if (searchQuery.trim()) { const query = searchQuery.toLowerCase(); items = items.filter( (item) => item.question.toLowerCase().includes(query) || item.hexagram_name.toLowerCase().includes(query) ); } return items; }, [allItems, activeFilter, searchQuery]); // 各分类数量 const filterCounts = useMemo(() => { const counts: Record = { all: allItems.length }; for (const cat of ALL_CATEGORIES) { counts[cat] = allItems.filter((item) => item.category === cat).length; } return counts; }, [allItems]); // 格式化最近时间 const formatLatest = (dateStr: string | null) => { if (!dateStr) return '--'; const date = new Date(dateStr); const now = new Date(); const isToday = date.toDateString() === now.toDateString(); const timeStr = date.toLocaleTimeString(locale === 'en' ? 'en-US' : 'zh-CN', { hour: '2-digit', minute: '2-digit', }); if (isToday) { return locale === 'en' ? `Today ${timeStr}` : `今天 ${timeStr}`; } return date.toLocaleDateString(locale === 'en' ? 'en-US' : 'zh-CN', { month: 'short', day: 'numeric', }); }; // 点击卡片跳转 const handleItemClick = (item: HistoryItem) => { setSelectedId(item.id); navigate(`/${locale}/history/${item.threadId}`); }; // 返回首页 const handleBackHome = () => { navigate(`/${locale}/dashboard`); }; return (
{/* Header */}

{i18n.title}

{/* Search */}
setSearchQuery(e.target.value)} className="w-60 pl-9 pr-4 py-2 rounded-full bg-white border border-slate-200 text-sm focus:outline-none focus:border-violet-400 transition-colors" />
{/* Error */} {error && (
{error}
)} {/* Stats */}

{i18n.statTotal}

{stats.total}

{i18n.statLatest}

{formatLatest(stats.latest)}

{/* Main Content: List + Filters */}
{/* List */}

{i18n.title}

{filteredItems.length} {locale === 'en' ? 'items' : '条'}
{loading ? (
{locale === 'en' ? 'Loading...' : '加载中...'}
) : filteredItems.length === 0 ? (
{i18n.noResults}
) : (
{filteredItems.map((item) => ( ))}
)}
{/* Side: Quick Filters */}

{i18n.filters}

{ALL_CATEGORIES.map((cat) => ( ))}
); }