Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"build": "vite build && tsup",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test:timeline-virtualization": "node tests/e2e/timeline-virtualization.mjs",
"test:watch": "vitest",
"report:sdk-cutover": "bun src/utils/sdkCutoverPolicy.report.ts"
},
Expand Down
150 changes: 150 additions & 0 deletions packages/studio/src/hooks/useStudioTestHooks.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// @vitest-environment happy-dom
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import { usePlayerStore } from "../player/store/playerStore";
import {
createTimelinePerformanceFixture,
type TimelinePerformanceFixtureProfile,
} from "../player/lib/timelinePerformanceFixture";
import { useStudioTestHooks } from "./useStudioTestHooks";

Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);

const PROFILES: readonly TimelinePerformanceFixtureProfile[] = [
"dense-short",
"long-overlap",
"keyframe-heavy-expanded",
"composition-heavy",
"remote-unsupported",
];

function Probe(): null {
useStudioTestHooks({
previewIframeRef: { current: null },
buildDomSelectionFromTarget: vi.fn(),
applyDomSelection: vi.fn(),
});
return null;
}

describe("timeline performance fixture", () => {
afterEach(() => {
window.__studioTest = undefined;
usePlayerStore.getState().reset();
});

it("generates the expected dense-short 50k distribution", () => {
const first = createTimelinePerformanceFixture({
elementCount: 50_000,
profile: "dense-short",
});

expect(first.summary).toEqual({
elementCount: 50_000,
profile: "dense-short",
duration: 120,
trackCount: 1_000,
keyframedElementCount: 0,
expandedElementCount: 0,
});
expect(new Set(first.elements.map((element) => element.track)).size).toBe(1_000);
const perTrack = new Map<number, number>();
for (const element of first.elements) {
perTrack.set(element.track, (perTrack.get(element.track) ?? 0) + 1);
}
expect(Math.max(...perTrack.values())).toBeLessThanOrEqual(128);
});

it.each(PROFILES)("generates an identical 50k %s fixture", (profile) => {
const first = createTimelinePerformanceFixture({ elementCount: 50_000, profile });
const second = createTimelinePerformanceFixture({ elementCount: 50_000, profile });
expect(second).toEqual(first);
});

it.each(PROFILES)("builds the %s 1k scale profile", (profile) => {
const fixture = createTimelinePerformanceFixture({ elementCount: 1_000, profile });
expect(fixture.elements).toHaveLength(1_000);
expect(fixture.summary.elementCount).toBe(1_000);
expect(fixture.summary.duration).toBeGreaterThan(0);
expect(new Set(fixture.elements.map((element) => element.key)).size).toBe(1_000);
if (profile === "keyframe-heavy-expanded") {
expect(fixture.keyframeCache.size).toBe(1_000);
expect(fixture.gsapAnimations.size).toBe(1_000);
expect(fixture.expandedClipIds.size).toBe(1_000);
}
});

it("publishes one dev-only loader that replaces fixture state atomically", () => {
const host = document.createElement("div");
const root = createRoot(host);
act(() => root.render(<Probe />));
const api = window.__studioTest;
expect(api).toBeDefined();
if (!api) throw new Error("Expected dev Studio test API");
let notifications = 0;
usePlayerStore.setState({
isPlaying: true,
requestedSeekTime: 42,
clipRevealRequest: { elementId: "stale", nonce: 7 },
clipManifest: [],
lintFindingsByElement: new Map([["stale", { count: 1, messages: ["stale"] }]]),
});
const unsubscribe = usePlayerStore.subscribe(() => {
notifications += 1;
});

const summary = api.loadTimelinePerformanceFixture({
elementCount: 1_000,
profile: "keyframe-heavy-expanded",
});

expect(summary.elementCount).toBe(1_000);
expect(notifications).toBe(1);
expect(usePlayerStore.getState()).toMatchObject({
isPlaying: false,
requestedSeekTime: null,
clipRevealRequest: null,
clipManifest: null,
duration: 600,
timelineReady: true,
});
expect(usePlayerStore.getState().lintFindingsByElement.size).toBe(0);
expect(usePlayerStore.getState().elements).toHaveLength(1_000);
expect(usePlayerStore.getState().expandedClipIds.size).toBe(1_000);

api.resetTimelinePerformanceFixture();
expect(notifications).toBe(2);
expect(usePlayerStore.getState()).toMatchObject({
isPlaying: false,
duration: 0,
timelineReady: false,
elements: [],
});
unsubscribe();
act(() => root.unmount());
expect(window.__studioTest).toBeUndefined();
});

it("does not mutate state when the fixture request is invalid", () => {
const host = document.createElement("div");
const root = createRoot(host);
act(() => root.render(<Probe />));
const api = window.__studioTest;
if (!api) throw new Error("Expected dev Studio test API");
const before = usePlayerStore.getState().elements;

expect(() =>
Reflect.apply(api.loadTimelinePerformanceFixture, api, [
{ elementCount: 999, profile: "dense-short" },
]),
).toThrow("elementCount must be 1000 or 50000");
expect(usePlayerStore.getState().elements).toBe(before);
expect(() =>
Reflect.apply(api.loadTimelinePerformanceFixture, api, [
{ elementCount: 1_000, profile: "constructor" },
]),
).toThrow("Unknown timeline performance fixture profile");
act(() => root.unmount());
});
});
42 changes: 42 additions & 0 deletions packages/studio/src/hooks/useStudioTestHooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { useEffect } from "react";
import type { DomEditSelection } from "../components/editor/domEditing";
import { createTimelineResetState, usePlayerStore } from "../player/store/playerStore";
import {
readTimelinePerformanceDiagnostics,
type TimelinePerformanceDiagnostics,
} from "../player/lib/timelinePerformanceDiagnostics";
import {
createTimelinePerformanceFixture,
type TimelinePerformanceFixtureSpec,
type TimelinePerformanceFixtureSummary,
} from "../player/lib/timelinePerformanceFixture";
import { TIMELINE_VIEWPORT_BUDGETS } from "../player/lib/timelineViewportBudgets";

interface StudioTestHookDeps {
previewIframeRef: React.MutableRefObject<HTMLIFrameElement | null>;
Expand All @@ -12,6 +23,12 @@ interface StudioTestHookDeps {

interface StudioTestApi {
selectByDomId: (id: string) => Promise<boolean>;
loadTimelinePerformanceFixture: (
spec: TimelinePerformanceFixtureSpec,
) => TimelinePerformanceFixtureSummary;
resetTimelinePerformanceFixture: () => void;
readTimelinePerformanceDiagnostics: () => Readonly<TimelinePerformanceDiagnostics>;
timelineViewportBudgets: typeof TIMELINE_VIEWPORT_BUDGETS;
}

declare global {
Expand Down Expand Up @@ -52,6 +69,31 @@ export function useStudioTestHooks({
applyDomSelection(selection, { revealPanel: true });
return true;
},
loadTimelinePerformanceFixture: (spec) => {
const fixture = createTimelinePerformanceFixture(spec);
usePlayerStore.setState({
...createTimelineResetState(),
currentTime: 0,
duration: fixture.summary.duration,
timelineReady: true,
loopEnabled: false,
zoomMode: "manual",
manualZoomPercent: 2_000,
elements: fixture.elements,
selectedElementId: null,
selectedElementIds: new Set(),
selectedKeyframes: new Set(),
keyframeCache: fixture.keyframeCache,
gsapAnimations: fixture.gsapAnimations,
expandedClipIds: fixture.expandedClipIds,
});
return fixture.summary;
},
resetTimelinePerformanceFixture: () => {
usePlayerStore.getState().reset();
},
readTimelinePerformanceDiagnostics: () => readTimelinePerformanceDiagnostics(),
timelineViewportBudgets: TIMELINE_VIEWPORT_BUDGETS,
};
window.__studioTest = api;
return () => {
Expand Down
95 changes: 22 additions & 73 deletions packages/studio/src/player/components/Timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useRef, useMemo, useCallback, useState, useEffect, memo } from "react";
import { useRef, useMemo, useCallback, useState, memo } from "react";
import { useMusicBeatAnalysis } from "../../hooks/useMusicBeatAnalysis";
import { isMusicTrack } from "../../utils/timelineInspector";
import { remapBeatAnalysisToComposition } from "../../utils/beatEditActions";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
import { useExpandedTimelineElements } from "../hooks/useExpandedTimelineElements";
import { useMountEffect } from "../../hooks/useMountEffect";
import { defaultTimelineTheme } from "./timelineTheme";
import { useTimelineRangeSelection } from "./useTimelineRangeSelection";
import { useTimelinePlayhead } from "./useTimelinePlayhead";
Expand All @@ -16,14 +15,12 @@ import { TimelineCanvas } from "./TimelineCanvas";
import { type KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu";
import { useTimelineClipDrag } from "./useTimelineClipDrag";
import { TimelineOverlays } from "./TimelineOverlays";
import { animationContributesLane } from "./TimelinePropertyLanes";
import { useTimelineEditPinning } from "./useTimelineEditPinning";
import { useTimelineStackingSync } from "./useTimelineStackingSync";
import { useTimelineGeometry } from "./useTimelineGeometry";
import { useAutoExpandKeyframedClips } from "./useAutoExpandKeyframedClips";
import { GUTTER, LABEL_COL_W, TRACKS_LEFT_PAD, generateTicks } from "./timelineLayout";
import { GUTTER, LABEL_COL_W, TRACKS_LEFT_PAD } from "./timelineLayout";
import { useTimelineScrollViewport } from "./useTimelineScrollViewport";
import { STUDIO_PREVIEW_FPS } from "../lib/time";
import { useResolvedTimelineEditCallbacks } from "./useResolvedTimelineEditCallbacks";
import type { TimelineProps } from "./TimelineTypes";
import {
Expand All @@ -37,11 +34,17 @@ import { useTimelineGapHighlights } from "./useTimelineGapHighlights";
import { useStudioPlaybackContextOptional } from "../../contexts/StudioContext";
import { TimelineRazorGuide, useTimelineRazorInteraction } from "./TimelineRazorInteraction";
import { useTimelinePerformanceTelemetry } from "./useTimelinePerformanceTelemetry";
import {
getEffectiveTimelineDuration,
getTimelinePreviewElement,
hasKeyframedTimelineClips,
} from "./timelineViewModel";
import { useTimelineSelectionLifecycle } from "./useTimelineSelectionLifecycle";
import { useTimelineShiftModifier } from "./useTimelineShiftModifier";
import { useTimelineTicks } from "./useTimelineTicks";

// Re-export pure utilities so existing imports from "./Timeline" still resolve.
export {
generateTicks,
formatTimelineTickLabel,
shouldAutoScrollTimeline,
getTimelineScrollLeftForZoomTransition,
getTimelineScrollLeftForZoomAnchor,
Expand All @@ -52,6 +55,7 @@ export {
shouldHandleTimelineDeleteKey,
getDefaultDroppedTrack,
} from "./timelineLayout";
export { formatTimelineTickLabel, generateTicks } from "./timelineRulerGeometry";

export const Timeline = memo(function Timeline({
onSeek,
Expand Down Expand Up @@ -117,13 +121,7 @@ export const Timeline = memo(function Timeline({
// Label mode = comp has keyframed clips (not just when expanded): keeps the layer
// disclosure + property column visible and reserves a GUTTER before 0s (Figma).
const hasKeyframedClips = useMemo(
() =>
Array.from(gsapAnimations.values()).some((list) =>
// Same lane-contribution predicate the layout uses: real keyframes OR a
// synthesizable flat tween. Checking animation.keyframes alone left a
// flat-tween-only comp without its reserved label column.
list.some((animation) => animationContributesLane(animation)),
),
() => hasKeyframedTimelineClips(gsapAnimations),
[gsapAnimations],
);
const labelMode = hasKeyframedClips;
Expand All @@ -142,20 +140,7 @@ export const Timeline = memo(function Timeline({
const activeTool = usePlayerStore((s) => s.activeTool);
const [hoveredClip, setHoveredClip] = useState<string | null>(null);
const isDragging = useRef(false);
const [shiftHeld, setShiftHeld] = useState(false);

useMountEffect(() => {
const key = (e: KeyboardEvent) => e.key === "Shift" && setShiftHeld(e.type === "keydown");
const blur = () => setShiftHeld(false);
window.addEventListener("keydown", key);
window.addEventListener("keyup", key);
window.addEventListener("blur", blur);
return () => {
window.removeEventListener("keydown", key);
window.removeEventListener("keyup", key);
window.removeEventListener("blur", blur);
};
});
const shiftHeld = useTimelineShiftModifier();

const [showPopover, setShowPopover] = useState(false);
const [kfContextMenu, setKfContextMenu] = useState<KeyframeDiamondContextMenuState | null>(null);
Expand All @@ -172,12 +157,10 @@ export const Timeline = memo(function Timeline({
// Last horizontal scroll offset, restored across the post-edit iframe reload (pinned zoom).
const lastScrollLeftRef = useRef(0);

const effectiveDuration = useMemo(() => {
const safeDur = Number.isFinite(duration) ? duration : 0;
if (rawElements.length === 0) return safeDur;
const result = Math.max(safeDur, ...rawElements.map((el) => el.start + el.duration));
return Number.isFinite(result) ? result : safeDur;
}, [rawElements, duration]);
const effectiveDuration = useMemo(
() => getEffectiveTimelineDuration(duration, rawElements),
[duration, rawElements],
);

const keyframeCache = usePlayerStore((s) => s.keyframeCache);
useAutoExpandKeyframedClips(gsapAnimations);
Expand Down Expand Up @@ -300,14 +283,6 @@ export const Timeline = memo(function Timeline({
toggleSelectedKeyframe,
});

const selectedElement = useMemo(
() =>
expandedElements.find((element) => (element.key ?? element.id) === selectedElementId) ?? null,
[expandedElements, selectedElementId],
);
const selectedElementRef = useRef<TimelineElement | null>(selectedElement);
selectedElementRef.current = selectedElement;

const {
pps,
fitPps,
Expand Down Expand Up @@ -406,41 +381,15 @@ export const Timeline = memo(function Timeline({
});
setRangeSelectionRef.current = setRangeSelection; // stable ref consumed by useTimelineClipDrag

const prevSelectedRef = useRef(selectedElementRef.current);
// eslint-disable-next-line no-restricted-syntax, react-hooks/exhaustive-deps
useEffect(() => {
const prev = prevSelectedRef.current;
const curr = selectedElementRef.current;
prevSelectedRef.current = curr;
if (prev && !curr) {
setShowPopover(false);
setRangeSelection(null);
}
});

// Frame display mode labels ruler ticks as frame numbers — pass the fps so ticks snap to frames.
const tickFps = timeDisplayMode === "frame" ? STUDIO_PREVIEW_FPS : undefined;
const { major, minor } = useMemo(
() => generateTicks(displayDuration, pps, tickFps),
[displayDuration, pps, tickFps],
useTimelineSelectionLifecycle(expandedElements, selectedElementId, setShowPopover, () =>
setRangeSelection(null),
);

const { major, minor } = useTimelineTicks(displayDuration, pps, timeDisplayMode);
const majorTickInterval = major.length >= 2 ? major[1] - major[0] : effectiveDuration;

const getPreviewElement = useCallback(
(element: TimelineElement): TimelineElement => {
if (
resizingClip &&
(resizingClip.element.key ?? resizingClip.element.id) === (element.key ?? element.id)
) {
return {
...element,
start: resizingClip.previewStart,
duration: resizingClip.previewDuration,
playbackStart: resizingClip.previewPlaybackStart,
};
}
return element;
},
(element: TimelineElement): TimelineElement => getTimelinePreviewElement(element, resizingClip),
[resizingClip],
);

Expand Down
Loading
Loading