"use client"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { buttonVariants, type ButtonVariants } from "@/components/ui/button"; import { useHydrated } from "@/components/ui/use-hydrated"; import { cn } from "@/lib/utils"; // --------------------------------------------------------------------------- // A piece's lifecycle and its mutes, from a list: Park, Mark done, Reactivate // (POST /api/pieces/lifecycle), Mute / Unmute one kind of call on it // (POST /api/pieces/mute). Reversible, so no plan-then-confirm: the page's // offers the way back for ten seconds instead, and says plainly what // the change did ("Marked done — hidden from Active"), because a piece that // leaves Active leaves every list that shows Active too. // // A button says what it did by an event the bar hears (`umtool:lifecycle`), // because the line it sat on is gone after the refresh -- a parked piece's // calls, and a muted kind, are hidden. // --------------------------------------------------------------------------- export type LifecycleState = "active" | "parked" | "done"; /** What was done, and the POST that takes it back. */ type Change = { piece: string; title: string; word: string; note?: string; undo: { url: string; body: Record } }; const EVENT = "umtool:lifecycle"; const DONE_WORD: Record = { active: "Reactivated", parked: "Parked", done: "Marked done" }; /** What the change means for the lists, after the word. */ const DONE_NOTE: Record = { active: "back in Active", parked: "hidden from Active", done: "hidden from Active" }; /** How long the bar offers Undo. */ const UNDO_MS = 10_000; const LIFECYCLE = "/api/pieces/lifecycle"; const MUTE = "/api/pieces/mute"; async function send(url: string, body: Record): Promise { try { const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body), cache: "no-store", }); if (res.ok) return null; const j = (await res.json().catch(() => null)) as { error?: string } | null; return j?.error ?? `HTTP ${res.status}`; } catch (err) { return (err as Error).message; } } export function LifecycleButton({ piece, title, from, to, label, variant = "quiet", size = "sm", className, }: { piece: string; title: string; from: LifecycleState; to: LifecycleState; label: string; variant?: ButtonVariants["variant"]; size?: ButtonVariants["size"]; className?: string; }) { const hydrated = useHydrated(); const router = useRouter(); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); return ( <> {error && ( {error} )} ); } /** Mute (or Unmute) one kind of call on a piece: the server's, beside the lifecycle. */ export function MuteButton({ piece, kind, title, muted, label, variant = "quiet", size = "sm", className, }: { piece: string; kind: string; title: string; /** Muted now: the button unmutes. */ muted: boolean; label?: string; variant?: ButtonVariants["variant"]; size?: ButtonVariants["size"]; className?: string; }) { const hydrated = useHydrated(); const router = useRouter(); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); return ( <> {error && ( {error} )} ); } /** "Marked done — hidden from Active. Quartering diet · Undo", for ten seconds after a change. */ export function UndoBar() { const router = useRouter(); const [last, setLast] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); useEffect(() => { const on = (e: Event) => { setError(null); setLast((e as CustomEvent).detail); }; window.addEventListener(EVENT, on); return () => window.removeEventListener(EVENT, on); }, []); useEffect(() => { if (!last || busy) return; const t = setTimeout(() => setLast(null), UNDO_MS); return () => clearTimeout(t); }, [last, busy]); if (!last) return null; return (
{last.word} {last.note ? ` — ${last.note}.` : ""} {" "} {last.title} {error && {error}}
); }