"use client"; import { useMemo, useRef, useState } from "react"; import { SCREEN_KINDS, lineOfText, screenFromRead, screenReadLines, screenWarnings, shapeSpans, validateScreens, withoutLines } from "umtool-report-to-video/post-screens"; import { isNarrated, pickedTake } from "umtool-report-to-video/post-narration"; import { ownPictureOnly } from "umtool-report-to-video/post-item"; import { lineTakeWarnings, lineTakesOf, partRoles, threadReadErrors } from "umtool-report-to-video/post-thread"; import { postFileUrl } from "./review/PostItemPlayer"; import { ScreenLineVoice, lineReadings, useScreenVoice, type LineTakeOption, type ScreenVoice, type VoiceEntry } from "./ScreenLineVoice"; // --------------------------------------------------------------------------- // A POST's SCREENS ON THE BENCH (report-to-video/post-screens.mjs; docs/report-video.md, "Screens"; operator, 12 Nov: // "The 'read text screenshots as posts' ability in different variations should be first class"): the text screenshots // the post carries -- each the attachment it is in (and, in a video, the seconds it is visible), what it is (DMs, an // email, a post, text), its lines transcribed by hand (pasted as `who: words | time`), who says each and on which side, // which lines are read (a line's excerpt trimmed to the words read), and whether a person checked the transcription. // No model: everything here is typed. The bench's Save writes it (updatePostItem's `screens`); the read is made with // `umtool posts screen

read` (the line shown here, and its button). A read line may be RECORDED in the // operator's voice (ScreenLineVoice: `posts screen … record`) and that take CHOSEN for it (`lineTakes`). // --------------------------------------------------------------------------- type Line = { who?: string; side?: "left" | "right"; text: string; time?: string; redact?: true }; type Read = "all" | "none" | Array<{ line: number; text?: string }>; type Screen = { id: string; media: number; at?: [number, number]; kind: string; title?: string; source?: string; checked?: { by: string; at: string }; ref?: string; unread?: "omit" | "dim"; lines: Line[]; read: Read; saidAs?: Record; lineTakes?: Record }; type LineRecs = Array<{ role: string; screen: string; n: number; takes: Array<{ n: number; id: string; take: { steps?: Array<{ op: string; voice?: string }>; engine?: string; screenLine?: { heard?: unknown } }; current: boolean; chosen: boolean }> }>; /** A recording's step as the select says it: recorded, cleaned, converted to its voice. */ const stepOf = (t: { steps?: Array<{ op: string; voice?: string }>; engine?: string }) => { const st = t.steps?.[0]; return st?.op === "convert" ? `converted to ${String(st.voice ?? "a stock voice").split(":").pop()}` : st?.op === "enhance" ? "cleaned" : "recorded"; }; type Media = { src: string; kind: string; seconds?: number }; type Entry = Record & { id: string; media?: Media[]; screens?: Screen[] }; const r2 = (v: number) => Math.round(v * 100) / 100; /** The read as a list of `{ line, text? }` (all: every line not redacted; none: none). */ const readList = (sc: Screen): Array<{ line: number; text?: string }> => (sc.read === "all" ? sc.lines.map((l, i) => (l.redact ? null : { line: i })).filter((x): x is { line: number } => !!x) : sc.read === "none" ? [] : sc.read); export default function PostScreensPanel({ project, entry, saved, setEntry, onEntry }: { project: string; entry: Entry; saved: Entry; setEntry: (f: (e: Entry) => Entry) => void; onEntry: (e: Entry, token: string | null) => void }) { const screens = useMemo(() => (Array.isArray(entry.screens) ? entry.screens : []), [entry.screens]); // THE OPERATOR's VOICE for a line: one mic, one job; a line is recorded as SAVED (the draft differing: Save first). const dirty = JSON.stringify(entry) !== JSON.stringify(saved); const dirtyNow = useRef(dirty); dirtyNow.current = dirty; const voice = useScreenVoice({ project, id: entry.id, onEntry: onEntry as (e: VoiceEntry, t: string | null) => void, unsaved: () => dirtyNow.current }); const recs = useMemo(() => lineTakesOf(entry) as LineRecs, [entry]); const lineWarnings = useMemo(() => lineTakeWarnings(entry) as string[], [entry]); const media = Array.isArray(entry.media) ? entry.media : []; const errors = useMemo(() => validateScreens(entry) as string[], [entry]); // The build's guard on the read as it is (post-thread.mjs): a line edited, removed or moved under a read is refused // until the read is made again -- Save keeps the screens; the build will not play the old read. const refused = useMemo(() => (isNarrated(entry) ? (threadReadErrors(entry, {}) as string[]).filter((x) => /screen [a-z0-9-]+/.test(x)) : []), [entry]); const warnings = useMemo(() => screenWarnings(entry, { narrated: isNarrated(entry), takeRoles: pickedTake(entry) ? (partRoles(entry) as Array<{ role: string | null }>).map((r) => r.role) : [] }) as string[], [entry]); const [paste, setPaste] = useState>({}); const setScreens = (f: (list: Screen[]) => Screen[]) => setEntry((e) => { const next = f(Array.isArray(e.screens) ? (e.screens as Screen[]) : []); const out = { ...e }; if (next.length) out.screens = next; else delete out.screens; return out; }); const setScreen = (id: string, f: (s: Screen) => Screen) => setScreens((list) => list.map((s) => (s.id === id ? f(s) : s))); // Lines or words changed: the check was for the words before. const setLines = (id: string, f: (lines: Line[]) => Line[]) => setScreen(id, (s) => { const { checked: _c, ...rest } = s; void _c; return { ...rest, lines: f(s.lines) }; }); const nextId = () => { const have = new Set(screens.map((s) => s.id)); let n = 1; while (have.has(`s${n}`)) n += 1; return `s${n}`; }; // A PICTURE-ONLY post's read made its screen (post-screens.mjs screenFromRead: `posts screen … add --from-read`). const pictureOnly = ownPictureOnly(entry) as boolean; const script = typeof (entry.narration as { script?: unknown } | undefined)?.script === "string" ? String((entry.narration as { script: string }).script) : ""; const [fromReadError, setFromReadError] = useState(null); const addFromRead = () => { const made = screenFromRead(entry, { pictureOnly, id: nextId() }) as { screen?: Screen; error?: string }; setFromReadError(made.error ?? null); if (made.screen) setScreens((list) => [...list, made.screen as Screen]); }; const add = () => { const m = media[0]; const sc: Screen = { id: nextId(), media: 0, kind: "dm", source: "transcribed", lines: [], read: "all", ...(m && (m.kind === "video" || m.kind === "gif") ? { at: [0, r2(Math.min(5, m.seconds ?? 5))] as [number, number] } : {}) }; setScreens((list) => [...list, sc]); }; if (!media.length) return null; return (

screens

text screenshots in its attachments, transcribed, drawn on the card and read after its words {pictureOnly && script.trim() && ( )}
{fromReadError && (

{fromReadError}

)} {screens.map((sc) => ( setPaste((p) => ({ ...p, [sc.id]: v }))} setScreen={(f) => setScreen(sc.id, f)} setLines={(f) => setLines(sc.id, f)} remove={() => setScreens((list) => list.filter((s) => s.id !== sc.id))} voice={voice} dirty={dirty} recs={recs} /> ))} {screens.length > 0 && (
umtool posts screen {project} {entry.id} read
)} {voice.job && (

{voice.job.what === "line" ? `${voice.job.role}: ` : "the read: "} {voice.job.state === "running" ? voice.job.message ?? "running…" : voice.job.state === "failed" ? voice.job.error ?? "failed" : "done"}

)} {voice.status && (

{voice.status}

)} {voice.mic.error && (

{voice.mic.error}

)} {errors.length > 0 && ( )} {refused.length > 0 && ( )} {warnings.length + lineWarnings.length > 0 && ( )}
); } function ScreenEditor({ project, entry, sc, media, paste, setPaste, setScreen, setLines, remove, voice, dirty, recs, }: { voice: ScreenVoice; dirty: boolean; recs: LineRecs; project: string; entry: Entry; sc: Screen; media: Media[]; paste: string; setPaste: (v: string) => void; setScreen: (f: (s: Screen) => Screen) => void; setLines: (f: (lines: Line[]) => Line[]) => void; remove: () => void; }) { const m = media[sc.media]; const timed = m?.kind === "video" || m?.kind === "gif"; const video = useRef(null); const reads = new Map(readList(sc).map((r) => [r.line, r])); const drawnReads = new Map((screenReadLines(sc) as Array<{ n: number; cutBefore: boolean; cutAfter: boolean }>).map((r) => [r.n, r])); const setRead = (list: Array<{ line: number; text?: string }>) => setScreen((s) => ({ ...s, read: list.length === s.lines.filter((l) => !l.redact).length && list.every((x) => !x.text) ? "all" : list.length ? [...list].sort((a, b) => a.line - b.line) : "none" })); const toggleRead = (n: number, on: boolean) => { const list = readList(sc).filter((x) => x.line !== n); setRead(on ? [...list, { line: n }] : list); }; const setExcerpt = (n: number, text: string) => setRead([...readList(sc).filter((x) => x.line !== n), text.trim() ? { line: n, text } : { line: n }]); // What line n is SAID AS (saidAs; lane line-rerun): never drawn -- the card shows the line as written. const setSaidAs = (n: number, text: string) => setScreen((s) => { const sa = { ...(s.saidAs ?? {}) }; if (text.trim()) sa[String(n)] = text; else delete sa[String(n)]; const { saidAs: _a, ...rest } = s; void _a; return Object.keys(sa).length ? { ...rest, saidAs: sa } : rest; }); const spaced = (t: string) => t.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim(); const setLine = (n: number, patch: Partial) => setLines((lines) => lines.map((l, i) => { if (i !== n) return l; const out: Record = { ...l, ...patch }; for (const [k, v] of Object.entries(out)) if (v === undefined || v === "" || v === false) if (k !== "text") delete out[k]; return out as Line; }), ); const at = sc.at ?? [0, 0]; return (
{sc.id} {timed && ( visible setScreen((s) => ({ ...s, at: [r2(Number(e.target.value)), at[1]] }))} data-post-screen-at-from="" aria-label="from (s)" /> – setScreen((s) => ({ ...s, at: [at[0], r2(Number(e.target.value))] }))} data-post-screen-at-to="" aria-label="to (s)" /> s )}
{timed && m && (
)} {sc.lines.length > 0 && ( {sc.lines.map((l, n) => { const r = reads.get(n); const d = drawnReads.get(n); const shapes = (shapeSpans(l.text) as Array<{ kind: string }>).map((x) => x.kind); return ( ); })}
# read who side words (the excerpt read below, when trimmed) time redact your voice
{n} toggleRead(n, e.target.checked)} data-post-screen-read={n} aria-label={`read line ${n}`} /> setLine(n, { who: e.target.value || undefined })} data-post-screen-who={n} /> setLine(n, { text: e.target.value })} data-post-screen-text={n} /> {r && !l.redact && ( setExcerpt(n, e.target.value)} data-post-screen-excerpt={n} /> )} {d && !l.redact && ( setSaidAs(n, e.target.value)} aria-label={`said as, line ${n}`} data-post-screen-said-as={n} /> )} {d && (d.cutBefore || d.cutAfter) && reads an excerpt} {shapes.length > 0 && !l.redact && · masked: {shapes.join(", ")}} setLine(n, { time: e.target.value || undefined })} data-post-screen-time={n} /> { setLine(n, { redact: e.target.checked ? true : undefined }); // A redacted line is never read: its said-as goes with it (review 1, finding 7). if (e.target.checked) setSaidAs(n, ""); }} data-post-screen-redact={n} aria-label={`redact line ${n}`} /> {d && !l.redact && ( x.screen === sc.id && x.n === n)?.takes ?? []).map((x): LineTakeOption => ({ id: x.id, label: `take ${x.n} · ${stepOf(x.take)}${x.take.screenLine?.heard ? " · heard" : " · not heard"}`, current: x.current }))} chosen={sc.lineTakes?.[String(n)]} readings={lineReadings(entry, `screen:${sc.id}:${n}`)} edited={(() => { const said = sc.saidAs?.[String(n)]; const playing = lineReadings(entry, `screen:${sc.id}:${n}`).find((x) => x.picked); return !!said && !!playing && spaced(playing.text) !== spaced(said); })()} onChoose={(id) => setScreen((s) => { const lt = { ...(s.lineTakes ?? {}) }; if (id) lt[String(n)] = id; else delete lt[String(n)]; const { lineTakes: _l, ...rest } = s; void _l; return Object.keys(lt).length ? { ...rest, lineTakes: lt } : rest; }) } /> )}
)}