"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { blockedBy, type DeleteAnswer, type DeletePlanView } from "@/lib/delete-plan"; // ONE WAY TO DELETE ANYTHING (release 27 follow-up): an article, an iteration, // a project, a take. The button asks the route for the PLAN (a dry run: every // path that would go, with its size, what is kept and why, what refuses), shows // it, and deletes only on "Confirm delete". A refusal force can answer (a // published article, a take the manifest is) offers "Delete anyway", which // asks for the plan again with force before the confirm is offered. // // Labels are contracts (e2e/article-delete.spec.ts): the trigger is `label` // ("Delete article", "Delete i3", "Delete project", "Delete take "); the // plan is the region "delete " holding the lists "what goes", "what is // kept", "refused", the checkbox "delete anyway", and the buttons // "Confirm delete" and "Keep it". const fmt = (n: number) => { if (n < 1024) return `${n} B`; const u = ["KiB", "MiB", "GiB", "TiB"]; let v = n / 1024; let i = 0; while (v >= 1024 && i < u.length - 1) { v /= 1024; i += 1; } return `${v.toFixed(v >= 100 ? 0 : 1)} ${u[i]}`; }; type Phase = | { at: "closed" } | { at: "asking" } | { at: "plan"; plan: DeletePlanView; error: string | null } | { at: "deleting"; plan: DeletePlanView } | { at: "error"; error: string }; export default function DeletePlan({ label, subject, endpoint, body, forceLabel = "delete anyway", verb = "Delete", confirmLabel = "Confirm delete", onDone, size = "sm", }: { label: string; subject: string; endpoint: string; body: Record; forceLabel?: string; /** The plan's first word ("Free" for the takes' work files); the region keeps its "delete " name. */ verb?: string; confirmLabel?: string; onDone: (answer: Extract) => void; size?: "sm" | "md"; }) { const [phase, setPhase] = useState({ at: "closed" }); const [force, setForce] = useState(false); const ask = async (withForce: boolean) => { setPhase({ at: "asking" }); try { const r = await fetch(endpoint, { method: "POST", body: JSON.stringify({ ...body, dryRun: true, force: withForce }) }); const b = (await r.json()) as { plan?: DeletePlanView; error?: string }; if (b.plan) setPhase({ at: "plan", plan: b.plan, error: null }); else setPhase({ at: "error", error: b.error ?? `HTTP ${r.status}` }); } catch (e) { setPhase({ at: "error", error: (e as Error).message }); } }; const confirm = async (plan: DeletePlanView) => { setPhase({ at: "deleting", plan }); try { const r = await fetch(endpoint, { method: "POST", body: JSON.stringify({ ...body, force }) }); const b = (await r.json()) as DeleteAnswer & { error?: string }; if (r.ok && b.applied) { setPhase({ at: "closed" }); onDone(b); return; } setPhase({ at: "plan", plan: b.plan ?? plan, error: b.error ?? `HTTP ${r.status}` }); } catch (e) { setPhase({ at: "plan", plan, error: (e as Error).message }); } }; if (phase.at === "closed") { return ( ); } if (phase.at === "asking") return reading what would go…; if (phase.at === "error") { return ( {phase.error} ); } const plan = phase.plan; const blocked = blockedBy(plan, force); const forceable = plan.refusals.length > 0 && plan.refusals.every((r) => r.forceable); return (
0 ? "yes" : "no"} className="mt-2 basis-full rounded border border-[var(--color-bad)] bg-[var(--color-ink)] p-2 text-[12px]" >

{verb} {subject}: {plan.remove.length} path{plan.remove.length === 1 ? "" : "s"},{" "} {fmt(plan.bytes)} {" "} in {plan.files} file{plan.files === 1 ? "" : "s"}. There is no undo.

{plan.refusals.length > 0 && (
    {plan.refusals.map((r, i) => (
  • {r.reason}
  • ))}
)} {plan.steps.length > 0 && (
    {plan.steps.map((s, i) => (
  • {s}
  • ))}
)}
    {plan.remove.map((e) => (
  • {e.label} {e.what}{" "} {e.kind === "link" ? "a link" : fmt(e.bytes)} {e.parts && e.parts.length > 0 && ( {e.parts .slice(0, 8) .map((p) => `${p.name} ${fmt(p.bytes)}`) .join(" · ")} {e.parts.length > 8 ? ` · … ${e.parts.length - 8} more` : ""} )}
  • ))}
{plan.kept.length > 0 && (
kept ({plan.kept.length})
    {plan.kept.map((k, i) => (
  • {k.what}: {k.why}
  • ))}
)} {phase.at === "plan" && phase.error && (

{phase.error}

)}
{forceable && ( )}
); }