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(editor): prevent audio track slider jump on release

EtienneLescot9afb6d80495d
diff --git a/src/components/ai-edition/RightPanes.audioTrack.test.tsx b/src/components/ai-edition/RightPanes.audioTrack.test.tsx
index bcc534366..595518b8e 100644
--- a/src/components/ai-edition/RightPanes.audioTrack.test.tsx
+++ b/src/components/ai-edition/RightPanes.audioTrack.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom";
-import { fireEvent, render, screen } from "@testing-library/react";
+import { act, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { I18nProvider } from "@/contexts/I18nContext";
import { LOCALE_STORAGE_KEY } from "@/i18n/config";
@@ -201,4 +201,148 @@ describe("AudioTrackPane reset button", () => {
expect(fadeInSlider).toHaveValue("0");
expect(fadeOutSlider).toHaveValue("0");
});
+
+ it("retains slider value on release while async commit is in flight without jumping back", async () => {
+ let resolveGain: () => void = () => undefined;
+ let resolveFadeIn: () => void = () => undefined;
+ let resolveFadeOut: () => void = () => undefined;
+
+ const setAudioTrackGain = vi.fn().mockImplementation(
+ () =>
+ new Promise<void>((res) => {
+ resolveGain = res;
+ }),
+ );
+ const updateAudioTrack = vi.fn().mockImplementation(
+ (_id: string, patch: { fadeInMs?: number; fadeOutMs?: number }) =>
+ new Promise<void>((res) => {
+ if (patch.fadeInMs !== undefined) resolveFadeIn = res;
+ if (patch.fadeOutMs !== undefined) resolveFadeOut = res;
+ }),
+ );
+
+ const mockTrack: AxcutAudioTrack = {
+ id: "audio_track_1",
+ clipId: "clip_1",
+ assetId: "asset_audio_1",
+ trackId: "audio_track_1",
+ startMs: 1000,
+ endMs: 5000,
+ durationSec: 10,
+ offsetMs: 0,
+ gainDb: -6,
+ fadeInMs: 500,
+ fadeOutMs: 1000,
+ muted: false,
+ loop: false,
+ kind: "music",
+ label: "test-audio.mp3",
+ origin: "user",
+ };
+
+ const tl = {
+ selectedAudioTrackId: "audio_track_1",
+ audioTracks: [mockTrack],
+ assets: [
+ {
+ id: "asset_audio_1",
+ kind: "audio",
+ label: "test-audio.mp3",
+ originalPath: "/path/test-audio.mp3",
+ durationSec: 10,
+ },
+ ],
+ setAudioTrackGain,
+ updateAudioTrack,
+ } as unknown as TimelineApi;
+
+ const { rerender } = render(
+ <I18nProvider>
+ <AudioTrackPane tl={tl} />
+ </I18nProvider>,
+ );
+
+ const gainSlider = screen.getByRole("slider", { name: "Output level" });
+ const fadeInSlider = screen.getByRole("slider", { name: "Fade in" });
+ const fadeOutSlider = screen.getByRole("slider", { name: "Fade out" });
+
+ // 1. Gain slider: drag to 2.5 and release (onMouseUp)
+ fireEvent.change(gainSlider, { target: { value: "2.5" } });
+ fireEvent.mouseUp(gainSlider);
+
+ expect(setAudioTrackGain).toHaveBeenCalledWith("audio_track_1", 2.5);
+ // While save is in flight, the slider MUST NOT jump back to -6
+ expect(gainSlider).toHaveValue("2.5");
+
+ // Complete the async save and simulate the store update
+ await act(async () => {
+ resolveGain();
+ });
+
+ rerender(
+ <I18nProvider>
+ <AudioTrackPane
+ tl={
+ {
+ ...tl,
+ audioTracks: [{ ...mockTrack, gainDb: 2.5 }],
+ } as unknown as TimelineApi
+ }
+ />
+ </I18nProvider>,
+ );
+ expect(gainSlider).toHaveValue("2.5");
+
+ // 2. Fade in slider: drag to 1500 and release
+ fireEvent.change(fadeInSlider, { target: { value: "1500" } });
+ fireEvent.mouseUp(fadeInSlider);
+
+ expect(updateAudioTrack).toHaveBeenCalledWith("audio_track_1", { fadeInMs: 1500 });
+ // While save is in flight, the slider MUST NOT jump back to 500
+ expect(fadeInSlider).toHaveValue("1500");
+
+ await act(async () => {
+ resolveFadeIn();
+ });
+
+ rerender(
+ <I18nProvider>
+ <AudioTrackPane
+ tl={
+ {
+ ...tl,
+ audioTracks: [{ ...mockTrack, gainDb: 2.5, fadeInMs: 1500 }],
+ } as unknown as TimelineApi
+ }
+ />
+ </I18nProvider>,
+ );
+ expect(fadeInSlider).toHaveValue("1500");
+
+ // 3. Fade out slider: drag to 2500 and release
+ fireEvent.change(fadeOutSlider, { target: { value: "2500" } });
+ fireEvent.mouseUp(fadeOutSlider);
+
+ expect(updateAudioTrack).toHaveBeenCalledWith("audio_track_1", { fadeOutMs: 2500 });
+ // While save is in flight, the slider MUST NOT jump back to 1000
+ expect(fadeOutSlider).toHaveValue("2500");
+
+ await act(async () => {
+ resolveFadeOut();
+ });
+
+ rerender(
+ <I18nProvider>
+ <AudioTrackPane
+ tl={
+ {
+ ...tl,
+ audioTracks: [{ ...mockTrack, gainDb: 2.5, fadeInMs: 1500, fadeOutMs: 2500 }],
+ } as unknown as TimelineApi
+ }
+ />
+ </I18nProvider>,
+ );
+ expect(fadeOutSlider).toHaveValue("2500");
+ });
});
diff --git a/src/components/ai-edition/RightPanes.tsx b/src/components/ai-edition/RightPanes.tsx
index cb80a15d2..ca70e8885 100644
--- a/src/components/ai-edition/RightPanes.tsx
+++ b/src/components/ai-edition/RightPanes.tsx
@@ -3118,9 +3118,15 @@ export function AudioTrackPane({ tl }: { tl: TimelineApi }) {
decimals={1}
suffix=" dB"
onChange={(value) => setLiveGain(value)}
- onCommit={() => {
- if (liveGain !== null) void tl.setAudioTrackGain(track.id, liveGain);
- setLiveGain(null);
+ onCommit={async () => {
+ if (liveGain !== null) {
+ const target = liveGain;
+ try {
+ await tl.setAudioTrackGain(track.id, target);
+ } finally {
+ setLiveGain((current) => (current === target ? null : current));
+ }
+ }
}}
/>
<SliderCell
@@ -3132,9 +3138,15 @@ export function AudioTrackPane({ tl }: { tl: TimelineApi }) {
decimals={0}
suffix=" ms"
onChange={setLiveFadeIn}
- onCommit={() => {
- if (liveFadeIn !== null) void tl.updateAudioTrack(track.id, { fadeInMs: liveFadeIn });
- setLiveFadeIn(null);
+ onCommit={async () => {
+ if (liveFadeIn !== null) {
+ const target = liveFadeIn;
+ try {
+ await tl.updateAudioTrack(track.id, { fadeInMs: target });
+ } finally {
+ setLiveFadeIn((current) => (current === target ? null : current));
+ }
+ }
}}
/>
<SliderCell
@@ -3146,10 +3158,15 @@ export function AudioTrackPane({ tl }: { tl: TimelineApi }) {
decimals={0}
suffix=" ms"
onChange={setLiveFadeOut}
- onCommit={() => {
- if (liveFadeOut !== null)
- void tl.updateAudioTrack(track.id, { fadeOutMs: liveFadeOut });
- setLiveFadeOut(null);
+ onCommit={async () => {
+ if (liveFadeOut !== null) {
+ const target = liveFadeOut;
+ try {
+ await tl.updateAudioTrack(track.id, { fadeOutMs: target });
+ } finally {
+ setLiveFadeOut((current) => (current === target ? null : current));
+ }
+ }
}}
/>
</div>