"use client"; import { useCallback, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Field, Input, Select } from "@/components/ui/field"; import { InspectorFrame, SaidLine, announceManifestChanged, call, refusal, useOnManifestChanged, type InspectorProps, type Said } from "./shared"; // A TEASER OR CLOSING CARD (umtool redesign L6, item 2): with no `block`, a NEW card -- where it goes (the end: a // closing card; the start: a cold open; after an entry), its lines (the first its title; each with the end of // its words as a second tier, `break`, and a `hold`), its beat -- inserted; with `block` a teaser's id, its lines // and beat changed. POST /api/report/teaser (op insert | set); the build's teaser rules refuse in their words. // `umtool timeline

teaser-insert` / `timeline

teaser ` are the twins. type Line = { text: string; break: string; hold: string; keep?: Record }; type View = { entries: { id: string; type: string; at: number; label: string | null }[]; teasers: { id: string; at: number; lines: unknown[]; beat: number | null }[]; limits: { lines: [number, number]; rows: number; chars: number; hold: number; beat: { min: number; max: number } }; token: string | null; }; const blank = (): Line => ({ text: "", break: "", hold: "" }); /** A line as written into the editor's fields (keys it does not show kept as they are). */ function lineOf(l: unknown): Line { if (typeof l === "string") return { text: l, break: "", hold: "" }; const o = (l && typeof l === "object" ? l : {}) as Record; const { text, break: brk, hold, ...keep } = o; return { text: typeof text === "string" ? text : "", break: typeof brk === "string" ? brk : "", hold: typeof hold === "number" ? String(hold) : "", ...(Object.keys(keep).length ? { keep } : {}) }; } /** The line as the writer takes it: a string when it is only its words. */ function valueOf(l: Line): unknown { const o: Record = { ...(l.keep ?? {}), text: l.text.trim() }; if (l.break.trim()) o.break = l.break.trim(); if (l.hold.trim()) o.hold = Number(l.hold); return Object.keys(o).length === 1 ? o.text : o; } export default function TeaserInspector({ project, block = null }: InspectorProps) { const router = useRouter(); const [view, setView] = useState(null); const [lines, setLines] = useState([blank()]); const [id, setId] = useState(""); const [beat, setBeat] = useState(""); const [position, setPosition] = useState("end"); const [busy, setBusy] = useState(false); const [said, setSaid] = useState(null); const editing = block ? view?.teasers.find((t) => t.id === block) ?? null : null; const load = useCallback(async (reset = true) => { const { ok, j } = await call(`/api/report/teaser?project=${encodeURIComponent(project)}`); if (!ok) return null; setView(j); const t = block ? j.teasers.find((x) => x.id === block) : null; if (t && reset) { setLines(t.lines.length ? t.lines.map(lineOf) : [blank()]); setBeat(t.beat === null ? "" : String(t.beat)); } return j; }, [project, block]); useEffect(() => { void load(); }, [load]); useOnManifestChanged(project, () => load(false)); if (!view) return null; if (block && !editing) return

No teaser {block} in the draft.

; const setLine = (i: number, patch: Partial) => { setSaid(null); setLines((ls) => ls.map((l, k) => (k === i ? { ...l, ...patch } : l))); }; // "after:@": the entry's index names one of an id's twins (a variant's), as the writer's `at` does. const posValue = () => { if (position === "end" || position === "start") return position; const m = /^after:(.*)@(\d+)$/.exec(position); return m ? { after: m[1], at: Number(m[2]) } : { after: position.slice("after:".length) }; }; const submit = async () => { setBusy(true); setSaid(null); try { const values = lines.filter((l) => l.text.trim()).map(valueOf); const beatV = beat.trim() ? Number(beat) : null; const body = editing ? { project, token: view.token, op: "set", id: editing.id, patch: { lines: values, beat: beatV } } : { project, token: view.token, op: "insert", position: posValue(), spec: { lines: values, ...(id.trim() ? { id: id.trim() } : {}), ...(beatV !== null ? { beat: beatV } : {}) } }; const { ok, status, j } = await call<{ id?: string; at?: number }>("/api/report/teaser", { method: "POST", body: JSON.stringify(body) }); if (!ok) { setSaid({ ok: false, text: refusal(j, status) }); if (j.stale) await load(); return; } if (editing) { await load(); setSaid({ ok: true, text: `Saved ${j.id} to the draft.` }); } else { await load(); setLines([blank()]); setId(""); setBeat(""); setSaid({ ok: true, text: `Inserted ${j.id} at ${(j.at ?? 0) + 1} — Undo last edit takes it back.` }); } announceManifestChanged(project); router.refresh(); } finally { setBusy(false); } }; const lim = view.limits; return ( {!editing && (
setId(e.target.value)} /> setBeat(e.target.value)} />
)} {editing && (
setBeat(e.target.value)} />
)}
    {lines.map((l, i) => (
  1. setLine(i, { text: e.target.value })} /> setLine(i, { break: e.target.value })} /> setLine(i, { hold: e.target.value })} />
  2. ))}
); }