import { useEffect } from 'react' import { useDiagramStore } from '../../state/diagramStore' import Modal from '../common/Modal' function formatUpdatedAt(iso: string): string { const date = new Date(iso) return date.toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' }) } export default function DiagramManagerModal({ onClose }: { onClose: () => void }) { const diagram = useDiagramStore((s) => s.diagram) const diagramList = useDiagramStore((s) => s.diagramList) const refreshDiagramList = useDiagramStore((s) => s.refreshDiagramList) const newDiagram = useDiagramStore((s) => s.newDiagram) const switchToDiagram = useDiagramStore((s) => s.switchToDiagram) const deleteDiagram = useDiagramStore((s) => s.deleteDiagram) useEffect(() => { refreshDiagramList() }, [refreshDiagramList]) const handleOpen = async (id: string) => { if (id === diagram.id) return await switchToDiagram(id) onClose() } const handleNew = async () => { await newDiagram() onClose() } const handleDelete = async (id: string, name: string) => { const confirmed = window.confirm( diagramList.length === 1 ? `Delete "${name}"? This is your only diagram, so a fresh empty one will replace it.` : `Delete "${name}"? This can't be undone.`, ) if (!confirmed) return await deleteDiagram(id) } return (
) }