import { useQrState } from '../store/qrContext'; import { MODE_LABELS, type HistoryEntry } from '../types'; import { persistHistory } from '../hooks/useQrEncode'; export default function HistoryList() { const { state, dispatch } = useQrState(); const handleClick = (entry: HistoryEntry) => { dispatch({ type: 'SET_MODE', payload: entry.mode }); // 优先使用存储的 formData 恢复表单,否则回退到纯文本 if (entry.formData) { dispatch({ type: 'SET_FORM_DATA', payload: entry.formData }); } else { dispatch({ type: 'SET_FORM_DATA', payload: { text: entry.content } }); } }; const handleDelete = (e: React.MouseEvent, id: string) => { e.stopPropagation(); const updated = state.history.filter((h) => h.id !== id); dispatch({ type: 'SET_HISTORY', payload: updated }); persistHistory(updated); }; const handleClear = () => { dispatch({ type: 'SET_HISTORY', payload: [] }); persistHistory([]); }; const formatTime = (ts: number) => { const d = new Date(ts); return `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`; }; return (
📋 历史记录 {state.history.length > 0 && ( )}
{state.history.length === 0 && (

暂无记录

)} {state.history.map((entry) => (
handleClick(entry)} className="group flex items-center justify-between px-2 py-1.5 rounded-lg text-xs cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-800 transition-all" >
{MODE_LABELS[entry.mode] || entry.mode} {formatTime(entry.timestamp)}
{entry.content.length > 20 ? entry.content.slice(0, 20) + '...' : entry.content}
))}
); }