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 reviewed editor async and ref flows

Vitali Gusatinsky19a078829320
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index 1f22da311..d942a8a7f 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -394,10 +394,14 @@ export function NewEditorShell() {
);
const handleDropAsset = useCallback(
- (assetId: string) => {
- void tl.insertClipAt(assetId, clips.length);
- },
- [tl, clips.length],
+ (assetId: string) =>
+ tl.insertClipAt(assetId, clips.length).catch((error) => {
+ toast.error(te("mediaStage.couldNotAddAsset"), {
+ description: error instanceof Error ? error.message : String(error),
+ });
+ throw error;
+ }),
+ [tl, clips.length, te],
);
// Ref so the 'ended' listener below always sees the latest clips without tearing
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 30bc1aa02..0f3691959 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -162,7 +162,9 @@ export function VirtualPreview({
}: VirtualPreviewProps) {
const { settings } = useEditorSettings();
const audioOffsetMsRef = useRef(settings.audioOffsetMs);
- audioOffsetMsRef.current = settings.audioOffsetMs;
+ useEffect(() => {
+ audioOffsetMsRef.current = settings.audioOffsetMs;
+ }, [settings.audioOffsetMs]);
// ponytail: an oversized, offset video inside .videoFrame's overflow:hidden
// box — the same "scale + negative-position the full frame, let the
// container clip the rest" technique the export renderer uses via a Pixi
@@ -194,10 +196,12 @@ export function VirtualPreview({
autoMaster: settings.audioAutoMaster,
gainDb: settings.audioGainDb,
});
- audioSettingsRef.current = {
- autoMaster: settings.audioAutoMaster,
- gainDb: settings.audioGainDb,
- };
+ useEffect(() => {
+ audioSettingsRef.current = {
+ autoMaster: settings.audioAutoMaster,
+ gainDb: settings.audioGainDb,
+ };
+ }, [settings.audioAutoMaster, settings.audioGainDb]);
const audioContextRef = useRef<AudioContext | null>(null);
const audioContextCloseTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const audioSourceNodesRef = useRef(new WeakMap<HTMLAudioElement, MediaElementAudioSourceNode>());
diff --git a/src/components/ai-edition/v4/MediaStage.test.ts b/src/components/ai-edition/v4/MediaStage.test.ts
index fe457c243..cb2d79540 100644
--- a/src/components/ai-edition/v4/MediaStage.test.ts
+++ b/src/components/ai-edition/v4/MediaStage.test.ts
@@ -2,11 +2,11 @@ import { describe, expect, it, vi } from "vitest";
import { addSelectedAssetToTimeline } from "./MediaStage";
describe("addSelectedAssetToTimeline", () => {
- it("forwards the selected asset ID and displayed fallback name", () => {
- const onAdd = vi.fn();
+ it("reports success after the selected asset is added", async () => {
+ const onAdd = vi.fn(async () => undefined);
const onSuccess = vi.fn();
- addSelectedAssetToTimeline(
+ await addSelectedAssetToTimeline(
{ id: "asset-7", label: "", originalPath: "/recordings/demo.mp4" },
onAdd,
onSuccess,
@@ -16,11 +16,29 @@ describe("addSelectedAssetToTimeline", () => {
expect(onSuccess).toHaveBeenCalledWith("demo.mp4");
});
- it("does nothing without a selected asset", () => {
- const onAdd = vi.fn();
+ it("does not report success when adding the asset fails", async () => {
+ const error = new Error("insert failed");
+ const onAdd = vi.fn(async () => {
+ throw error;
+ });
const onSuccess = vi.fn();
- addSelectedAssetToTimeline(null, onAdd, onSuccess);
+ await expect(
+ addSelectedAssetToTimeline(
+ { id: "asset-7", label: "Demo", originalPath: "/recordings/demo.mp4" },
+ onAdd,
+ onSuccess,
+ ),
+ ).rejects.toBe(error);
+
+ expect(onSuccess).not.toHaveBeenCalled();
+ });
+
+ it("does nothing without a selected asset", async () => {
+ const onAdd = vi.fn(async () => undefined);
+ const onSuccess = vi.fn();
+
+ await addSelectedAssetToTimeline(null, onAdd, onSuccess);
expect(onAdd).not.toHaveBeenCalled();
expect(onSuccess).not.toHaveBeenCalled();
diff --git a/src/components/ai-edition/v4/MediaStage.tsx b/src/components/ai-edition/v4/MediaStage.tsx
index e15dc1c2e..e69ff3452 100644
--- a/src/components/ai-edition/v4/MediaStage.tsx
+++ b/src/components/ai-edition/v4/MediaStage.tsx
@@ -33,17 +33,21 @@ function basename(path: string): string {
return path.split(/[\\/]/).pop() ?? path;
}
-export function addSelectedAssetToTimeline(
+export async function addSelectedAssetToTimeline(
selected: Pick<AxcutAsset, "id" | "label" | "originalPath"> | null,
- onAddToTimeline: (assetId: string) => void,
+ onAddToTimeline: (assetId: string) => Promise<void>,
onSuccess: (label: string) => void,
-): void {
+): Promise<void> {
if (!selected) return;
- onAddToTimeline(selected.id);
+ await onAddToTimeline(selected.id);
onSuccess(selected.label || basename(selected.originalPath));
}
-export function MediaStage({ onAddToTimeline }: { onAddToTimeline: (assetId: string) => void }) {
+export function MediaStage({
+ onAddToTimeline,
+}: {
+ onAddToTimeline: (assetId: string) => Promise<void>;
+}) {
const t = useScopedT("editor");
const projectId = useProjectStore((s) => s.projectId);
const document = useProjectStore((s) => s.document);
@@ -106,9 +110,9 @@ export function MediaStage({ onAddToTimeline }: { onAddToTimeline: (assetId: str
};
const addSelectedToTimeline = () => {
- addSelectedAssetToTimeline(selected, onAddToTimeline, (label) => {
+ void addSelectedAssetToTimeline(selected, onAddToTimeline, (label) => {
toast.success(t("mediaStage.addedToTimeline", { label }));
- });
+ }).catch(() => undefined);
};
return (
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index ba549180f..1ab58ac88 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -361,7 +361,7 @@ export function V4Timeline({
tl: TimelineApi;
setCurrentTime: (sec: number) => void;
variant?: "edit" | "media";
- onDropAsset?: (assetId: string) => void;
+ onDropAsset?: (assetId: string) => Promise<void>;
videoSources?: VideoSource[];
playing: boolean;
onTogglePlay: () => void;
@@ -1549,7 +1549,7 @@ export function V4Timeline({
e.preventDefault();
setDragOver(false);
const id = e.dataTransfer.getData(ASSET_MIME);
- if (id && onDropAsset) onDropAsset(id);
+ if (id && onDropAsset) void onDropAsset(id).catch(() => undefined);
}}
>
{clips.map((c, i) => {