"use client"; import { useEffect, useRef, useState } from "react"; import { formatRelativeTime } from "../lib/format"; import { createSession, deleteSession, exportSessionToMarkdown, getAllSessions, type StoredSession, setActiveSessionId, } from "../lib/sessionStorage"; interface SessionManagerProps { activeSessionId: string | null; onSessionChange: (sessionId: string) => void; } function HistoryIcon() { return ( ); } function PlusIcon() { return ( ); } function DownloadIcon() { return ( ); } function TrashIcon() { return ( ); } export default function SessionManager({ activeSessionId, onSessionChange, }: SessionManagerProps) { const [isOpen, setIsOpen] = useState(false); const [sessions, setSessions] = useState([]); const [confirmDelete, setConfirmDelete] = useState(null); const dropdownRef = useRef(null); useEffect(() => { if (isOpen) setSessions(getAllSessions()); }, [isOpen]); useEffect(() => { function handleClickOutside(e: MouseEvent) { if ( dropdownRef.current && !dropdownRef.current.contains(e.target as Node) ) { setIsOpen(false); setConfirmDelete(null); } } if (isOpen) document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, [isOpen]); const handleNewSession = () => { const session = createSession(); setSessions(getAllSessions()); setIsOpen(false); onSessionChange(session.id); }; const handleSelectSession = (id: string) => { setActiveSessionId(id); setIsOpen(false); onSessionChange(id); }; const handleDelete = (id: string) => { deleteSession(id); const remaining = getAllSessions(); setSessions(remaining); setConfirmDelete(null); if (id === activeSessionId) { if (remaining.length > 0) { setActiveSessionId(remaining[0].id); onSessionChange(remaining[0].id); } else { const session = createSession(); setSessions(getAllSessions()); onSessionChange(session.id); } } }; const handleExport = (session: StoredSession) => { exportSessionToMarkdown(session); }; const activeTitle = sessions.find((s) => s.id === activeSessionId)?.title ?? "Sessions"; return (
{isOpen && (
Sessions
{sessions.length === 0 && (
No sessions yet
)} {sessions.map((session) => (
{confirmDelete === session.id ? (
) : (
)}
))}
)}
); }