diff --git a/packages/studio/src/player/components/useTimelineFocusCoordinator.test.tsx b/packages/studio/src/player/components/useTimelineFocusCoordinator.test.tsx index 10fa3ae243..c4849bad5a 100644 --- a/packages/studio/src/player/components/useTimelineFocusCoordinator.test.tsx +++ b/packages/studio/src/player/components/useTimelineFocusCoordinator.test.tsx @@ -150,17 +150,27 @@ describe("useTimelineFocusCoordinator", () => { expect(document.activeElement).not.toBe(externalTarget); }); - it("persists a deterministic parent-row fallback when a focused clip disappears", async () => { + it("moves focus to the parent row when a focused clip disappears, writing the store only on unmount", async () => { usePlayerStore.getState().requestTimelineFocus(clipId); await act(async () => root.render()); + const writes: string[] = []; + const off = usePlayerStore.subscribe((now, was) => { + if (now.timelineFocus !== was.timelineFocus) writes.push(now.timelineFocus?.id ?? "none"); + }); const collapsedRows: readonly TimelineLogicalRow[] = [{ ...rows[0]!, items: [] }]; await act(async () => root.render()); - expect(usePlayerStore.getState().timelineFocus?.id).toBe(rowId); expect(document.activeElement?.getAttribute("data-timeline-focus-id")).toBe(rowId); + // A write right after the delete's commit makes every player-store subscriber render once more for nothing. + expect(writes, "no store write while the timeline is mounted").toEqual([]); + + act(() => root.unmount()); + off(); + expect(usePlayerStore.getState().timelineFocus?.id, "a remount keeps the row").toBe(rowId); + root = createRoot(host); }); - it("persists the next surviving row when both a clip and its parent track disappear", async () => { + it("moves focus to the next surviving row when both a clip and its parent track disappear", async () => { const nextRowId = timelineTrackRowId(2); const nextRow: TimelineLogicalRow = { ...rows[0]!, @@ -173,8 +183,10 @@ describe("useTimelineFocusCoordinator", () => { await act(async () => root.render()); - expect(usePlayerStore.getState().timelineFocus?.id).toBe(nextRowId); expect(document.activeElement?.getAttribute("data-timeline-focus-id")).toBe(nextRowId); + act(() => root.unmount()); + expect(usePlayerStore.getState().timelineFocus?.id).toBe(nextRowId); + root = createRoot(host); }); it("keeps a request alive across a few renders where its target hasn't landed yet", async () => { diff --git a/packages/studio/src/player/components/useTimelineFocusCoordinator.ts b/packages/studio/src/player/components/useTimelineFocusCoordinator.ts index 6e20247ce0..1a123669b1 100644 --- a/packages/studio/src/player/components/useTimelineFocusCoordinator.ts +++ b/packages/studio/src/player/components/useTimelineFocusCoordinator.ts @@ -126,6 +126,15 @@ const MAX_UNRESOLVED_FOCUS_RETRIES = 5; // bounds it by wall time instead, independent of re-renders. const UNRESOLVED_FOCUS_TIMEOUT_MS = 4000; +function keepFallbackForRemount(resolvedRef: RefObject<{ nonce: number; id: string } | null>) { + return () => { + const resolved = resolvedRef.current; + const stored = usePlayerStore.getState().timelineFocus; + if (resolved && stored?.nonce === resolved.nonce && stored.id !== resolved.id) + usePlayerStore.getState().requestTimelineFocus(resolved.id); + }; +} + /** Model-first focus actor; mounting is a consequence of its returned pins. */ // Resolution, fallback, reveal, and focus form one ordered state machine. // fallow-ignore-next-line complexity @@ -178,6 +187,8 @@ export function useTimelineFocusCoordinator({ previousRowsRef.current = logicalRows; }, [logicalRows]); + useEffect(() => keepFallbackForRemount(resolvedRef), []); + // Apply a request exactly once after its logical target and DOM node are both ready. // fallow-ignore-next-line complexity useEffect(() => { @@ -204,10 +215,6 @@ export function useTimelineFocusCoordinator({ if (unresolvedFocusAttemptsRef.current.count !== 0) { unresolvedFocusAttemptsRef.current = { id: -1, count: 0 }; } - if (resolution.target.id !== request.id) { - usePlayerStore.getState().requestTimelineFocus(resolution.target.id); - return; - } if ( appliedRef.current?.nonce === request.nonce && appliedRef.current.id === resolution.target.id