import { useState } from "react";
import { keepPreviousData, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Archive, History, RotateCcw, Trash2 } from "lucide-react";
import { api, type Operation, type Page, type Pair } from "../api";
import { Badge, Button, Card, Empty, Modal, Spinner, cx, formatAgo, formatBytes } from "../ui";
import { useToast } from "../toast";
export function ActivityPage() {
const [view, setView] = useState<"all" | "quarantine">("all");
return (
Activity
Every change playlink makes is recorded here and can be undone.
{(["all", "quarantine"] as const).map((v) => (
setView(v)}
className={cx("rounded-md px-3 py-1 text-sm capitalize cursor-pointer", view === v ? "bg-panel-2 text-white" : "text-muted")}
>
{v === "all" ? "History" : "Quarantine"}
))}
);
}
const actionLabel: Record = {
link: "Linked slave → master",
promote: "Promoted slave → master",
build: "Built into slave",
remove: "Removed from slave",
rename: "Renamed slave folder",
};
const statusTone: Record = {
done: "green",
failed: "red",
planned: "blue",
interrupted: "amber",
running: "gray",
};
export function OpsTable({ pairId, quarantine }: { pairId?: number; quarantine?: boolean }) {
const [page, setPage] = useState(0);
const [confirm, setConfirm] = useState(null);
const qc = useQueryClient();
const toast = useToast();
const pairs = useQuery({ queryKey: ["pairs"], queryFn: () => api.get("/pairs") });
const names = Object.fromEntries((pairs.data ?? []).map((p) => [p.id, p.name]));
const ops = useQuery({
queryKey: ["ops", pairId, quarantine, page],
queryFn: () => api.get>(`/operations?limit=50&offset=${page * 50}${pairId ? `&pair=${pairId}` : ""}${quarantine ? "&quarantine=1" : ""}`),
placeholderData: keepPreviousData,
});
const act = useMutation({
mutationFn: ({ op, kind }: { op: Operation; kind: "undo" | "purge" }) => api.post(`/operations/${op.id}/${kind}`),
onSuccess: (_, { kind }) => {
toast("ok", kind === "undo" ? "Operation undone" : "Quarantined file permanently deleted");
setConfirm(null);
qc.invalidateQueries({ queryKey: ["ops"] });
qc.invalidateQueries({ queryKey: ["matches"] });
qc.invalidateQueries({ queryKey: ["pair"] });
},
onError: (e) => toast("err", e.message),
});
if (ops.isLoading) return ;
const items = ops.data?.items ?? [];
return (
<>
{items.length === 0 && (
: } title={quarantine ? "Quarantine is empty" : "No activity yet"}>
{quarantine ? "Originals replaced by a non-identical file show up here until they are purged." : undefined}
)}
{items.map((o) => {
const canUndo = o.status === "done" && !o.undone_at && !o.purged_at;
const inQuarantine = !!o.quarantine_path && !o.purged_at && !o.undone_at && (o.status === "done" || o.status === "interrupted");
return (
{actionLabel[o.action]}
{formatAgo(o.created_at)} · {o.trigger}
{!pairId && names[o.pair_id] && ` · ${names[o.pair_id]}`}
{o.rel_slave}
{o.rel_new && o.rel_new !== o.rel_slave && o.rel_new !== o.rel_master && (
now {o.rel_new}
)}
{o.error &&
{o.error}
}
{inQuarantine && (
{o.quarantine_side} original kept in quarantine
)}
{o.bytes > 0 && o.status === "done" && !o.undone_at && −{formatBytes(o.bytes)} }
{o.dry_run ? "dry run" : o.status}
{o.undone_at && undone }
{o.purged_at && purged }
{canUndo && (
setConfirm({ op: o, kind: "undo" })}>
{o.quarantine_path ? "Restore" : "Undo"}
)}
{inQuarantine && (
setConfirm({ op: o, kind: "purge" })} title="Delete quarantined original">
)}
);
})}
{ops.data && ops.data.total > 50 && (
setPage(page - 1)}>
Previous
= ops.data.total} onClick={() => setPage(page + 1)}>
Next
)}
setConfirm(null)} title={confirm?.kind === "undo" ? "Undo this operation?" : "Permanently delete?"}>
{confirm && (
{confirm.kind === "purge" ? (
<>
The quarantined {confirm.op.quarantine_side} original of {confirm.op.rel_slave} will be deleted.{" "}
This cannot be undone.
>
) : (
undoText(confirm.op)
)}
)}
setConfirm(null)}>
Cancel
confirm && act.mutate(confirm)}>
{confirm?.kind === "undo" ? "Undo" : "Delete forever"}
>
);
}
function undoText(o: Operation) {
switch (o.action) {
case "link":
return o.quarantine_path
? "The original slave file is restored from quarantine and the hardlink is removed."
: "The slave gets its own independent copy of the file again, which uses disk space.";
case "promote":
return "The original master file is restored from quarantine. The slave keeps its own file.";
case "build":
return "The hardlink is removed from the slave library. The master file is not affected.";
case "remove":
return "The hardlink is recreated in the slave library.";
case "rename":
return `The slave folder is renamed back from "${o.rel_new}" to "${o.rel_slave}", and the library is rescanned.`;
}
}