"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 { workHref } from "@/lib/hrefs"; import type { Cadence } from "@/lib/schedule-types"; import CadenceFields from "./CadenceFields"; // --------------------------------------------------------------------------- // A RECURRING REPORT'S CONTROLS (plans/umtool-redesign.md, R5): Pause/Resume, // Tick now, Edit cadence, Delete -- each the editor's `report-schedule` action // through POST /api/schedules (umtool never writes a schedule itself). The // page re-reads after each. // // part "all" on a series' page; on a living report's article the head // has "head" (Pause/Resume, Tick now, beside the chip) and the ⋯ // menu "menu" (Edit cadence, Delete) // // Delete is plan-then-confirm (the DeletePlan pattern): the trigger asks the // editor for the plan (what goes, what stays, what refuses), the plan takes // the focus, and only "Confirm delete" acts; "Keep it" (or Escape) closes it. // Labels are contracts (e2e/recurring.spec.ts): the buttons "Pause schedule", // "Resume schedule", "Tick now", "Edit cadence", "Save cadence", "Delete // schedule", the region "delete schedule ", "Confirm delete", "Keep it". // --------------------------------------------------------------------------- type Sched = { id: string; shape: string; paused: boolean; cadence: Cadence; running: boolean }; async function ask(body: Record): Promise> { const r = await fetch("/api/schedules", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); const b = (await r.json().catch(() => ({}))) as Record & { error?: string }; if (!r.ok) throw new Error(b.error ?? `HTTP ${r.status}`); return b; } export default function ScheduleControls({ schedule, part = "all" }: { schedule: Sched; part?: "head" | "menu" | "all" }) { const router = useRouter(); const hydrated = useHydrated(); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [said, setSaid] = useState(null); const [editing, setEditing] = useState(false); const [cadence, setCadence] = useState(schedule.cadence); const [plan, setPlan] = useState<{ removes: string; keeps: string[]; refused: string | null } | null>(null); const deleteRef = useRef(null); const editRef = useRef(null); // Closing the cadence form puts the focus back on its button. const closeEdit = () => { setEditing(false); requestAnimationFrame(() => editRef.current?.focus()); }; const confirmRef = useRef(null); const opened = useRef(false); useEffect(() => { if (plan) { opened.current = true; confirmRef.current?.focus(); } else if (opened.current) { opened.current = false; deleteRef.current?.focus(); } }, [plan]); const act = async (what: string, body: Record, done?: (b: Record) => void) => { setBusy(what); setError(null); setSaid(null); try { const b = await ask({ id: schedule.id, ...body }); done?.(b); router.refresh(); } catch (e) { setError((e as Error).message); } finally { setBusy(null); } }; const head = part === "head" || part === "all"; const menu = part === "menu" || part === "all"; return ( {head && ( <> )} {menu && ( <> {!editing ? ( ) : (
{ if (e.key === "Escape") { e.preventDefault(); closeEdit(); } }} onSubmit={(e) => { e.preventDefault(); void act("cadence", { action: "update", cadence }, closeEdit); }} > )} {!plan ? ( ) : (
{ if (e.key === "Escape") { e.preventDefault(); setPlan(null); } }} >

Delete {plan.removes}. There is no undo.

{plan.keeps.length > 0 && (
    {plan.keeps.map((k) => (
  • {k}
  • ))}
)} {plan.refused && (

{plan.refused}

)}
)} )} {said && ( {said} )} {error && ( {error} )}
); }