Skip to content

Commit 0ab4931

Browse files
fix(studio): a clip's thumbnail strip stays put while you trim it (#6942)
* fix(studio): a clip's thumbnail strip stays put while you trim it * fix(studio): cover the trim-to-hold link and drop the comment * refactor(studio): the held trim range lives in its own small hook * refactor(studio): the thumbnail's held request inputs move into one hook * chore(studio): drop the hook docstring
1 parent 6b0abb4 commit 0ab4931

4 files changed

Lines changed: 113 additions & 12 deletions

File tree

‎packages/studio/src/player/components/VideoThumbnail.test.tsx‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,62 @@ describe("VideoThumbnail during a zoom", () => {
452452
);
453453
});
454454
});
455+
456+
describe("VideoThumbnail during a trim", () => {
457+
beforeEach(() => {
458+
vi.useFakeTimers({
459+
toFake: ["setTimeout", "clearTimeout", "requestAnimationFrame", "cancelAnimationFrame"],
460+
});
461+
vi.mocked(decodeVideoThumbnail).mockResolvedValue({
462+
value: { kind: "filmstrip", urls: ["blob:a", "blob:b"], aspect: 16 / 9 },
463+
weight: 256,
464+
});
465+
});
466+
afterEach(() => {
467+
vi.useRealTimers();
468+
});
469+
470+
const trimmed = (duration: number, sourceStart: number) => (
471+
<VideoThumbnail
472+
videoSrc="/api/projects/p/preview/assets/clip.mp4"
473+
label=""
474+
labelColor="#fff"
475+
projectId="p"
476+
sessionEpoch={1}
477+
duration={duration}
478+
sourceStart={sourceStart}
479+
sourceRangeDuration={duration}
480+
/>
481+
);
482+
483+
it("keeps its strip through every move of a trim and decodes the new range once at rest", async () => {
484+
Object.defineProperty(host, "clientWidth", { configurable: true, value: 440 });
485+
Object.defineProperty(host, "clientHeight", { configurable: true, value: 40 });
486+
const root = harness.mount(host);
487+
await act(async () => {
488+
root.render(trimmed(10, 0));
489+
await Promise.resolve();
490+
});
491+
vi.mocked(decodeVideoThumbnail).mockClear();
492+
for (const [duration, start] of [
493+
[9.5, 0.5],
494+
[9, 1],
495+
[8.5, 1.5],
496+
] as const) {
497+
await act(async () => {
498+
markTimelineMotion();
499+
root.render(trimmed(duration, start));
500+
await Promise.resolve();
501+
});
502+
}
503+
expect(decodeVideoThumbnail).not.toHaveBeenCalled();
504+
await act(async () => {
505+
vi.runOnlyPendingTimers();
506+
await Promise.resolve();
507+
});
508+
const ranges = vi
509+
.mocked(decodeVideoThumbnail)
510+
.mock.calls.map(([r]) => [r.sourceStart, r.sourceRangeDuration]);
511+
expect(new Set(ranges.map((r) => r.join()))).toEqual(new Set(["1.5,8.5"]));
512+
});
513+
});

‎packages/studio/src/player/components/VideoThumbnail.tsx‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ function useVideoThumbnailSnapshot(
104104
return selectThumbnailSnapshot(posterSnapshot, richSnapshot, shownSnapshot);
105105
}
106106

107+
function useRangeHeldDuringTrim(
108+
props: Required<
109+
Pick<VideoThumbnailProps, "videoSrc" | "duration" | "projectId" | "sessionEpoch" | "priority">
110+
> &
111+
Pick<VideoThumbnailProps, "sourceStart" | "sourceRangeDuration">,
112+
) {
113+
const {
114+
videoSrc,
115+
sourceStart,
116+
sourceRangeDuration,
117+
duration,
118+
projectId,
119+
sessionEpoch,
120+
priority,
121+
} = props;
122+
const range = useValueAtRest(
123+
useMemo(
124+
() => ({ sourceStart, sourceRangeDuration, duration }),
125+
[duration, sourceRangeDuration, sourceStart],
126+
),
127+
);
128+
return useMemo(
129+
() => ({ videoSrc, ...range, projectId, sessionEpoch, priority }),
130+
[priority, projectId, range, sessionEpoch, videoSrc],
131+
);
132+
}
133+
107134
/** Sparse, bounded video frames supplied by the shared thumbnail scheduler. */
108135
export const VideoThumbnail = memo(function VideoThumbnail({
109136
videoSrc,
@@ -122,18 +149,15 @@ export const VideoThumbnail = memo(function VideoThumbnail({
122149
computeThumbnailStrip(container.width, 16 / 9, container.height).frameCount,
123150
),
124151
);
125-
const requestProps = useMemo(
126-
() => ({
127-
videoSrc,
128-
sourceStart,
129-
sourceRangeDuration,
130-
duration,
131-
projectId,
132-
sessionEpoch,
133-
priority,
134-
}),
135-
[duration, priority, projectId, sessionEpoch, sourceRangeDuration, sourceStart, videoSrc],
136-
);
152+
const requestProps = useRangeHeldDuringTrim({
153+
videoSrc,
154+
sourceStart,
155+
sourceRangeDuration,
156+
duration,
157+
projectId,
158+
sessionEpoch,
159+
priority,
160+
});
137161
const posterRequest = useMemo(
138162
() => createVideoThumbnailRequest(requestProps, 1, false),
139163
[requestProps],

‎packages/studio/src/player/components/useTimelineClipDrag.resize.test.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { usePreviewFrameStore } from "../store/previewFrameStore";
88
import type { BlockedClipState, DraggedClipState, ResizingClipState } from "./useTimelineClipDrag";
99
import { useTimelineClipDrag } from "./useTimelineClipDrag";
1010
import { mountReactHarness } from "../../hooks/domSelectionTestHarness";
11+
import { isTimelineMoving, TIMELINE_REST_MS } from "./timelineMotion";
1112

1213
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
1314

@@ -25,6 +26,7 @@ function el(id: string, over: Partial<TimelineElement> = {}): TimelineElement {
2526
}
2627

2728
afterEach(() => {
29+
vi.useRealTimers();
2830
document.body.innerHTML = "";
2931
usePlayerStore.getState().reset();
3032
usePreviewFrameStore.setState({ time: null });
@@ -222,6 +224,20 @@ describe("useTimelineClipDrag — single-clip resize (unchanged path)", () => {
222224
h.unmount();
223225
});
224226

227+
it("marks the timeline as moving on every trim move, so thumbnails hold their range", () => {
228+
vi.useFakeTimers();
229+
const a = el("a", { start: 0, duration: 2 });
230+
const h = renderResizeHarness([a], []);
231+
h.startResize(a, "end");
232+
act(() => vi.advanceTimersByTime(TIMELINE_REST_MS));
233+
expect(isTimelineMoving()).toBe(false);
234+
235+
h.movePointer(50);
236+
237+
expect(isTimelineMoving()).toBe(true);
238+
h.unmount();
239+
});
240+
225241
it("does not let an older rejected resize roll back a newer optimistic gesture", async () => {
226242
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
227243
const a = el("a", { start: 0, duration: 2 });

‎packages/studio/src/player/components/useTimelineClipDrag.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useRef, useState, useCallback, useMemo, useEffect } from "react";
22
import { useMountEffect } from "../../hooks/useMountEffect";
3+
import { markTimelineMotion } from "./timelineMotion";
34
import {
45
applyTimelineAutoScrollStep,
56
resolveTimelineAutoScrollLoopAction,
@@ -201,6 +202,7 @@ export function useTimelineClipDrag({
201202
const [resizingClip, setResizingClipState] = useState<ResizingClipState | null>(null);
202203
const resizingClipRef = useRef<ResizingClipState | null>(null);
203204
const publishResizingClip = useCallback((next: ResizingClipState | null) => {
205+
if (next) markTimelineMotion();
204206
resizingClipRef.current = next;
205207
setResizingClipState(next);
206208
}, []);

0 commit comments

Comments
 (0)