Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): read the document at write time in addZoomsBulk

超超超超超级喜欢你的达妮娅df956f8f010d
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index 2bc430311..e9ad26df1 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -1596,3 +1596,63 @@ describe("useTimeline audio tracks", () => {
expect(probeAudioDurationMock).toHaveBeenCalledTimes(1);
});
});
+
+// The wand (`V4Timeline.runAutoZooms`) captures this callback, awaits a
+// multi-second cursor-telemetry IPC, and only then calls it. Anything the user
+// commits during that wait is in the store but not in the callback's render
+// closure, so reading the closure writes back a snapshot that drops their edit.
+// Its `add*` siblings compute and save in the same tick, which is why this one
+// is the reachable case.
+describe("useTimeline.addZoomsBulk reads the document at write time", () => {
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ beforeEach(() => {
+ useProjectStore.getState().clear();
+ for (const mock of Object.values(bridgeMocks)) mock.mockReset();
+ toastErrorMock.mockReset();
+ bridgeMocks.save.mockImplementation(async (document: AxcutDocument) => ({
+ success: true,
+ document,
+ }));
+ useProjectStore.setState({
+ projectId: "proj_test",
+ document: sampleDoc,
+ revision: 1,
+ status: "ready",
+ error: null,
+ });
+ });
+
+ it("does not write back the document from before the telemetry wait", async () => {
+ const { result } = renderTimeline();
+ // Captured the way the wand captures it: before the wait, not after.
+ const addZoomsBulk = result.current.addZoomsBulk;
+
+ // The user's edit lands while the wand is off fetching telemetry.
+ const edited: AxcutDocument = {
+ ...sampleDoc,
+ project: { ...sampleDoc.project, title: "Edited while the wand was busy" },
+ };
+ act(() => {
+ useProjectStore.setState({ document: edited, revision: 2 });
+ });
+
+ let added: number | undefined;
+ await act(async () => {
+ added = await addZoomsBulk([
+ { span: { start: 1000, end: 2000 }, focus: { cx: 0.5, cy: 0.5 } },
+ ]);
+ });
+
+ expect(added).toBe(1);
+ const saved = bridgeMocks.save.mock.calls.at(-1)?.[0] as AxcutDocument;
+ // The zoom was added, and the edit is still there.
+ expect(saved.zoomRanges).toHaveLength(1);
+ expect(saved.project.title).toBe("Edited while the wand was busy");
+ expect(useProjectStore.getState().document?.project.title).toBe(
+ "Edited while the wand was busy",
+ );
+ });
+});
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 40e2408fd..860b9f13f 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -334,7 +334,15 @@ export function useTimeline() {
// Returns the count actually added (0 when there's no doc/suggestions).
const addZoomsBulk = useCallback(
async (suggestions: AutoZoomSuggestion[]) => {
- if (!document || suggestions.length === 0) return 0;
+ // Read from the store, not off the render closure. Unlike its `add*` siblings,
+ // which compute and save in the same tick, this one is reached from the wand
+ // AFTER a multi-second cursor-telemetry IPC: the closure document is the one
+ // from before that wait, so anything the user committed during it is missing
+ // from the snapshot, and writing the snapshot back drops their edit. Reading
+ // here is also what lets this compose with `useSequentialTimelineOps` -- same
+ // reason as `applyClipEdit`, `setTrimEntries` and `insertClipAt`.
+ const doc = useProjectStore.getState().document;
+ if (!doc || suggestions.length === 0) return 0;
const anchored = suggestions.flatMap((s) =>
anchorRegionsWithDerivedMs(
[
@@ -347,18 +355,21 @@ export function useTimeline() {
focusMode: "auto" as const,
},
],
- document.timeline.clips,
+ // Anchored against the SAME document the write is built from: anchoring
+ // on the stale clips and saving the fresh document would place regions
+ // against a timeline that no longer exists.
+ doc.timeline.clips,
() => createId("zoom"),
),
);
const next: AxcutDocument = {
- ...document,
- zoomRanges: [...document.zoomRanges, ...anchored] as AxcutDocument["zoomRanges"],
+ ...doc,
+ zoomRanges: [...doc.zoomRanges, ...anchored] as AxcutDocument["zoomRanges"],
};
if (!(await saveDocument(next, { history: true }))) return 0;
return suggestions.length;
},
- [document, saveDocument],
+ [saveDocument],
);
const addTrim = useCallback(