"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