import { useEffect, useState } from 'react' import { useDiagramSnapshotStore } from '../../state/diagramSnapshotStore' import { useDiagramStore } from '../../state/diagramStore' import Modal from '../common/Modal' function formatDateTime(iso: string): string { return new Date(iso).toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' }) } export default function VersionHistoryModal({ onClose }: { onClose: () => void }) { const diagram = useDiagramStore((s) => s.diagram) const access = useDiagramStore((s) => s.access) const canRestore = access?.myPermission === 'owner' || access?.myPermission === 'edit' const diagramId = diagram.id const snapshots = useDiagramSnapshotStore((s) => s.snapshots) const isLoaded = useDiagramSnapshotStore((s) => s.isLoaded) const load = useDiagramSnapshotStore((s) => s.load) const restore = useDiagramSnapshotStore((s) => s.restore) const [restoringId, setRestoringId] = useState(null) useEffect(() => { load(diagramId) }, [diagramId, load]) const handleRestore = async (snapshotId: string, createdAt: string) => { const confirmed = window.confirm( `Restore the diagram to how it was on ${formatDateTime(createdAt)}? If you've made changes since your last restore, the current state is saved as a new checkpoint first, so this can be undone.`, ) if (!confirmed) return setRestoringId(snapshotId) try { await restore(diagramId, snapshotId) } finally { setRestoringId(null) } } return (

Checkpoints saved periodically while editing (up to the 50 most recent).

{!isLoaded &&

Loading…

} {isLoaded && snapshots.length === 0 && (

No checkpoints yet.

)} {snapshots.map((s) => (
{formatDateTime(s.createdAt)}
{s.savedByUsername &&
by {s.savedByUsername}
}
{canRestore && ( )}
))}
) }