Skip to content

Commit 11c1f61

Browse files
Codexmiguel-heygen
authored andcommitted
perf(studio): schedule adaptive timeline thumbnails
1 parent 29f004c commit 11c1f61

13 files changed

Lines changed: 1015 additions & 5 deletions

‎packages/studio/src/components/TimelineToolbar.test.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { TimelineToolbar } from "./TimelineToolbar";
1212

1313
afterEach(() => {
1414
document.body.innerHTML = "";
15-
usePlayerStore.setState({ autoKeyframeEnabled: true });
15+
usePlayerStore.setState({ autoKeyframeEnabled: true, thumbnailMode: "adaptive" });
1616
});
1717

1818
function renderToolbar(
@@ -58,6 +58,25 @@ describe("TimelineToolbar — auto-keyframe toggle (#1808)", () => {
5858
act(() => root.unmount());
5959
});
6060
});
61+
62+
describe("TimelineToolbar — adaptive thumbnails", () => {
63+
it("keeps a user-controlled hidden mode as the rollback path", () => {
64+
const { host, root } = renderToolbar();
65+
const button = host.querySelector<HTMLButtonElement>(
66+
'button[aria-label="Hide thumbnails — labels only"]',
67+
);
68+
if (!button) throw new Error("thumbnail toggle not rendered");
69+
70+
act(() => button.click());
71+
72+
expect(usePlayerStore.getState().thumbnailMode).toBe("hidden");
73+
expect(button.getAttribute("aria-label")).toBe(
74+
"Show thumbnails — posters stay visible; richer previews appear on interaction",
75+
);
76+
act(() => root.unmount());
77+
});
78+
});
79+
6180
describe("TimelineToolbar — motion path endpoints", () => {
6281
it("does not advertise a destructive keyframe toggle for a required endpoint", () => {
6382
usePlayerStore.setState({ currentTime: 10 });

‎packages/studio/src/components/TimelineToolbar.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef } from "react";
2-
import { Magnet, MagnifyingGlassMinus, MagnifyingGlassPlus } from "@phosphor-icons/react";
2+
import { Image, Magnet, MagnifyingGlassMinus, MagnifyingGlassPlus } from "@phosphor-icons/react";
33
import {
44
useEnableKeyframes,
55
isPlayheadWithinTween,
@@ -111,6 +111,9 @@ export function TimelineToolbar({ domEditSession, onSplitElement }: TimelineTool
111111
const setTimelineSnapEnabled = usePlayerStore((s) => s.setTimelineSnapEnabled);
112112
const autoKeyframeEnabled = usePlayerStore((s) => s.autoKeyframeEnabled);
113113
const setAutoKeyframeEnabled = usePlayerStore((s) => s.setAutoKeyframeEnabled);
114+
const thumbnailMode = usePlayerStore((s) => s.thumbnailMode);
115+
const setThumbnailMode = usePlayerStore((s) => s.setThumbnailMode);
116+
const thumbnailsVisible = thumbnailMode === "adaptive";
114117
// Subscribe so the add-beat button reacts to playhead movement and analysis load.
115118
const currentTime = usePlayerStore((s) => s.currentTime);
116119
const beatAnalysisReady = usePlayerStore((s) => s.beatAnalysis !== null);
@@ -403,6 +406,31 @@ export function TimelineToolbar({ domEditSession, onSplitElement }: TimelineTool
403406
})()}
404407
</div>
405408
<div className="flex items-center gap-0.5">
409+
<Tooltip
410+
label={
411+
thumbnailsVisible
412+
? "Hide thumbnails — labels only"
413+
: "Show thumbnails — posters stay visible; richer previews appear on interaction"
414+
}
415+
>
416+
<button
417+
type="button"
418+
aria-label={
419+
thumbnailsVisible
420+
? "Hide thumbnails — labels only"
421+
: "Show thumbnails — posters stay visible; richer previews appear on interaction"
422+
}
423+
aria-pressed={thumbnailsVisible}
424+
onClick={() => setThumbnailMode(thumbnailsVisible ? "hidden" : "adaptive")}
425+
className={`h-7 px-2 rounded-md text-[11px] font-medium transition-colors ${
426+
thumbnailsVisible
427+
? "bg-studio-accent/10 text-studio-accent"
428+
: "text-neutral-400 hover:bg-white/[0.06] hover:text-neutral-200"
429+
}`}
430+
>
431+
<Image size={16} aria-hidden="true" />
432+
</button>
433+
</Tooltip>
406434
<Tooltip label="Fit timeline to width">
407435
<button
408436
type="button"

‎packages/studio/src/hooks/useRenderClipContent.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { createRoot } from "react-dom/client";
55
import { afterEach, describe, expect, it } from "vitest";
66
import { CompositionThumbnail, VideoThumbnail } from "../player";
77
import { AudioWaveform } from "../player/components/AudioWaveform";
8-
import type { TimelineElement } from "../player/store/playerStore";
8+
import { usePlayerStore, type TimelineElement } from "../player/store/playerStore";
99
import { normalizeCompositionSrc } from "./useRenderClipContent";
1010
import { useRenderClipContent } from "./useRenderClipContent";
1111

1212
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
1313

1414
afterEach(() => {
15+
usePlayerStore.setState({ thumbnailMode: "hidden" });
1516
document.body.innerHTML = "";
1617
});
1718

@@ -106,6 +107,8 @@ describe("useRenderClipContent", () => {
106107
});
107108

108109
it("passes empty labels to thumbnail content so TimelineClip owns clip names", () => {
110+
usePlayerStore.setState({ thumbnailMode: "adaptive" });
111+
109112
const cases: Array<{ content: ReactNode; type: unknown }> = [
110113
{
111114
content: renderClipContent({

‎packages/studio/src/hooks/useRenderClipContent.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { TimelineElement } from "../player";
55
import { AudioWaveform } from "../player/components/AudioWaveform";
66
import { ImageThumbnail } from "../player/components/ImageThumbnail";
77
import { encodePreviewPath, resolveMediaPreviewUrl } from "../player/components/thumbnailUtils";
8+
import { usePlayerStore } from "../player/store/playerStore";
9+
import { effectiveThumbnailMode } from "../player/lib/thumbnailPolicy";
810

911
export function normalizeCompositionSrc(
1012
compSrc: string,
@@ -87,13 +89,22 @@ export function useRenderClipContent({
8789
activePreviewUrl,
8890
effectiveTimelineDuration,
8991
}: UseRenderClipContentOptions) {
92+
// Self-sourced so the adaptive policy gates thumbnail generation without App plumbing.
93+
const thumbnailMode = usePlayerStore((s) => s.thumbnailMode);
94+
const effectiveMode = effectiveThumbnailMode(thumbnailMode);
9095
return useCallback(
9196
// Pre-existing clip-content dispatcher; reduced by extracting renderAudioClip.
9297
// fallow-ignore-next-line complexity
9398
(el: TimelineElement, style: { clip: string; label: string }): ReactNode => {
9499
const pid = projectIdRef.current;
95100
if (!pid) return null;
96101

102+
// Thumbnail generation disabled (perf) -> plain clip bars. Audio still shows
103+
// its waveform (cheap, not a frame thumbnail). Toggle: timeline toolbar.
104+
if (effectiveMode === "hidden") {
105+
return el.tag === "audio" ? renderAudioClip(el, pid, style.label) : null;
106+
}
107+
97108
let compSrc = el.compositionSrc;
98109
if (compSrc) {
99110
compSrc = normalizeCompositionSrc(compSrc, pid, window.location.origin);
@@ -182,6 +193,6 @@ export function useRenderClipContent({
182193

183194
return null;
184195
},
185-
[projectIdRef, compIdToSrc, activePreviewUrl, effectiveTimelineDuration],
196+
[projectIdRef, compIdToSrc, activePreviewUrl, effectiveTimelineDuration, effectiveMode],
186197
);
187198
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, expect, it } from "vitest";
2+
import { defaultThumbnailMode, effectiveThumbnailMode } from "./thumbnailPolicy";
3+
4+
describe("thumbnail runtime policy", () => {
5+
it("defaults missing preferences adaptively after activation", () => {
6+
expect(defaultThumbnailMode(undefined, "follow-preference")).toBe("adaptive");
7+
expect(defaultThumbnailMode(undefined, "legacy-default")).toBe("hidden");
8+
});
9+
10+
it("forces the safe renderer without overwriting user intent", () => {
11+
expect(effectiveThumbnailMode("adaptive", "force-hidden")).toBe("hidden");
12+
expect(effectiveThumbnailMode("adaptive", "follow-preference")).toBe("adaptive");
13+
});
14+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export type ThumbnailMode = "adaptive" | "hidden";
2+
export type ThumbnailRuntimePolicy = "follow-preference" | "force-hidden" | "legacy-default";
3+
4+
// "Adaptive" currently means the scheduler pauses rich work while scrolling.
5+
// The mode name leaves room for finer-grained runtime heuristics later.
6+
7+
const rawPolicy = import.meta.env.VITE_STUDIO_TIMELINE_THUMBNAIL_POLICY;
8+
9+
const studioThumbnailRuntimePolicy: ThumbnailRuntimePolicy =
10+
rawPolicy === "force-hidden" || rawPolicy === "legacy-default" ? rawPolicy : "follow-preference";
11+
12+
export function defaultThumbnailMode(
13+
storedMode: ThumbnailMode | undefined,
14+
policy: ThumbnailRuntimePolicy = studioThumbnailRuntimePolicy,
15+
): ThumbnailMode {
16+
return storedMode ?? (policy === "legacy-default" ? "hidden" : "adaptive");
17+
}
18+
19+
export function effectiveThumbnailMode(
20+
preferredMode: ThumbnailMode,
21+
policy: ThumbnailRuntimePolicy = studioThumbnailRuntimePolicy,
22+
): ThumbnailMode {
23+
return policy === "force-hidden" ? "hidden" : preferredMode;
24+
}

0 commit comments

Comments
 (0)