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): stop losing the source-dimension backfill on fresh recordings

EtienneLescotd8944fc67911
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index fa6cc96fd..e67f07d07 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -306,6 +306,94 @@ describe("useTimeline backfills missing source dimensions on load", () => {
expect(saved?.video).toMatchObject({ width: 1920, height: 1080 });
});
+ // A fresh recording: the document is rewritten (duration, camera link, auto-zoom) while
+ // the probe is still out. That re-render used to cancel the probe's write for good.
+ it("still persists probed dims when the document changes while the probe is running", async () => {
+ let resolveProbe!: (dims: { width: number; height: number }) => void;
+ probeVideoDimensionsMock.mockReturnValue(
+ new Promise((resolve) => {
+ resolveProbe = resolve;
+ }),
+ );
+ const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] };
+ useProjectStore.setState({
+ projectId: "proj_test",
+ document: unprobed,
+ revision: 1,
+ status: "ready",
+ error: null,
+ });
+ renderTimeline();
+ await waitFor(() => expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(1));
+ act(() => {
+ useProjectStore.setState({ document: { ...unprobed, zoomRanges: [] }, revision: 2 });
+ });
+ await act(async () => {
+ resolveProbe({ width: 1080, height: 1920 });
+ });
+ await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(1));
+ const saved = useProjectStore.getState().document?.assets.find((a) => a.id === "asset_1");
+ expect(saved?.video).toMatchObject({ width: 1080, height: 1920 });
+ });
+
+ // The fresh-recording auto-zooms are saved while the probe is out. The store only takes
+ // them once that save returns, so a backfill built on the store before then erased them.
+ it("keeps a save that is still in flight when the probe resolves", async () => {
+ let resolveProbe!: (dims: { width: number; height: number }) => void;
+ probeVideoDimensionsMock.mockReturnValue(
+ new Promise((resolve) => {
+ resolveProbe = resolve;
+ }),
+ );
+ const unprobed = { ...sampleDoc, assets: [{ ...sampleDoc.assets[0], video: undefined }] };
+ useProjectStore.setState({
+ projectId: "proj_test",
+ document: unprobed,
+ revision: 1,
+ status: "ready",
+ error: null,
+ });
+ renderTimeline();
+ await waitFor(() => expect(probeVideoDimensionsMock).toHaveBeenCalledTimes(1));
+
+ let releaseZoomSave!: () => void;
+ const zoomSaveGate = new Promise<void>((resolve) => {
+ releaseZoomSave = resolve;
+ });
+ bridgeMocks.save.mockImplementationOnce(async (doc: typeof sampleDoc) => {
+ await zoomSaveGate;
+ return { success: true, document: doc };
+ });
+ const zoomed = {
+ ...unprobed,
+ zoomRanges: [
+ { id: "zoom_1", startMs: 0, endMs: 1000, depth: 3, focus: { cx: 0.5, cy: 0.5 } },
+ ],
+ } as AxcutDocument;
+ let zoomSave!: Promise<boolean>;
+ act(() => {
+ zoomSave = useProjectStore.getState().saveDocument(zoomed, { history: true });
+ });
+ await act(async () => {
+ resolveProbe({ width: 1080, height: 1920 });
+ await new Promise((r) => setTimeout(r, 0));
+ });
+ await act(async () => {
+ releaseZoomSave();
+ await zoomSave;
+ });
+
+ await waitFor(() => expect(bridgeMocks.save).toHaveBeenCalledTimes(2));
+ await waitFor(() => {
+ const doc = useProjectStore.getState().document;
+ expect(doc?.zoomRanges).toHaveLength(1);
+ expect(doc?.assets.find((a) => a.id === "asset_1")?.video).toMatchObject({
+ width: 1080,
+ height: 1920,
+ });
+ });
+ });
+
it("leaves an asset that already has video dims untouched", async () => {
useProjectStore.setState({
projectId: "proj_test",
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 0a0e6bf03..e262d81db 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -39,7 +39,7 @@ import {
} from "../timeline/timelineMap";
import { dropTrimPillsByIds, resolveTimelineSpanToTrim } from "../timeline/trim-mapping";
import type { AutoZoomSuggestion } from "../timeline/zoom-suggestions";
-import { useProjectStore } from "./projectStore";
+import { useProjectStore, waitForDocumentSaves } from "./projectStore";
// How long a region lasts when the caller doesn't say. The timeline's toolbar
// passes its own duration instead, derived from the current zoom so the new pill
@@ -192,7 +192,13 @@ export function useTimeline() {
!probedAssetIdsRef.current.has(a.id),
);
if (missing.length === 0) return;
- let cancelled = false;
+ // No cleanup cancels this. The effect re-runs on EVERY document change, and a fresh
+ // recording changes it several times while the probe is out (placeholder seed,
+ // measured duration, camera link, auto-zoom). A cancel dropped the result while the
+ // asset was already marked attempted, so nothing ever probed it again that session and
+ // the take was exported with no dims. The write below re-reads the store instead, and
+ // the project check is the only staleness that matters.
+ const originatingProjectId = document.project.id;
void (async () => {
type Dims = { width: number; height: number };
const probed: Record<string, { video?: Dims; camera?: Dims }> = {};
@@ -211,10 +217,18 @@ export function useTimeline() {
}
if (entry.video || entry.camera) probed[a.id] = entry;
}
- if (cancelled || Object.keys(probed).length === 0) return;
+ if (Object.keys(probed).length === 0) return;
+ // The store only takes a document once its save returns, so a write still in flight
+ // (the fresh-recording auto-zooms, typically) is invisible here. Building on the store
+ // before it lands and saving after it would erase it. Wait it out; on a timeout,
+ // write nothing and let a later run probe again.
+ if ((await waitForDocumentSaves()) === "timeout") {
+ for (const id of Object.keys(probed)) probedAssetIdsRef.current.delete(id);
+ return;
+ }
// Re-read fresh state so a concurrent edit made while probing isn't stomped.
const current = useProjectStore.getState().document;
- if (!current) return;
+ if (!current || current.project.id !== originatingProjectId) return;
// `history: false` — see the comment above: a backfill nobody asked for must
// not become the thing the next Ctrl+Z reverses.
await useProjectStore.getState().saveDocument(
@@ -237,9 +251,6 @@ export function useTimeline() {
{ history: false },
);
})();
- return () => {
- cancelled = true;
- };
}, [document]);
// Backfill the real duration of imported audio assets (issue #350), the audio