"use client";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { buttonVariants } from "@/components/ui/button";
import { cn } from "@/lib/utils";
// ---------------------------------------------------------------------------
// SAVE VERSION (umtool redesign L3; lib/report/versions.mjs saveVersion; the
// CLI twin is `umtool takes save
[--label L]`): the working draft frozen
// as the next v. Nothing is saved when the draft has no edits since its
// parent -- said, beside the button. The pick is never touched.
// ---------------------------------------------------------------------------
export default function SaveVersionButton({ project, className }: { project: string; className?: string }) {
const router = useRouter();
const [state, setState] = useState<{ busy: boolean; said: string | null; error: string | null }>({ busy: false, said: null, error: null });
const save = async () => {
setState({ busy: true, said: null, error: null });
const r = await fetch("/api/report/takes/save", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project }) });
const j = (await r.json().catch(() => ({}))) as { saved?: boolean; id?: string; why?: string; error?: string };
if (!r.ok) return setState({ busy: false, said: null, error: j.error ?? `HTTP ${r.status}` });
setState({ busy: false, said: j.saved ? `saved as ${j.id}` : (j.why ?? "nothing to save"), error: null });
router.refresh();
};
return (
{(state.said || state.error) && (
{state.error ?? state.said}
)}
);
}