"use client"; import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { postBenchHref, voicesHref } from "@/lib/hrefs"; import { scriptParts } from "umtool-report-to-video/post-narration"; import { redactText } from "umtool-report-to-video/post-redact"; import { MIC_KEY, ls, useMic } from "./useMic"; import { FilterProgress, TrimWave, sameSpan, type FilterJob, type Preview, type Span, type Who } from "./RecordFilter"; // --------------------------------------------------------------------------- // THE RECORDING SESSION, the client half (RecordPage.tsx): one post at a time, // in the cut's order -- its card and attachment, the script as a teleprompter // in its parts, the current part large -- recorded PART BY PART: // // Space Record / Stop (a count-in first, when it is on) // r Redo the current part // Enter the next part (or, the post done, the next post) // n / p the next / previous post // // Each part is uploaded the moment it is stopped (POST /api/report/post- // narration/record) -- on disk, so a crash loses at most the part being read // and a reload resumes; when the last part is in, the post's recorded take is // made (trimmed, joined with the parts' pauses, levelled to the placeholder's) // and PICKED. // // RECORD -> FILTER -> PREVIEW ("Filter before keeping": on when the post's // character has a stock voice to convert to; lib/report/recordPreview.mjs): the // last part in, the post WAITS -- each part's waveform with its cut marks (the // auto trim's answer, draggable), the pitch the converted read lands on, and // // f Filter: the character's recorded preset as ONE job (clean, // cut, convert), into a scratch dir -- no take -- then the raw // read and the filtered one play at one loudness // Enter Keep: the takes made from the preview's files (nothing is // converted again), picked, awaiting approval; the next post // not recorded yet is opened // Backspace / r Again: the preview dropped, the part recorded again // // The scratch dir (/narration/.record-preview/.operator/) is // removed on Keep, on Again, and when the post or the page is left (unless its // Filter is still running: it is there when you come back). // --------------------------------------------------------------------------- type Part = { n: number; para: number; text: string; start?: number; seconds?: number; file?: string }; export type RecordItem = { id: string; n: number; author: string; handle: string; date: string; text: string; script: string; plays: "recorded" | "placeholder" | "none"; recorded: boolean; placeholder: { file: string; seconds: number; parts: Part[] } | null; media: { src: string; kind: string } | null; avatar: string | null; /** Redacted terms (post-redact.mjs): marked on the teleprompter as words to leave out of the read. */ skip?: Array<{ term: string; wholeWord?: boolean }>; }; /** A part's words with each redacted term struck through: say everything but those. */ function SkipMarked({ text, skip }: { text: string; skip?: RecordItem["skip"] }) { const { spans } = redactText(text, skip ?? []) as { spans: Array<{ start: number; end: number }> }; if (!spans.length) return <>{text}; const out: ReactNode[] = []; let at = 0; for (const sp of spans) { out.push(text.slice(at, sp.start)); out.push( {text.slice(sp.start, sp.end)} , ); at = sp.end; } out.push(text.slice(at)); return <>{out}; } type Session = { count: number | null; parts: Record }; const COUNT_KEY = "umtool.record.count-in"; /** "Filter before keeping", when a person set it ("1" / "0"); unset: on for a post whose character converts. */ const REVIEW_KEY = "umtool.record.filter-first"; const RECORD_API = "/api/report/post-narration/record"; /** The gain that plays a sound of `lufs` at the level of the quieter of the two (an