Skip to content

Write HTML. Render video. Built for agents.

fix(studio): back to the master after a nested drill restores the master playhead (#4678)

Miguel Ángel73d26a5187c2
diff --git a/packages/studio/src/components/nle/useCompositionStack.test.tsx b/packages/studio/src/components/nle/useCompositionStack.test.tsx
index d40a5abaa8..1d4b29bd51 100644
--- a/packages/studio/src/components/nle/useCompositionStack.test.tsx
+++ b/packages/studio/src/components/nle/useCompositionStack.test.tsx
@@ -3,6 +3,7 @@ import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it } from "vitest";
import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness";
+import { liveTime, usePlayerStore } from "../../player/store/playerStore";
import { useCompositionStack } from "./useCompositionStack";
installReactActEnvironment();
@@ -111,3 +112,128 @@ describe("useCompositionStack — activating a composition by path", () => {
act(() => root.unmount());
});
});
+
+describe("useCompositionStack — back to the master", () => {
+ afterEach(() => {
+ document.body.innerHTML = "";
+ usePlayerStore.getState().setCurrentTime(0);
+ usePlayerStore.getState().setIsPlaying(false);
+ });
+
+ const INTRO = { id: "intro", compositionSrc: "compositions/intro.html" };
+ const LOGO = { id: "logo", compositionSrc: "compositions/logo.html" };
+ const seek = (time: number) => act(() => usePlayerStore.getState().setCurrentTime(time));
+ const time = () => usePlayerStore.getState().currentTime;
+
+ async function mount(props: { projectId?: string; activeCompositionPath?: string | null } = {}) {
+ const host = document.createElement("div");
+ document.body.append(host);
+ const root = createRoot(host);
+ const seen = {} as { stack: ReturnType<typeof useCompositionStack> };
+ function Harness(p: { projectId: string; activeCompositionPath: string | null }) {
+ seen.stack = useCompositionStack(p);
+ return null;
+ }
+ const render = async (next: typeof props) => {
+ props = { ...props, ...next };
+ await act(async () => {
+ root.render(
+ <Harness
+ projectId={props.projectId ?? "p"}
+ activeCompositionPath={props.activeCompositionPath ?? null}
+ />,
+ );
+ });
+ };
+ await render({});
+ const run = async (step: (stack: ReturnType<typeof useCompositionStack>) => void) => {
+ await act(async () => step(seen.stack));
+ };
+ return { seen, render, run, unmount: () => act(() => root.unmount()) };
+ }
+
+ it("restores the master playhead from the breadcrumb after a two-level drill", async () => {
+ const { seen, run, unmount } = await mount();
+ seek(12);
+ await run((s) => s.handleDrillDown(INTRO));
+ seek(3);
+ await run((s) => s.handleDrillDown(LOGO));
+ expect(seen.stack.compositionStack.map((level) => level.label)).toEqual([
+ "Master",
+ "intro",
+ "logo",
+ ]);
+ await run((s) => s.handleNavigateComposition(0));
+ expect(time()).toBe(12);
+ unmount();
+ });
+
+ it("restores the master playhead when Escape or a timeline double-click goes back", async () => {
+ const { run, unmount } = await mount();
+ seek(12);
+ await run((s) => s.handleDrillDown(INTRO));
+ seek(3);
+ await run((s) => s.handleDrillDown(LOGO));
+ seek(1);
+ await run((s) => s.updateCompositionStack((prev) => prev.slice(0, -1)));
+ expect(time()).toBe(1);
+ await run((s) => s.updateCompositionStack((prev) => prev.slice(0, -1)));
+ expect(time()).toBe(12);
+ unmount();
+ });
+
+ it("restores the live master playhead when the drill happens during playback", async () => {
+ const { run, unmount } = await mount();
+ seek(4);
+ act(() => usePlayerStore.getState().setIsPlaying(true));
+ liveTime.notify(9);
+ await run((s) => s.handleDrillDown(INTRO));
+ act(() => usePlayerStore.getState().setIsPlaying(false));
+ seek(2);
+ await run((s) => s.handleNavigateComposition(0));
+ expect(time()).toBe(9);
+ unmount();
+ });
+
+ it("saves the master playhead at the first of two drills in one batch", async () => {
+ const { run, unmount } = await mount();
+ seek(12);
+ await run((s) => {
+ s.handleDrillDown(INTRO);
+ usePlayerStore.getState().setCurrentTime(7);
+ s.handleDrillDown(LOGO);
+ });
+ await run((s) => s.handleNavigateComposition(0));
+ expect(time()).toBe(12);
+ unmount();
+ });
+
+ it("restores a master playhead of exactly 0", async () => {
+ const { run, unmount } = await mount();
+ await run((s) => s.handleDrillDown(INTRO));
+ seek(3);
+ await run((s) => s.handleNavigateComposition(0));
+ expect(time()).toBe(0);
+ unmount();
+ });
+
+ it("restores the master playhead when the Comps panel goes there and back", async () => {
+ const { render, unmount } = await mount();
+ seek(30);
+ await render({ activeCompositionPath: "compositions/intro.html" });
+ seek(4);
+ await render({ activeCompositionPath: "index.html" });
+ expect(time()).toBe(30);
+ unmount();
+ });
+
+ it("does not carry one project's master playhead into another", async () => {
+ const { run, render, unmount } = await mount({ projectId: "a" });
+ seek(12);
+ await run((s) => s.handleDrillDown(INTRO));
+ seek(3);
+ await render({ projectId: "b" });
+ expect(time()).toBe(3);
+ unmount();
+ });
+});
diff --git a/packages/studio/src/components/nle/useCompositionStack.ts b/packages/studio/src/components/nle/useCompositionStack.ts
index 44a0164acf..da758733d8 100644
--- a/packages/studio/src/components/nle/useCompositionStack.ts
+++ b/packages/studio/src/components/nle/useCompositionStack.ts
@@ -1,7 +1,7 @@
import { buildProjectApiPath } from "../../utils/projectRouting";
// Composition drill-down stack management for NLEContext/EditorShell
import { useState, useCallback, useRef, useEffect } from "react";
-import { usePlayerStore } from "../../player/store/playerStore";
+import { liveTime, usePlayerStore } from "../../player/store/playerStore";
import type { CompositionLevel } from "./CompositionBreadcrumb";
import { encodePreviewPath } from "../../player/components/thumbnailUtils";
@@ -37,16 +37,30 @@ export function useCompositionStack({
const onCompositionChangeRef = useRef(onCompositionChange);
onCompositionChangeRef.current = onCompositionChange;
+ const masterSeekRef = useRef(0);
+ const masterSeekProjectRef = useRef<string | null>(null);
+ const projectIdRef = useRef(projectId);
+ projectIdRef.current = projectId;
+ const stackRef = useRef(compositionStack);
+
const updateCompositionStack: typeof setCompositionStack = useCallback((action) => {
- setCompositionStack((prev) => {
- const next = typeof action === "function" ? action(prev) : action;
- const id = next[next.length - 1]?.id;
- queueMicrotask(() => onCompositionChangeRef.current?.(id === "master" ? null : id));
- return next;
- });
+ const prev = stackRef.current;
+ const next = typeof action === "function" ? action(prev) : action;
+ stackRef.current = next;
+ const player = usePlayerStore.getState();
+ if (prev.length === 1 && next.length > 1) {
+ masterSeekRef.current = player.isPlaying ? liveTime.latest() : player.currentTime;
+ masterSeekProjectRef.current = projectIdRef.current;
+ } else if (next.length === 1 && prev.length > 1) {
+ if (masterSeekProjectRef.current === projectIdRef.current) {
+ player.setCurrentTime(masterSeekRef.current);
+ }
+ }
+ setCompositionStack(next);
+ const id = next[next.length - 1]?.id;
+ queueMicrotask(() => onCompositionChangeRef.current?.(id === "master" ? null : id));
}, []);
- const masterSeekRef = useRef(0);
const [compIdToSrc, setCompIdToSrc] = useState<Map<string, string>>(new Map());
const compIdToSrcRef = useRef(compIdToSrc);
@@ -54,9 +68,6 @@ export function useCompositionStack({
const handleNavigateComposition = useCallback(
(index: number) => {
- if (index === 0 && masterSeekRef.current > 0) {
- usePlayerStore.getState().setCurrentTime(masterSeekRef.current);
- }
usePlayerStore.getState().setElements([]);
updateCompositionStack((prev) => prev.slice(0, index + 1));
},
@@ -67,7 +78,6 @@ export function useCompositionStack({
const handleDrillDown = useCallback(
(element: { id: string; compositionSrc?: string }) => {
if (!element.compositionSrc) return;
- masterSeekRef.current = usePlayerStore.getState().currentTime;
const compId = element.id;
let resolvedPath = compIdToSrcRef.current.get(compId);
diff --git a/packages/studio/src/player/components/Timeline.playheadRemount.test.tsx b/packages/studio/src/player/components/Timeline.playheadRemount.test.tsx
new file mode 100644
index 0000000000..a30d873a4c
--- /dev/null
+++ b/packages/studio/src/player/components/Timeline.playheadRemount.test.tsx
@@ -0,0 +1,65 @@
+// @vitest-environment happy-dom
+
+import React, { act } from "react";
+import { createRoot } from "react-dom/client";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { Timeline } from "./Timeline";
+import { liveTime, usePlayerStore } from "../store/playerStore";
+
+(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 } as ResizeObserverEntry], this as unknown as ResizeObserver);
+ }
+ unobserve() {}
+ disconnect() {}
+}
+
+const originalResizeObserver = globalThis.ResizeObserver;
+const originalClientWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "clientWidth");
+
+beforeAll(() => {
+ globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
+ Object.defineProperty(HTMLElement.prototype, "clientWidth", {
+ configurable: true,
+ get: () => 900,
+ });
+});
+
+afterAll(() => {
+ globalThis.ResizeObserver = originalResizeObserver;
+ if (originalClientWidth)
+ Object.defineProperty(HTMLElement.prototype, "clientWidth", originalClientWidth);
+ document.body.innerHTML = "";
+});
+
+const clips = [{ id: "intro", tag: "div", start: 2, duration: 10, track: 0 }];
+const needle = (host: HTMLElement) =>
+ host.querySelector<HTMLElement>("[data-timeline-playhead-layer] > div")?.style.transform;
+
+describe("Timeline playhead across a composition switch", () => {
+ it("places the remounted playhead at the restored time", async () => {
+ usePlayerStore.setState({ duration: 20, currentTime: 0, timelineReady: true, elements: clips });
+ const host = document.createElement("div");
+ document.body.append(host);
+ const root = createRoot(host);
+ await act(async () => root.render(<Timeline />));
+ const atZero = needle(host);
+ await act(async () => usePlayerStore.getState().setCurrentTime(4));
+ const atFour = needle(host);
+ expect(atFour).not.toBe(atZero);
+
+ await act(async () => usePlayerStore.setState({ elements: [], timelineReady: false }));
+ await act(async () => usePlayerStore.getState().setCurrentTime(1.5));
+ await act(async () => {
+ usePlayerStore.getState().setCurrentTime(4);
+ liveTime.notify(4);
+ });
+ await act(async () => usePlayerStore.setState({ elements: clips, timelineReady: true }));
+
+ expect(needle(host)).toBe(atFour);
+ act(() => root.unmount());
+ });
+});
diff --git a/packages/studio/src/player/components/useTimelinePlayhead.ts b/packages/studio/src/player/components/useTimelinePlayhead.ts
index 7f61430fe3..8f8bbdb087 100644
--- a/packages/studio/src/player/components/useTimelinePlayhead.ts
+++ b/packages/studio/src/player/components/useTimelinePlayhead.ts
@@ -104,7 +104,7 @@ export function useTimelinePlayhead({
useEffect(() => {
syncPlayheadPosition(currentTime);
- }, [currentTime, pps, syncPlayheadPosition]);
+ }, [currentTime, pps, syncPlayheadPosition, timelineReady, elementsLength]);
useLayoutEffect(() => {
const scroll = scrollRef.current;
diff --git a/packages/studio/src/player/store/liveTime.ts b/packages/studio/src/player/store/liveTime.ts
index 94bf6bf17d..ed2050efe9 100644
--- a/packages/studio/src/player/store/liveTime.ts
+++ b/packages/studio/src/player/store/liveTime.ts
@@ -1,9 +1,14 @@
// ponytail: Playback RAF updates the playhead/time display without per-frame React renders.
type TimeListener = (time: number) => void;
const timeListeners = new Set<TimeListener>();
+let latestTime = 0;
export const liveTime = {
- notify: (time: number) => timeListeners.forEach((listener) => listener(time)),
+ notify: (time: number) => {
+ latestTime = time;
+ timeListeners.forEach((listener) => listener(time));
+ },
+ latest: () => latestTime,
subscribe: (listener: TimeListener) => {
timeListeners.add(listener);
return () => timeListeners.delete(listener);