import { useTranslation } from 'react-i18next'; import { useQrState } from '../store/qrContext'; import { type HistoryEntry } from '../types'; import { persistHistory } from '../hooks/useQrEncode'; const MODE_I18N: Record = { text: 'mode.text', url: 'mode.url', wifi: 'mode.wifi', vcard: 'mode.vcard', email: 'mode.email', phone: 'mode.phone', sms: 'mode.sms', }; export default function HistoryList() { const { t } = useTranslation(); 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 (
📋 {t('history.title')} {state.history.length > 0 && ( )}
{state.history.length === 0 && (

{t('history.empty')}

)} {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" >
{t(MODE_I18N[entry.mode] || entry.mode)} {formatTime(entry.timestamp)}
{entry.content.length > 20 ? entry.content.slice(0, 20) + '...' : entry.content}
))}
); }