import { useEffect, useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faClock, faHistory, faTrashAlt, faTimes, faCircleNotch } from "@fortawesome/free-solid-svg-icons"; import { clearHistory, formatRelative, HISTORY_UPDATED_EVENT, loadHistory, removeFromHistory, type HistoryEntry, } from "../lib/history"; import type { InputValues } from "../lib/types"; import { useI18n } from "../lib/i18n"; interface HistoryDrawerProps { open: boolean; onClose: () => void; onLoad: (entry: HistoryEntry) => void; } export function HistoryDrawer({ open, onClose, onLoad }: HistoryDrawerProps) { const [entries, setEntries] = useState([]); const [loading, setLoading] = useState(false); const { t } = useI18n(); const fetchHistory = async () => { setLoading(true); try { const data = await loadHistory(); setEntries(data); } finally { setLoading(false); } }; useEffect(() => { if (open) fetchHistory(); }, [open]); useEffect(() => { window.addEventListener(HISTORY_UPDATED_EVENT, fetchHistory); return () => window.removeEventListener(HISTORY_UPDATED_EVENT, fetchHistory); }, []); // Close on Escape useEffect(() => { if (!open) return; const handler = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; window.addEventListener("keydown", handler); return () => window.removeEventListener("keydown", handler); }, [open, onClose]); return ( <> {/* Backdrop */} {open && (
)} {/* Drawer */}
{/* Header */}
{t.history} {!loading && entries.length > 0 && ( {entries.length} )}
{loading && }
{/* Entries */}
{entries.length === 0 && !loading ? (

{t.noHistory}

) : ( entries.map((e) => ( { onLoad(e); onClose(); }} onRemove={async () => { await removeFromHistory(e.id); }} /> )) )}
{/* Footer */} {entries.length > 0 && (
)}
); } // Keep old named export for compatibility but point to the drawer export { HistoryDrawer as HistoryPanel }; function HistoryRow({ entry, onLoad, onRemove, }: { entry: HistoryEntry; onLoad: () => void; onRemove: () => void; }) { const summary = summarizeInput(entry.input); return (
{summary}
{formatRelative(entry.timestamp)} {entry.input.mood && ( <> · {entry.input.mood} )}
); } function summarizeInput(v: InputValues): string { const idea = v.idea.trim(); const max = 55; if (idea.length <= max) return idea || "(no idea text)"; return idea.slice(0, max - 1) + "…"; }