"use client"; import { useEffect, useRef, useState } from "react"; // THE INTRO on a stage (lane intro-play; operator, 8 Nov 2026: "more first-class intro support (view in play the // cut)"): the page the build renders (report-to-video intro.mjs introPageHtml, composed for preview by POST // /api/report/chrome/intro) full frame, seeked to `s`, the seconds into the intro -- TeaserCardPlayer's handshake: // the page says `intro:ready` once its words are fitted, and nothing is sent before it. Its SOUND (the boot build and // the chime the mark lands on: the stand-in, or render.intro.sound) plays beside it while `playing` and `sound` are // on, slaved to `s` as a video entry's element is (re-seeked past a drift). Used by Play, Present and the Cut tab's // intro editor. export type IntroPage = { id: string; style: string; seconds: number; width: number; height: number; src: string; /** The intro's sound (GET /api/report/chrome/intro?sound=1…), or null: a silent intro. */ sound: string | null; title?: string; lines?: string[]; }; /** How far the sound may drift from the clock before it is re-seeked, seconds. */ const DRIFT = 0.25; export default function IntroCardPlayer({ page, s, playing = false, sound = true, speed = 1, testid = "play-intro", }: { page: IntroPage; s: number; /** The clock is running (and inside the intro). */ playing?: boolean; /** Its sound plays while playing (Present's "sound" setting; Play's always). */ sound?: boolean; speed?: number; testid?: string; }) { const box = useRef(null); const el = useRef(null); const audio = useRef(null); const [width, setWidth] = useState(0); const [readySrc, setReadySrc] = useState(null); const src = `${page.src}${page.src.includes("?") ? "&" : "?"}preview=1`; useEffect(() => { const node = box.current; if (!node) return; const ro = new ResizeObserver(([e]) => setWidth(e.contentRect.width)); ro.observe(node); return () => ro.disconnect(); }, []); useEffect(() => { const onMsg = (e: MessageEvent) => { if (e.source !== el.current?.contentWindow || e.origin !== window.location.origin) return; if ((e.data as { type?: string } | null)?.type === "intro:ready") setReadySrc(src); }; window.addEventListener("message", onMsg); return () => window.removeEventListener("message", onMsg); }, [src]); const live = readySrc === src; // Clamped into the intro: past its end it holds its last frame (the ground). const at = Math.max(0, Math.min(page.seconds, s)); useEffect(() => { if (live) el.current?.contentWindow?.postMessage({ type: "deck:seek", t: at }, window.location.origin); }, [live, at]); // The sound, slaved to the clock: playing only while the clock runs inside the intro. const run = playing && sound && !!page.sound && s >= 0 && s < page.seconds; useEffect(() => { const a = audio.current; if (!a) return; const want = Number.isFinite(speed) && speed > 0 ? speed : 1; if (Math.abs(a.playbackRate - want) > 1e-3) a.playbackRate = want; if (run) { if (Math.abs(a.currentTime - at) > DRIFT * Math.max(1, want)) { try { a.currentTime = at; } catch { // not seekable yet: the next tick tries again } } if (a.paused) void a.play().catch(() => {}); } else if (!a.paused) a.pause(); }, [run, at, speed]); // Gone from the stage (another item, another slide, the editor closed): its sound stops with it. useEffect(() => { const a = audio.current; return () => { if (a && !a.paused) a.pause(); }; }, [page.sound]); const scale = width > 0 ? width / page.width : 0; return (