"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { comboBadgeHtml, comboFrame, comboPlace, comboStart, comboTransform } from "umtool-report-to-video/post-combo"; import { postLayout, stepBoxes } from "umtool-report-to-video/post-item"; import type { PostPlan } from "./PostItemPlayer"; // --------------------------------------------------------------------------- // THE COMBO BADGE, PLAYED (report-to-video/post-combo.mjs; knobs.md, "The combo // counter"): over a post item that repeats an earlier item's attachment, // "COMBO ×" pops onto that attachment's corner as it starts -- the build's // own markup (comboBadgeHtml), moved by the build's own motion (comboFrame) on // the item's clock, in the box's px scaled to the player. Laid beside the // PostItemPlayer (Play the cut, the post bench), never inside it. Its sound, // when the cut asks for one, plays as it pops while playing. // --------------------------------------------------------------------------- export type ComboInfo = { count: number; media: number; show: boolean; knobs: Record & { sfx?: string }; group?: string; tier?: string | null; members?: Array<{ id: string; i: number; n: number; post: string | null; date: string | null; tier: string | null }>; }; type Box = { width: number; height: number }; export default function ComboBadge({ project, entry, plan, combo, box, s, playing, }: { project: string; entry: Record & { id: string }; plan: PostPlan; combo: ComboInfo | null | undefined; box: Box; /** The item's own second. */ s: number; playing: boolean; }) { const wrap = useRef(null); const [scale, setScale] = useState(0); useEffect(() => { const node = wrap.current; if (!node) return; const ro = new ResizeObserver(([e]) => setScale(e.contentRect.width / box.width)); ro.observe(node); return () => ro.disconnect(); }, [box.width]); const geo = useMemo(() => { if (!combo?.show) return null; const at = comboStart(plan, combo.media); const step = plan.steps.find((st) => st.items.some((m) => m.i === combo.media)); const layout = postLayout(entry, box); if (at === null || !step || !layout.slot) return null; const boxes = stepBoxes(step, layout.slot) as Array<{ x: number; y: number; width: number; height: number }>; const mb = boxes[step.items.findIndex((m) => m.i === combo.media)] ?? boxes[0]; const place = comboPlace(combo.count, combo.knobs, mb, box); const font = `/api/report/post-item?project=${encodeURIComponent(project)}&font=bold`; return { at, place, html: comboBadgeHtml(combo.count, combo.knobs, place.size, { font }) }; }, [combo, plan, entry, box, project]); const frame = geo ? comboFrame(s - geo.at, { mirror: geo.place.mirror }) : null; // Its sound, as it pops while playing (not when scrubbed onto). const sfx = typeof combo?.knobs?.sfx === "string" && combo.knobs.sfx !== "off" ? combo.knobs.sfx : null; const audio = useRef(null); const up = !!frame; const was = useRef(false); useEffect(() => { const a = audio.current; if (up && !was.current && playing && a && geo && s - geo.at < 0.3) { a.currentTime = 0; void a.play().catch(() => {}); } was.current = up; }, [up, playing, geo, s]); if (!combo) return null; const size = geo?.place.size; return (
{sfx && ( // eslint-disable-next-line jsx-a11y/media-has-caption