import { useEffect, useRef, useState } from "react"; import type { Session, Team } from "../types"; const SNAPSHOTS_KEY = "hermes-snapshots"; const SNAPSHOT_PREFIX = "hermes-snapshot-"; const WORKBENCH_KEY_V2 = "hermes-workbench-v2"; interface SessionSummary { id: string; title: string; workspacePath?: string | null; } interface SnapshotMeta { name: string; savedAt: string; note?: string; sessions?: SessionSummary[]; } function loadIndex(): SnapshotMeta[] { try { const raw = localStorage.getItem(SNAPSHOTS_KEY); const arr = raw ? JSON.parse(raw) : []; return Array.isArray(arr) ? arr : []; } catch { return []; } } function saveIndex(index: SnapshotMeta[]) { try { localStorage.setItem(SNAPSHOTS_KEY, JSON.stringify(index)); } catch {} } interface Props { teams: Team[]; sessions: Session[]; onLoadSnapshot: () => void; } export function SnapshotMenu({ teams, sessions, onLoadSnapshot }: Props) { const [open, setOpen] = useState(false); const [snapshots, setSnapshots] = useState([]); const [nameInput, setNameInput] = useState(""); const [noteInput, setNoteInput] = useState(""); const [expandedNote, setExpandedNote] = useState(null); const ref = useRef(null); useEffect(() => { if (open) setSnapshots(loadIndex()); }, [open]); useEffect(() => { if (!open) return; function onDoc(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); } window.addEventListener("mousedown", onDoc); return () => window.removeEventListener("mousedown", onDoc); }, [open]); function buildSessionSummaries(): SessionSummary[] { const sessionMap = new Map(sessions.map((s) => [s.id, s])); return teams .flatMap((t) => t.desks) .filter((d) => !("isPending" in d)) .flatMap((d) => { const s = sessionMap.get((d as Session).id); if (!s) return []; const summary: SessionSummary = { id: s.id, title: s.title_summary || s.title || s.id }; if (s.workspace_path) summary.workspacePath = s.workspace_path; return [summary]; }); } function handleSave() { const name = nameInput.trim(); if (!name) return; try { const current = localStorage.getItem(WORKBENCH_KEY_V2); if (!current) return; localStorage.setItem(SNAPSHOT_PREFIX + name, current); const index = loadIndex(); const existing = index.findIndex((s) => s.name === name); const meta: SnapshotMeta = { name, savedAt: new Date().toISOString(), note: noteInput.trim() || undefined, sessions: buildSessionSummaries(), }; if (existing >= 0) index[existing] = meta; else index.unshift(meta); saveIndex(index); setSnapshots(index); setNameInput(""); setNoteInput(""); } catch {} } function handleLoad(name: string) { try { const raw = localStorage.getItem(SNAPSHOT_PREFIX + name); if (!raw) return; localStorage.setItem(WORKBENCH_KEY_V2, raw); setOpen(false); onLoadSnapshot(); } catch {} } function handleDelete(name: string) { try { localStorage.removeItem(SNAPSHOT_PREFIX + name); const index = loadIndex().filter((s) => s.name !== name); saveIndex(index); setSnapshots(index); } catch {} } return (
{open && (
{/* ── Save form ── */}
Save current workbench
setNameInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") handleSave(); }} placeholder="Snapshot name…" style={{ flex: 1, background: "rgba(255,255,255,0.06)", border: "1px solid var(--card-border)", borderRadius: 6, padding: "5px 9px", color: "var(--text)", fontSize: 12, outline: "none", }} onFocus={(e) => (e.target.style.borderColor = "var(--accent2)")} onBlur={(e) => (e.target.style.borderColor = "var(--card-border)")} />