"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { cn } from "@/lib/utils"; import BlockMenu, { type BlockActions, type MenuAnchor } from "./BlockMenu"; // --------------------------------------------------------------------------- // THE STRIP (umtool redesign L7a): the cut as blocks, in the draft's order -- // the intro, every clip, post item and card, and + at the end. A block's width // is what it plays for (never under a readable minimum); the one on screen is // marked, the selected one is the inspector's. // // click select it (the viewer goes to its start) // ← → the block before / after (the workspace's keys) // Alt+← → move the selected block one place: the timeline's `move`, // the writer the Cut tab's TimelineList uses, one undo point // drag a block onto another: it goes to that place (the same write) // + Add a clip, Add posts, insert a card (the workspace's menu) // right-click, ⋯, Shift+F10 or the context-menu key // its menu (BlockMenu, lane L8b): the strip's own writers and // every registry verb for its kind, each the ⌘K form filled in // // The intro is not a timeline entry (render.intro: the build prepends it), so // it is selected and played like any block but never moved. // --------------------------------------------------------------------------- export type Block = { id: string; type: string; heading: string; /** Seconds it plays for (the plan's share), 0 when it is skipped. */ seconds: number; /** Its index in the manifest's timeline (null: the intro, which is not in it). */ at: number | null; rejected?: string | null; missing?: boolean; /** Its id is in the timeline more than once and the view did not say which it is: no index, nothing moves it. */ twin?: boolean; }; const TYPE_WORD: Record = { clip: "clip", post: "post", teaser: "card", card: "card", intro: "intro", video: "video" }; const TINT: Record = { clip: "border-[var(--color-line)] bg-[var(--color-panel)]", post: "border-[color-mix(in_srgb,var(--color-meter)_45%,transparent)] bg-[color-mix(in_srgb,var(--color-meter)_10%,var(--color-panel))]", teaser: "border-dashed border-[var(--color-line)] bg-[var(--color-panel-2)]", card: "border-dashed border-[var(--color-line)] bg-[var(--color-panel-2)]", intro: "border-dashed border-[var(--color-line)] bg-[var(--color-panel-2)]", video: "border-[var(--color-line)] bg-[var(--color-panel-2)]", }; /** Pixels per second of the strip, and the narrowest a block is drawn. */ const PX_PER_S = 9; const MIN_W = 76; const MAX_W = 260; export default function BlockStrip({ blocks, selected, playing, busy, onSelect, onMove, plus, marks = null, menu, canMove = true, }: { blocks: Block[]; selected: string | null; /** The block on screen now. */ playing: string | null; busy: boolean; onSelect: (id: string) => void; /** Move block `id` to timeline index `to`. */ onMove: (id: string, at: number, to: number) => void; /** The + at the end: what it opens (none outside Edit). */ plus: React.ReactNode; /** The review pass's answers (L7b): a block kept or to fix is marked. */ marks?: Map | null; /** What a block's menu does (none: no menus). */ menu?: BlockActions; /** Whether a block drags to a new place: only in Edit on the draft (the review pass and a version move nothing). */ canMove?: boolean; }) { const ref = useRef(null); const [drag, setDrag] = useState(null); const [over, setOver] = useState(null); // The open menu: its block -- by its place in the strip, two blocks may share an id -- and where it opens. A ref // too, so the key's menu and the contextmenu event the browser may send after it open one menu. const [open, setOpen] = useState<{ id: string; i: number; anchor: MenuAnchor; seq: number } | null>(null); const seq = useRef(0); const openRef = useRef<{ id: string; i: number } | null>(null); const blockEl = (i: number) => ref.current?.querySelector(`[data-ws-block-i="${i}"]`) as HTMLElement | null; const showMenu = (id: string, i: number, anchor: MenuAnchor | null) => { if (!menu || openRef.current?.i === i) return; const r = blockEl(i)?.getBoundingClientRect(); const at: MenuAnchor = anchor ?? { rect: { left: r?.left ?? 8, top: r?.top ?? 8, bottom: r?.bottom ?? 8 } }; openRef.current = { id, i }; seq.current += 1; setOpen({ id, i, anchor: at, seq: seq.current }); }; const closeMenu = useCallback((refocus: boolean) => { const o = openRef.current; openRef.current = null; setOpen(null); if (refocus && o) (ref.current?.querySelector(`[data-ws-block-i="${o.i}"]`) as HTMLElement | null)?.focus(); }, []); // Where its block is now: its place, or -- the strip re-read under it (an undo landing, another writer) -- the // one block with its id. A block that left, or whose id is now on two blocks, takes the menu with it. const openAt = (() => { if (!open) return null; if (blocks[open.i]?.id === open.id) return open.i; const hits = blocks.flatMap((b, j) => (b.id === open.id ? [j] : [])); return hits.length === 1 ? hits[0] : null; })(); const found = openAt !== null && menu ? menu.info(openAt) : null; const openInfo = found && found.id === open?.id ? found : null; useEffect(() => { if (open && !openInfo) closeMenu(false); else if (open && openAt !== null && openAt !== open.i) { openRef.current = { id: open.id, i: openAt }; setOpen({ ...open, i: openAt }); } }, [open, openInfo, openAt, closeMenu]); // The selected block kept in view (the keys walk past the strip's edge). useEffect(() => { if (!selected) return; const el = ref.current?.querySelector(`[data-ws-block="${CSS.escape(selected)}"]`) as HTMLElement | null; el?.scrollIntoView({ block: "nearest", inline: "nearest" }); }, [selected]); const byId = new Map(blocks.map((b) => [b.id, b])); return (
{blocks.map((b, i) => { const w = Math.round(Math.min(MAX_W, Math.max(MIN_W, b.seconds * PX_PER_S))); const on = b.id === selected; const movable = canMove && b.at !== null && !busy; const menuOn = open?.i === i; return (
{menu && ( )}
); })}
{plus}
{open && openInfo && menu && }
); }