"use client"; import { useMemo, useRef, useState } from "react"; import Waveform from "@/components/Waveform"; import { POST_MEDIA_MIN_SPAN } from "umtool-report-to-video/post-item"; import { projectFileMedia } from "./bench/benchMedia"; import { usePeaks } from "./bench/usePeaks"; import type { PostPlan } from "./review/PostItemPlayer"; // --------------------------------------------------------------------------- // ONE ATTACHMENT'S LANE on the post bench (PostNarrationPanel's lanes): a video's // or a GIF's whole length as a waveform -- its sound's envelope (the peaks route, // "use a file"), or a flat line for a GIF or a silent video -- with its in and out // as the waveform's two handles, the part the item plays dashed, and the playhead. // // The handles move `media[i].start` / `.end` as the number fields do (the bench's // draft, saved by its Save), never closer than // POST_MEDIA_MIN_SPAN and never past the file. While a handle moves, the bench's // clock follows the edge, so the player shows its frame. Keys, with the lane // focused: [ ] the in, , . the out (0.05 s; Shift 0.5 s, the clip bench's), and // ← → the edge last moved (0.1 s; Shift 1 s). // --------------------------------------------------------------------------- type Media = Record & { src: string; kind: string; seconds?: number; start?: number; end?: number; audio?: boolean }; type Edge = "start" | "end"; type Step = PostPlan["steps"][number]; const r2 = (v: number) => Math.round(v * 100) / 100; /** The trim `{start, end}` an edge at `t` makes, inside the file and at least POST_MEDIA_MIN_SPAN long. */ export function trimEdge(sel: { from: number; to: number }, edge: Edge, t: number, len: number) { const span = Math.min(POST_MEDIA_MIN_SPAN, len); if (edge === "start") return { from: r2(Math.max(0, Math.min(t, sel.to - span))), to: sel.to }; return { from: sel.from, to: r2(Math.min(len, Math.max(t, sel.from + span))) }; } export default function PostAttachmentLane({ project, media: m, index: i, step, s, setS, window: chosen = null, onTrim, children, }: { project: string; media: Media; index: number; /** Its step in the item's plan: where it comes in, the source seconds it plays. */ step: Step | null; s: number; setS: (s: number) => void; /** The narration's chosen window (posts fit), drawn as the reference. */ window?: { start: number; end: number } | null; /** An edge moved: the attachment's start / end (undefined: the file's own). */ onTrim: (i: number, patch: { start?: number; end?: number }) => void; /** The lane's own buttons (Re-pick best …), beside its title. */ children?: React.ReactNode; }) { const len = Number(m.seconds ?? 0) || 1; const sel = { from: Number(m.start ?? 0), to: Number(m.end ?? len) }; const played = step ? { from: step.srcFrom ?? 0, to: (step.srcFrom ?? 0) + step.seconds } : sel; // Its sound's envelope: none for a GIF or a video without sound (the bar still draws and drags). const sounded = m.kind === "video" && m.audio !== false; const fm = useMemo(() => projectFileMedia({ project, path: m.src, seconds: len }), [project, m.src, len]); const peaks = usePeaks(sounded ? fm : { ...fm, peaksUrl: null }); const [last, setLast] = useState("end"); const [showWhole, setShowWhole] = useState(false); const whole = useRef(null); const apply = (next: { from: number; to: number }, edge: Edge) => { onTrim(i, { start: next.from > 0.02 ? next.from : undefined, end: next.to < len - 0.02 ? next.to : undefined }); setLast(edge); // The player shows the edge's frame: the bench's clock at it, inside the attachment's step. const t = edge === "start" ? next.from : next.to; if (step) setS(Math.max(0, step.at + Math.max(0, Math.min(t - next.from, Math.max(0, next.to - next.from - 0.05))))); const v = whole.current; if (v) { try { v.currentTime = t; } catch { /* not seekable yet */ } } }; const nudge = (edge: Edge, by: number) => apply(trimEdge(sel, edge, (edge === "start" ? sel.from : sel.to) + by, len), edge); const playhead = step && s >= step.at ? (step.srcFrom ?? 0) + Math.min(step.seconds, s - step.at) : null; return (
{ if (e.metaKey || e.ctrlKey || e.altKey) return; const fine = e.shiftKey ? 0.5 : 0.05; const by: Record = { "[": ["start", -fine], "{": ["start", -fine], "]": ["start", fine], "}": ["start", fine], ",": ["end", -fine], "<": ["end", -fine], ".": ["end", fine], ">": ["end", fine], ArrowLeft: [last, e.shiftKey ? -1 : -0.1], ArrowRight: [last, e.shiftKey ? 1 : 0.1], }; const hit = by[e.key]; if (!hit) return; e.preventDefault(); e.stopPropagation(); nudge(hit[0], hit[1]); }} >
attachment {i + 1} · {m.kind} · {len.toFixed(1)} s · in {sel.from.toFixed(2)} · out {sel.to.toFixed(2)} ·{" "} plays {played.from.toFixed(1)}–{played.to.toFixed(1)} s {!sounded ? " · no sound" : ""} {children}
{ const e2: Edge = edge === "start" ? "start" : "end"; apply(trimEdge(sel, e2, e2 === "start" ? next.from : next.to, len), e2); }} onReachEdge={() => {}} height={72} />

drag a handle (or click near it) · focused: [ ] the in, , . the out, ← → the last moved (Shift: coarser) · the dashed lines: what the item plays

{showWhole && ( // eslint-disable-next-line jsx-a11y/media-has-caption
); }