"use client"; import { useEffect, useRef, useState } from "react"; // A TEASER on Play's stage (lane leaflit-labels): the card the build draws -- // its own page (chrome-teaser.mjs, composed for preview by POST // /api/report/chrome/teasers) -- full frame, seeked to `s`, the seconds into // the card. The page says `teaser:ready` once its words are fitted; nothing is // sent before it. Laid where a placeholder would be: under the deck's page, so // a card that counts keeps the deck (and the count stepping at its mark) over it. export type TeaserPage = { id: string; seconds: number; width: number; height: number; src: string }; export default function TeaserCardPlayer({ page, s }: { page: TeaserPage; s: number }) { const box = useRef(null); const el = 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 === "teaser:ready") setReadySrc(src); }; window.addEventListener("message", onMsg); return () => window.removeEventListener("message", onMsg); }, [src]); const live = readySrc === src; // Clamped into the card: a hard cut shows its middle, never past its last frame. 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]); const scale = width > 0 ? width / page.width : 0; return (