Up And Down : première version
This commit is contained in:
commit
89c5430308
76 files changed
+20730
No files matched your search
@@ -0,0 +1,99 @@
|
||||
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<Version[] | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [notice, setNotice] = useState<string | null>(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<unknown>, 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 (
|
||||
<div className="modal-backdrop" onClick={onClose}>
|
||||
<div className="modal" role="dialog" aria-modal="true" aria-label={`Historique de ${name}`} onClick={(e) => e.stopPropagation()}>
|
||||
<h3>Historique · {name}</h3>
|
||||
<p className="muted small-text">
|
||||
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.
|
||||
</p>
|
||||
{error && <div className="error">{error}</div>}
|
||||
{notice && <div className="notice">{notice}</div>}
|
||||
<ul className="versions">
|
||||
{versions?.map((v) => (
|
||||
<li key={v.id}>
|
||||
<HistoryIcon size={16} />
|
||||
<div className="grow">
|
||||
<span title={fmtDate(v.time)}>{fmtRelative(v.time)}</span>
|
||||
<small className="muted">
|
||||
{" "}
|
||||
· {fmtDate(v.time)} · {fmtSize(v.size)}
|
||||
</small>
|
||||
</div>
|
||||
<a className="icon-btn" href={api.versionUrl(path, v.id)} download={name} title="Télécharger cette version" aria-label="Télécharger cette version">
|
||||
<DownloadIcon />
|
||||
</a>
|
||||
<button
|
||||
disabled={busy}
|
||||
title="Remplacer le fichier par cette version"
|
||||
onClick={() =>
|
||||
run(async () => {
|
||||
await api.restore(path, v.id);
|
||||
onChanged();
|
||||
}, "Version restaurée.")
|
||||
}
|
||||
>
|
||||
<RestoreIcon />
|
||||
<span>Restaurer</span>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
{versions && versions.length === 0 && <li className="muted empty">Aucune version antérieure.</li>}
|
||||
{!versions && !error && <li className="muted empty">Chargement…</li>}
|
||||
</ul>
|
||||
<div className="modal-actions">
|
||||
<button disabled={busy} onClick={() => run(() => api.snapshot(path), "Version créée.")} title="Fixer l'état actuel comme version">
|
||||
<HistoryIcon />
|
||||
<span>Créer une version</span>
|
||||
</button>
|
||||
<span className="spacer" />
|
||||
<button onClick={onClose}>Fermer</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user