"use client"; import { useState, useTransition } from "react"; import { Trash2, Loader2, CheckCircle2 } from "lucide-react"; import { previewOrphanFiles, purgeOrphanFiles } from "@/app/actions/maintenance"; import { formatBytes } from "@/lib/utils"; type State = | { kind: "idle" } | { kind: "scanning" } | { kind: "result"; count: number; bytes: number } | { kind: "deleted"; deleted: number; bytes: number }; export function PurgeOrphansButton() { const [state, setState] = useState({ kind: "idle" }); const [pending, start] = useTransition(); const scan = () => { setState({ kind: "scanning" }); start(async () => { const r = await previewOrphanFiles(); setState({ kind: "result", count: r.count, bytes: r.bytes }); }); }; const purge = () => { if (state.kind !== "result") return; if (!confirm(`Delete ${state.count} orphan file${state.count === 1 ? "" : "s"} (${formatBytes(state.bytes)}) from disk? Cannot be undone.`)) return; start(async () => { const r = await purgeOrphanFiles(); setState({ kind: "deleted", deleted: r.deleted, bytes: r.bytes }); }); }; return (
Purge Orphan Files
Find and delete files in the library / thumbnail folders that no image record references. Useful after deleting images with “Delete files from disk” turned off.
{state.kind === "result" && (
{state.count} orphan{state.count === 1 ? "" : "s"} · {formatBytes(state.bytes)}
)} {state.kind === "deleted" && (
Deleted {state.deleted} file{state.deleted === 1 ? "" : "s"} · freed {formatBytes(state.bytes)}
)}
{state.kind === "idle" && ( )} {state.kind === "scanning" && ( Scanning… )} {state.kind === "result" && state.count > 0 && ( <> )} {state.kind === "result" && state.count === 0 && ( )} {state.kind === "deleted" && ( )}
); }