import { ArrowDownToLine, ArrowLeft, CheckCircle, History, RotateCcw, Trash2, XCircle, } from 'lucide-react' import { useCallback, useEffect, useState } from 'react' import { Button } from '@/components/ui/button' import { type SessionRecord, clearSessions, deleteSession, listSessions } from '@/lib/db' import { downloadHistoryExport } from '@/lib/history-export' function timeAgo(ts: number): string { const seconds = Math.floor((Date.now() - ts) / 1000) if (seconds < 60) return 'just now' const minutes = Math.floor(seconds / 60) if (minutes < 60) return `${minutes}m ago` const hours = Math.floor(minutes / 60) if (hours < 24) return `${hours}h ago` const days = Math.floor(hours / 24) return `${days}d ago` } export function HistoryList({ onSelect, onBack, onRerun, }: { onSelect: (id: string) => void onBack: () => void onRerun: (task: string) => void }) { const [sessions, setSessions] = useState([]) const [loading, setLoading] = useState(true) const load = useCallback(async () => { try { setSessions(await listSessions()) } catch (err) { console.error('[HistoryList] Failed to load sessions:', err) } finally { setLoading(false) } }, []) useEffect(() => { load() }, [load]) const handleDelete = async (e: React.MouseEvent, id: string) => { e.stopPropagation() await deleteSession(id) setSessions((prev) => prev.filter((s) => s.id !== id)) } const handleExport = (e: React.MouseEvent, session: SessionRecord) => { e.stopPropagation() downloadHistoryExport(session.task, session.createdAt, session.history) } const handleRerun = (e: React.MouseEvent, task: string) => { e.stopPropagation() onRerun(task) } return (
{/* Header */}
History {sessions.length > 0 && ( )}
{/* List */}
{loading && (
{[...Array(4)].map((_, i) => (
))}
)} {!loading && sessions.length === 0 && (

No history yet

)} {sessions.map((session) => (
onSelect(session.id)} className="w-full text-left px-3 py-2.5 border-b hover:bg-muted/50 transition-colors cursor-pointer flex items-start gap-2 group" > {/* Status icon */} {session.status === 'completed' ? ( ) : ( )} {/* Content */}

{session.task}

{timeAgo(session.createdAt)} ยท {session.history.length} steps

))}
) }