From b064e40e13810a73ddf27c4e467fd8a5ba33400d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 17 Nov 2026 12:37:23 +0000 Subject: [PATCH] fix(studio): adding a track no longer remounts every clip on the timeline (#8068) * test(studio): adding a track keeps every existing timeline clip mounted * fix(studio): adding a track no longer remounts every clip on the timeline --- .../components/Timeline.newTrack.test.tsx | 106 ++++++++++++++++++ .../src/player/components/TimelineLanes.tsx | 6 +- .../player/components/timelineRowKeys.test.ts | 28 +++++ .../src/player/components/timelineRowKeys.ts | 27 +++++ 4 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 packages/studio/src/player/components/Timeline.newTrack.test.tsx create mode 100644 packages/studio/src/player/components/timelineRowKeys.test.ts create mode 100644 packages/studio/src/player/components/timelineRowKeys.ts diff --git a/packages/studio/src/player/components/Timeline.newTrack.test.tsx b/packages/studio/src/player/components/Timeline.newTrack.test.tsx new file mode 100644 index 0000000000..735cd858ba --- /dev/null +++ b/packages/studio/src/player/components/Timeline.newTrack.test.tsx @@ -0,0 +1,106 @@ +// @vitest-environment happy-dom + +import React, { act } from "react"; +import { createRoot } from "react-dom/client"; +import { afterAll, beforeAll, expect, it } from "vitest"; +import type { TimelineElement } from "../store/playerStore"; +import { normalizeToZones } from "./timelineZones"; + +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + +class MockResizeObserver { + constructor(private readonly callback: ResizeObserverCallback) {} + observe(target: Element) { + this.callback( + [{ target, borderBoxSize: [{ inlineSize: 900, blockSize: 400 }] } as never], + this as unknown as ResizeObserver, + ); + } + unobserve() {} + disconnect() {} +} + +const originalResizeObserver = globalThis.ResizeObserver; +const sizes = ["clientWidth", "clientHeight"] as const; +const originals = sizes.map((name) => Object.getOwnPropertyDescriptor(HTMLElement.prototype, name)); + +beforeAll(() => { + globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver; + Object.defineProperty(HTMLElement.prototype, "clientWidth", { + configurable: true, + get: () => 900, + }); + Object.defineProperty(HTMLElement.prototype, "clientHeight", { + configurable: true, + get: () => 400, + }); +}); + +afterAll(() => { + globalThis.ResizeObserver = originalResizeObserver; + sizes.forEach((name, i) => { + const original = originals[i]; + if (original) Object.defineProperty(HTMLElement.prototype, name, original); + }); + document.body.innerHTML = ""; +}); + +const clip = (id: string, track: number, start = 0): TimelineElement => ({ + id, + label: id, + tag: "div", + start, + duration: 5, + track, +}); + +async function settleUntil(ready: () => boolean) { + for (let i = 0; i < 60 && !ready(); i++) + await act(async () => new Promise((done) => requestAnimationFrame(() => done()))); +} + +/** Mounts the timeline on `first`, then shows `next`; returns which of `kept` got a new DOM node. */ +async function remountedAfter(first: TimelineElement[], next: TimelineElement[], kept: string[]) { + const [{ Timeline }, { usePlayerStore }] = await Promise.all([ + import("./Timeline"), + import("../store/playerStore"), + ]); + usePlayerStore.setState({ duration: 60, timelineReady: true, elements: normalizeToZones(first) }); + const host = document.body.appendChild(document.createElement("div")); + const root = createRoot(host); + const node = (id: string) => host.querySelector(`[data-el-id="${id}"]`); + try { + await act(async () => root.render(React.createElement(Timeline))); + await settleUntil(() => kept.every((id) => node(id))); + const before = new Map(kept.map((id) => [id, node(id)])); + expect([...before.values()].every(Boolean)).toBe(true); + await act(async () => usePlayerStore.setState({ elements: normalizeToZones(next) })); + await settleUntil(() => next.every(({ id }) => node(id))); + return kept.filter((id) => node(id) !== before.get(id)); + } finally { + act(() => root.unmount()); + usePlayerStore.getState().reset(); + } +} + +// An agent edit that adds a track re-zones every lane below it (normalizeToZones), shifting each clip's display +// track by one. On a long film that remounted the whole timeline in one frame. The 30 s budget matches +// Timeline.virtualization.test. +it( + "adding a track above the others keeps every existing clip mounted", + { timeout: 30_000 }, + async () => { + const ids = ["a", "b", "c", "d", "e"]; + const authored = ids.map((id, i) => clip(id, i + 1)); + expect(await remountedAfter(authored, [clip("new", 0), ...authored], ids)).toEqual([]); + }, +); + +it( + "deleting a track's first clip keeps the rest of the timeline mounted", + { timeout: 30_000 }, + async () => { + const first = [clip("x1", 0, 0), clip("x2", 0, 10), clip("y", 1), clip("z", 2)]; + expect(await remountedAfter(first, first.slice(1), ["x2", "y", "z"])).toEqual([]); + }, +); diff --git a/packages/studio/src/player/components/TimelineLanes.tsx b/packages/studio/src/player/components/TimelineLanes.tsx index a02fa648c1..3ede05dca1 100644 --- a/packages/studio/src/player/components/TimelineLanes.tsx +++ b/packages/studio/src/player/components/TimelineLanes.tsx @@ -31,6 +31,7 @@ import { TimelineTrackRow } from "./TimelineTrackRow"; import { isTimelineClipActive } from "./useTimelineActiveClips"; import { queryTimelineClipIndex } from "../lib/timelineClipIndex"; import { getTimelineElementIdentity } from "../lib/timelineElementHelpers"; +import { timelineRows } from "./timelineRowKeys"; import { timelineClipFocusId } from "./timelineNavigationIdentity"; import { useTimelineKeyboardActor } from "./useTimelineKeyboardActor"; import { TimelineTransitionOverlays } from "./TimelineTransitionOverlays"; @@ -145,6 +146,7 @@ export function TimelineLanes({ onDrillDown, }); const clipActions = useRef(null!); + const rows = timelineRows(tracks); clipActions.current = { gestures: { pps, @@ -218,7 +220,7 @@ export function TimelineLanes({ const logicalRow = trackLogicalRows[0]; if (!logicalRow) return null; const rowHeight = rowGeometry.getRowHeight(row); - const els = tracks.find(([t]) => t === trackNum)?.[1] ?? []; + const els = rows.clips(trackNum); const renderElements = rowsVirtualized ? queryTimelineClipIndex( clipIndex, @@ -302,7 +304,7 @@ export function TimelineLanes({ : undefined; return ( ({ + id, + tag, + start: 0, + duration: 1, + track, + ...(authoredTrack === undefined ? {} : { authoredTrack }), +}); + +it("keys rows by zone and authored track, numbering repeats and empty lanes apart, and finds each row's clips", () => { + const rows = timelineRows([ + [0, [el("a", 0, 3)]], + [1, [el("b", 1, 3)]], + [2, [el("c", 2, 3, "audio")]], + [3, []], + ]); + expect([0, 1, 2, 3, 9].map(rows.reactKey)).toEqual([ + "visual:3", + "visual:3#1", + "audio:3", + "lane:3", + "lane:9", + ]); +}); diff --git a/packages/studio/src/player/components/timelineRowKeys.ts b/packages/studio/src/player/components/timelineRowKeys.ts new file mode 100644 index 0000000000..9cb92b771d --- /dev/null +++ b/packages/studio/src/player/components/timelineRowKeys.ts @@ -0,0 +1,27 @@ +import type { TimelineElement } from "../store/playerStore"; +import { resolveElementTrack } from "../../utils/studioHelpers"; +import { classifyZone } from "./timelineZones"; + +type Tracks = readonly (readonly [number, readonly TimelineElement[]])[]; +const NO_CLIPS: readonly TimelineElement[] = []; + +/** Each row's clips, and its React key: the clips' zone and authored track, which adding a track does not renumber + * as it does display lanes. Rows sharing one (overlap sub-lanes, expanded children) take a #n suffix in row order. */ +export function timelineRows(tracks: Tracks) { + const seen = new Map(); + const keys = new Map(); + const clips = new Map(); + for (const [track, els] of tracks) { + const base = els[0] + ? `${classifyZone(els[0])}:${resolveElementTrack(els[0])}` + : `lane:${track}`; + const repeat = seen.get(base) ?? 0; + seen.set(base, repeat + 1); + keys.set(track, repeat ? `${base}#${repeat}` : base); + clips.set(track, els); + } + return { + reactKey: (track: number) => keys.get(track) ?? `lane:${track}`, + clips: (track: number) => clips.get(track) ?? NO_CLIPS, + }; +}