import { useCallback, useEffect, useState } from "react"; import { api, type Version } from "./api"; import { DownloadIcon, HistoryIcon, RestoreIcon } from "./icons"; import { fmtDate, fmtRelative, fmtSize } from "./format"; interface Props { path: string; name: string; onClose: () => void; /** Appelé après une restauration (le fichier a changé). */ onChanged: () => void; } /** Versions antérieures d'un fichier : téléchargement, restauration, création d'une version à la demande. */ export function HistoryDialog({ path, name, onClose, onChanged }: Props) { const [versions, setVersions] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const load = useCallback(() => api.history(path).then(setVersions).catch((e) => setError(e.message)), [path]); useEffect(() => { load(); }, [load]); useEffect(() => { const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose(); window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [onClose]); const run = async (fn: () => Promise, done?: string) => { setBusy(true); setError(null); setNotice(null); try { await fn(); await load(); if (done) setNotice(done); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } }; return (
e.stopPropagation()}>

Historique · {name}

Quand un fichier est remplacé, son contenu précédent est conservé ici. Restaurer une version garde l'état actuel dans l'historique : l'opération s'annule.

{error &&
{error}
} {notice &&
{notice}
}
    {versions?.map((v) => (
  • {fmtRelative(v.time)} {" "} · {fmtDate(v.time)} · {fmtSize(v.size)}
  • ))} {versions && versions.length === 0 &&
  • Aucune version antérieure.
  • } {!versions && !error &&
  • Chargement…
  • }
); }