import { createPortal } from 'react-dom' import { useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { api, type HistoryEntry } from '../api' import { useCaps } from '../lib/capabilities' import { restoreWarning } from '../lib/platformCopy' import type { ToastMsg } from './Toast' interface Props { onClose: () => void onNotify: (kind: ToastMsg['kind'], text: string) => void onError: (msg: string) => void } function formatWhen(iso: string): string { const d = new Date(iso) if (Number.isNaN(d.getTime())) return iso return d.toLocaleString(undefined, { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }) } // Backend joins the summary's parts with "; " (e.g. "Added 6 hotcues; edited // beatgrid on 3 tracks"). Split them back out so each renders as its own chip // that wraps — far more scannable than one truncated run-on line. function summaryChips(summary: string): string[] { return summary .split('; ') .map((p) => p.trim()) .filter(Boolean) .map((p) => p.charAt(0).toUpperCase() + p.slice(1)) } /** * Version history browser for the current collection. Every save is a commit in * a purely-local git repo (see backend history.py); this lists them newest-first * and lets the user restore any past version (written back as a new forward save) * or wipe the entire history. Restore overwrites the collection on disk, so the * user is warned first, in the loaded platform's own terms. */ export function HistoryPanel({ onClose, onNotify, onError }: Props) { const caps = useCaps() const qc = useQueryClient() const history = useQuery({ queryKey: ['history'], queryFn: api.history }) const [confirmingId, setConfirmingId] = useState(null) const [clearing, setClearing] = useState(false) const restore = useMutation({ mutationFn: (id: string) => api.restoreVersion(id), onSuccess: () => { // A restore swaps the whole collection — refetch everything. qc.invalidateQueries() onNotify('success', 'Restored — the library now reflects that version.') setConfirmingId(null) onClose() }, onError: (e: Error) => { onError(e.message) setConfirmingId(null) }, }) const clear = useMutation({ mutationFn: () => api.clearHistory(), onSuccess: () => { qc.invalidateQueries({ queryKey: ['history'] }) onNotify('success', 'Version history deleted.') setClearing(false) }, onError: (e: Error) => { onError(e.message) setClearing(false) }, }) const entries = history.data ?? [] return createPortal(
e.stopPropagation()} >
Version History
Every save is a restorable version of this library. {restoreWarning(caps.save)}
{history.isLoading ? (
Loading…
) : entries.length === 0 ? (
No versions yet. Saving a change records the first one.
) : (
    {entries.map((e: HistoryEntry, i: number) => { const isCurrent = i === 0 const isConfirming = confirmingId === e.id return (
  • {isCurrent && ( current )} {summaryChips(e.summary).map((chip, ci) => ( {chip} ))}
    {formatWhen(e.timestamp)} · {e.id.slice(0, 8)}
    {isConfirming ? (
    ) : ( )}
  • ) })}
)}
{/* Destructive: wipe the whole history. */}
{clearing ? (
Permanently delete ALL {entries.length} version {entries.length === 1 ? '' : 's'}? This can't be undone.
) : (
)}
, document.body, ) }