Skip to content

Commit 18b9aca

Browse files
committed
fix(studio): improve preview loading reliability
1 parent 3c857d7 commit 18b9aca

8 files changed

Lines changed: 287 additions & 127 deletions

File tree

‎packages/studio/src/components/nle/NLEContext.tsx‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface NLEContextValue {
3232
togglePlay: () => void;
3333
seek: (time: number, options?: { keepPlaying?: boolean }) => boolean;
3434
refreshPlayer: () => void;
35-
onIframeLoad: () => void;
35+
onIframeLoad: (reportError?: (message: string) => void) => void;
3636
// composition stack (from useCompositionStack)
3737
compositionStack: CompositionLevel[];
3838
updateCompositionStack: React.Dispatch<React.SetStateAction<CompositionLevel[]>>;
@@ -122,14 +122,17 @@ export function NLEProvider({
122122
refreshPlayer();
123123
}, [refreshKey, refreshPlayer]);
124124

125-
const onIframeLoad = useCallback(() => {
126-
baseOnIframeLoad();
127-
// Pre-load + register MotionPathPlugin once so adding a motion path in the
128-
// studio doesn't take the async plugin-load flash path on the first soft
129-
// reload (the comp may not ship the plugin until it actually uses one).
130-
ensureMotionPathPluginLoaded(iframeRef.current);
131-
onIframeRef?.(iframeRef.current);
132-
}, [baseOnIframeLoad, iframeRef, onIframeRef]);
125+
const onIframeLoad = useCallback(
126+
(reportError?: (message: string) => void) => {
127+
baseOnIframeLoad(reportError);
128+
// Pre-load + register MotionPathPlugin once so adding a motion path in the
129+
// studio doesn't take the async plugin-load flash path on the first soft
130+
// reload (the comp may not ship the plugin until it actually uses one).
131+
ensureMotionPathPluginLoaded(iframeRef.current);
132+
onIframeRef?.(iframeRef.current);
133+
},
134+
[baseOnIframeLoad, iframeRef, onIframeRef],
135+
);
133136

134137
const {
135138
compositionStack,

‎packages/studio/src/components/nle/NLEPreview.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { readStudioUiPreferences, writeStudioUiPreferences } from "../../utils/s
1515
interface NLEPreviewProps {
1616
projectId: string;
1717
iframeRef: RefObject<HTMLIFrameElement | null>;
18-
onIframeLoad: () => void;
18+
onIframeLoad: (reportError?: (message: string) => void) => void;
1919
onCompositionLoadingChange?: (loading: boolean) => void;
2020
portrait?: boolean;
2121
directUrl?: string;
@@ -491,9 +491,9 @@ export const NLEPreview = memo(function NLEPreview({
491491
ref={setPreviewIframeRef}
492492
projectId={directUrl ? undefined : projectId}
493493
directUrl={directUrl}
494-
onLoad={() => {
494+
onLoad={(reportError) => {
495495
updateCompositionSizeFromPreview();
496-
onIframeLoad();
496+
onIframeLoad(reportError);
497497
applyInitialZoom();
498498
}}
499499
onCompositionLoadingChange={onCompositionLoadingChange}

‎packages/studio/src/components/sidebar/CompositionsTab.drag.test.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,33 @@ function mount(onSelect = vi.fn(), onAddToTimeline = vi.fn()) {
4040
}
4141

4242
describe("composition card drag", () => {
43+
it("uses a cached image instead of eagerly mounting a live preview iframe", () => {
44+
const { host } = mount();
45+
expect(host.querySelector('img[src*="/thumbnail/"]')).not.toBeNull();
46+
expect(host.querySelector("iframe")).toBeNull();
47+
});
48+
49+
it("mounts one live preview only after sustained hover and removes it on leave", () => {
50+
vi.useFakeTimers();
51+
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
52+
try {
53+
const { host, card } = mount();
54+
act(() => {
55+
card.dispatchEvent(new Event("pointerover", { bubbles: true }));
56+
vi.advanceTimersByTime(300);
57+
});
58+
expect(host.querySelectorAll("iframe")).toHaveLength(1);
59+
60+
act(() => {
61+
card.dispatchEvent(new Event("pointerout", { bubbles: true }));
62+
});
63+
expect(host.querySelector("iframe")).toBeNull();
64+
} finally {
65+
consoleError.mockRestore();
66+
vi.useRealTimers();
67+
}
68+
});
69+
4370
it("keeps ordinary click navigation", () => {
4471
const { card, onSelect } = mount();
4572
act(() => card.click());

‎packages/studio/src/components/sidebar/CompositionsTab.tsx‎

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { memo, useCallback, useEffect, useRef, useState } from "react";
22
import { setPreviewMediaMuted } from "../../player/lib/timelineIframeHelpers";
3+
import { buildCompositionThumbnailUrl } from "../../player/components/CompositionThumbnail";
34
import { TIMELINE_COMPOSITION_MIME } from "../../utils/timelineCompositionDrop";
45

56
interface CompositionsTabProps {
@@ -130,6 +131,7 @@ function CompCard({
130131
}) {
131132
const [hovered, setHovered] = useState(false);
132133
const [stageSize, setStageSize] = useState(DEFAULT_PREVIEW_STAGE);
134+
const [livePreviewLoaded, setLivePreviewLoaded] = useState(false);
133135
const iframeRef = useRef<HTMLIFrameElement | null>(null);
134136
const hoverTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
135137
const syncTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -159,9 +161,16 @@ function CompCard({
159161
hoverTimer.current = null;
160162
}
161163
setHovered(false);
164+
setLivePreviewLoaded(false);
162165
};
163166
const name = comp.replace(/^compositions\//, "").replace(/\.html$/, "");
164167
const previewUrl = `/api/projects/${projectId}/preview/comp/${comp}`;
168+
const thumbnailUrl = buildCompositionThumbnailUrl({
169+
previewUrl,
170+
seekTime: 0,
171+
duration: THUMBNAIL_SEEK_TIME_SECONDS * 2,
172+
origin: window.location.origin,
173+
});
165174
const previewScale = resolveCompositionPreviewScale({
166175
cardWidth: CARD_W,
167176
cardHeight: CARD_H,
@@ -172,7 +181,7 @@ function CompCard({
172181
const thumbnailOffsetY = (CARD_H - stageSize.height * previewScale) / 2;
173182

174183
useEffect(() => {
175-
requestIframePlaybackSync(hovered);
184+
if (hovered) requestIframePlaybackSync(true);
176185
}, [hovered, requestIframePlaybackSync]);
177186

178187
useEffect(() => {
@@ -216,36 +225,49 @@ function CompCard({
216225
}`}
217226
>
218227
<div className="w-20 h-[45px] rounded overflow-hidden bg-neutral-900 flex-shrink-0 relative">
219-
<iframe
220-
ref={iframeRef}
221-
src={previewUrl}
222-
sandbox="allow-scripts allow-same-origin"
228+
<img
229+
src={thumbnailUrl}
230+
alt=""
231+
draggable={false}
223232
loading="lazy"
224-
className="absolute border-none pointer-events-none"
225-
style={{
226-
transformOrigin: "0 0",
227-
width: stageSize.width,
228-
height: stageSize.height,
229-
left: thumbnailOffsetX,
230-
top: thumbnailOffsetY,
231-
transform: `scale(${previewScale})`,
232-
}}
233-
onLoad={(e) => {
234-
try {
235-
const iframe = e.currentTarget;
236-
const root = iframe.contentDocument?.querySelector("[data-composition-id]");
237-
const width = Number(root?.getAttribute("data-width")) || DEFAULT_PREVIEW_STAGE.width;
238-
const height =
239-
Number(root?.getAttribute("data-height")) || DEFAULT_PREVIEW_STAGE.height;
240-
setStageSize({ width, height });
241-
requestIframePlaybackSync(hovered);
242-
} catch {
243-
setStageSize(DEFAULT_PREVIEW_STAGE);
244-
}
245-
}}
246-
title={`${name} preview`}
247-
tabIndex={-1}
233+
decoding="async"
234+
className={`absolute inset-0 h-full w-full object-contain transition-opacity ${
235+
livePreviewLoaded ? "opacity-0" : "opacity-100"
236+
}`}
248237
/>
238+
{hovered && (
239+
<iframe
240+
ref={iframeRef}
241+
src={previewUrl}
242+
sandbox="allow-scripts allow-same-origin"
243+
className="absolute border-none pointer-events-none"
244+
style={{
245+
transformOrigin: "0 0",
246+
width: stageSize.width,
247+
height: stageSize.height,
248+
left: thumbnailOffsetX,
249+
top: thumbnailOffsetY,
250+
transform: `scale(${previewScale})`,
251+
}}
252+
onLoad={(e) => {
253+
try {
254+
const iframe = e.currentTarget;
255+
const root = iframe.contentDocument?.querySelector("[data-composition-id]");
256+
const width =
257+
Number(root?.getAttribute("data-width")) || DEFAULT_PREVIEW_STAGE.width;
258+
const height =
259+
Number(root?.getAttribute("data-height")) || DEFAULT_PREVIEW_STAGE.height;
260+
setStageSize({ width, height });
261+
setLivePreviewLoaded(true);
262+
requestIframePlaybackSync(true);
263+
} catch {
264+
setStageSize(DEFAULT_PREVIEW_STAGE);
265+
}
266+
}}
267+
title={`${name} preview`}
268+
tabIndex={-1}
269+
/>
270+
)}
249271
</div>
250272
<div
251273
className="min-w-0 flex-1"

‎packages/studio/src/player/components/Player.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
// @vitest-environment happy-dom
22

33
import { describe, expect, it } from "vitest";
4-
import { hasUnloadedAssets, shouldShowCompositionLoadingOverlay } from "./Player";
4+
import {
5+
hasUnloadedAssets,
6+
readPreviewErrorMessage,
7+
shouldShowCompositionLoadingOverlay,
8+
} from "./Player";
9+
10+
describe("preview errors", () => {
11+
it("reads the player probe error for the visible retry state", () => {
12+
expect(
13+
readPreviewErrorMessage(
14+
new CustomEvent("error", {
15+
detail: { message: "Composition timeline not found after 8s" },
16+
}),
17+
),
18+
).toBe("Composition timeline not found after 8s");
19+
});
20+
21+
it("falls back when the player emits an unstructured error", () => {
22+
expect(readPreviewErrorMessage(new Event("error"))).toBe(
23+
"The composition preview did not become ready.",
24+
);
25+
});
26+
});
527

628
describe("composition loading overlay", () => {
729
it("shows while the composition is loading", () => {

0 commit comments

Comments
 (0)