"use client"; import { useRouter } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { useHydrated } from "@/components/ui/use-hydrated"; import type { ActionAnswer, ActionPlanView } from "@/lib/system/plan-types"; // --------------------------------------------------------------------------- // PLAN, THEN CONFIRM for Settings · system's actions (P5) -- the DeletePlan // pattern (components/DeletePlan.tsx) for a move or a prune: the button asks // the route for the PLAN (a dry run: what it touches, with sizes, what refuses, // what is worth knowing), shows it, and acts only on the confirm, whose words // say what happens ("Move out", "Drop 2 records"). "Keep it" closes the plan // and nothing was written. A long one starts a umtool job (the rail's Running // list has it); the page re-reads when it is started. // // Labels are contracts (e2e/settings.spec.ts): the region is "plan " // holding the lists "touches", "refused" and "worth knowing", and the buttons // (data-plan-confirm) 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"; done?: string } | { at: "asking" } | { at: "plan"; plan: ActionPlanView; error: string | null } | { at: "working"; plan: ActionPlanView } | { at: "error"; error: string }; async function post(endpoint: string, body: Record) { const r = await fetch(endpoint, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); const b = (await r.json().catch(() => ({}))) as Partial & { plan?: ActionPlanView; error?: string }; return { ok: r.ok, status: r.status, body: b }; } export default function ActionPlan({ label, subject, endpoint, body, }: { /** The trigger's words ("Move out", "Prune"). */ label: string; /** What it acts on, for the region's name ("index prune", "reports/x out"). */ subject: string; endpoint: string; body: Record; }) { const router = useRouter(); const hydrated = useHydrated(); const [phase, setPhase] = useState({ at: "closed" }); // The keys follow the plan: it opens on its confirm (or Keep it when there // is nothing to confirm), and closing returns to the button that opened it. const trigger = useRef(null); const first = useRef(null); const opened = useRef(false); useEffect(() => { if (phase.at === "plan" || phase.at === "error") { opened.current = true; first.current?.focus(); } else if (phase.at === "closed" && opened.current) { opened.current = false; trigger.current?.focus(); } }, [phase.at]); const ask = async () => { setPhase({ at: "asking" }); try { const r = await post(endpoint, { ...body, dryRun: true }); if (r.body.plan) setPhase({ at: "plan", plan: r.body.plan, error: null }); else setPhase({ at: "error", error: r.body.error ?? `HTTP ${r.status}` }); } catch (e) { setPhase({ at: "error", error: (e as Error).message }); } }; const confirm = async (plan: ActionPlanView) => { setPhase({ at: "working", plan }); try { const r = await post(endpoint, body); if (r.ok && r.body.applied) { setPhase({ at: "closed", done: r.body.job ? `${r.body.message} — a job, in Running` : r.body.message }); router.refresh(); return; } setPhase({ at: "plan", plan: r.body.plan ?? plan, error: r.body.error ?? `HTTP ${r.status}` }); } catch (e) { setPhase({ at: "plan", plan, error: (e as Error).message }); } }; if (phase.at === "closed") { return ( {phase.done && ( {phase.done} )} ); } if (phase.at === "asking") return Reading the plan…; if (phase.at === "error") { return ( {phase.error} ); } const plan = phase.plan; return (
{ if (e.key === "Escape" && phase.at === "plan") { e.preventDefault(); setPhase({ at: "closed" }); } }} >

{plan.title}

{plan.summary}

{plan.refusals.length > 0 && (
    {plan.refusals.map((r, i) => (
  • {r}
  • ))}
)} {plan.warnings.length > 0 && (
    {plan.warnings.map((w, i) => (
  • {w}
  • ))}
)} {plan.rows.length > 0 && (
    {plan.rows.map((r, i) => (
  • {r.label} {r.what} {r.bytes !== undefined ? `${fmt(r.bytes)}${r.files !== undefined ? ` · ${r.files} files` : ""}` : ""}
  • ))}
)} {phase.at === "plan" && phase.error && (

{phase.error}

)}
{!plan.blocked && ( )}
); }