Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(export): move the progress bar through every phase of an export

davbio108aa5440d65
diff --git a/src/ui/fullview/ExportPreviewModal.tsx b/src/ui/fullview/ExportPreviewModal.tsx
index ffd010a..52376e9 100644
--- a/src/ui/fullview/ExportPreviewModal.tsx
+++ b/src/ui/fullview/ExportPreviewModal.tsx
@@ -23,6 +23,13 @@ import { BUNDLE_EXTENSION } from '@/core/transfer/schema';
import { localStorage } from '@/lib/browser-api';
import { Button } from '@/ui/components/ui/button';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog';
+import {
+ encodeProgress,
+ MUX_PROGRESS_SHARE,
+ muxProgress,
+ narrateProgress,
+ VOICE_PROGRESS_SHARE,
+} from '@/ui/fullview/export-progress';
const VideoStepPlayer = lazy(() => import('@/ui/fullview/VideoStepPlayer'));
@@ -97,6 +104,8 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
const typedStepCount = steps.filter((step) => step.inputValue && screenshots.has(step.id)).length;
const { cover, stepDescriptions, resolution, screenshots: withScreenshots, stepUrls, imageScale } = options;
const voiceover = options.voiceover && voiceoverReady;
+ const voiceShare = voiceover ? VOICE_PROGRESS_SHARE : 0;
+ const muxShare = voiceover ? MUX_PROGRESS_SHARE : 0;
const previewOptions = useMemo<ExportOptions>(
() => ({ ...DEFAULT_EXPORT_OPTIONS, cover, screenshots: withScreenshots, stepUrls, imageScale, stepDescriptions }),
@@ -133,6 +142,7 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
setNarratedSeconds(null);
setVoiceoverError(null);
const timer = setTimeout(async () => {
+ let allClipsLanded = false;
try {
const { exportGuideAsVideo } = await import('@/core/export/video-export');
const {
@@ -148,10 +158,21 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
{
signal: controller.signal,
onProgress: (encoded, frames) => {
- if (!controller.signal.aborted) setVideoProgress(frames > 0 ? encoded / frames : 0);
+ if (controller.signal.aborted) return;
+ setVoiceProgress(null);
+ setVideoProgress(
+ encodeProgress(encoded, frames, allClipsLanded ? voiceShare : 0, allClipsLanded ? muxShare : 0),
+ );
},
onVoiceProgress: (done, total) => {
- if (!controller.signal.aborted) setVoiceProgress(done < total ? { done, total } : null);
+ if (controller.signal.aborted) return;
+ allClipsLanded = done === total;
+ setVoiceProgress(done < total ? { done, total } : null);
+ setVideoProgress(narrateProgress(done, total, voiceShare));
+ },
+ onMuxProgress: (done, total) => {
+ if (controller.signal.aborted) return;
+ setVideoProgress(muxProgress(done, total, muxShare));
},
},
);
@@ -177,7 +198,20 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
if (url) URL.revokeObjectURL(url);
setVideoUrl(null);
};
- }, [open, mode, guide, steps, screenshots, cover, stepDescriptions, resolution, voiceover, videoPending]);
+ }, [
+ open,
+ mode,
+ guide,
+ steps,
+ screenshots,
+ cover,
+ stepDescriptions,
+ resolution,
+ voiceover,
+ voiceShare,
+ muxShare,
+ videoPending,
+ ]);
const update = (patch: Partial<ExportOptions>) => {
const next = { ...options, ...patch };
@@ -211,6 +245,7 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
downloadAbort.current = controller;
setDownloadProgress(0);
const { exportGuideAsVideo } = await import('@/core/export/video-export');
+ let allClipsLanded = false;
const {
blob,
extension,
@@ -222,12 +257,17 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
{ ...options, voiceover },
{
signal: controller.signal,
- onProgress: (encoded, frames) => setDownloadProgress(frames > 0 ? encoded / frames : 0),
- onVoiceProgress: (done, total) => setVoiceProgress(done < total ? { done, total } : null),
+ onProgress: (encoded, frames) =>
+ setDownloadProgress(
+ encodeProgress(encoded, frames, allClipsLanded ? voiceShare : 0, allClipsLanded ? muxShare : 0),
+ ),
+ onVoiceProgress: (done, total) => {
+ allClipsLanded = done === total;
+ setDownloadProgress(narrateProgress(done, total, voiceShare));
+ },
+ onMuxProgress: (done, total) => setDownloadProgress(muxProgress(done, total, muxShare)),
},
);
- // A download that never opened the video tab must still say the narration was dropped:
- // the guide can be exported to video straight from the format list.
setVoiceoverError(failed ?? null);
downloadBlob(blob, safeFilename(guide.title, extension));
} else if (format === 'bundle') {
@@ -246,7 +286,6 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
if (!(error instanceof DOMException && error.name === 'AbortError')) throw error;
} finally {
downloadAbort.current = null;
- setVoiceProgress(null);
setExporting(null);
}
}
diff --git a/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx b/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx
index 46cc023..0b1489d 100644
--- a/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx
+++ b/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
-import { fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { DEFAULT_EXPORT_OPTIONS } from '@/core/export/options';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
@@ -191,3 +191,130 @@ describe('ExportPreviewModal voice-over controls', () => {
expect((silent() as HTMLButtonElement).disabled).toBe(false);
});
});
+
+describe('ExportPreviewModal video progress', () => {
+ const narrated = () => screen.queryByRole('button', { name: /exportPreview\.audioNarrated/ });
+ const hooks = () =>
+ exportGuideAsVideo.mock.calls.at(-1)?.[4] as {
+ onProgress: (a: number, b: number) => void;
+ onVoiceProgress: (a: number, b: number) => void;
+ onMuxProgress: (a: number, b: number) => void;
+ };
+ const percent = () => screen.getByText(/^\d+%$/).textContent;
+ const label = () => screen.getByText(/^exportPreview\.(narrating|encodingVideo)/).textContent;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ stored.value = { voiceoverProvider: 'openai', voiceoverApiKeys: { openai: 'sk-test' } };
+ canExportVideo.mockResolvedValue(true);
+ exportGuideAsHTML.mockResolvedValue('<html lang="en"><head></head><body></body></html>');
+ URL.createObjectURL = vi.fn(() => 'blob:video');
+ URL.revokeObjectURL = vi.fn();
+ exportGuideAsVideo.mockImplementation(() => new Promise(() => {}));
+ });
+
+ async function openNarratedVideo() {
+ renderModal(3);
+ await waitFor(() => expect(narrated()).not.toBeNull());
+ fireEvent.click(narrated() as HTMLElement);
+ fireEvent.click(await screen.findByRole('button', { name: 'exportPreview.modeVideo' }));
+ await waitFor(() => expect(exportGuideAsVideo).toHaveBeenCalled());
+ await waitFor(() => expect(exportGuideAsVideo.mock.calls.at(-1)?.[3].voiceover).toBe(true));
+ }
+
+ it('moves the bar while clips are synthesised instead of sitting at zero', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onVoiceProgress(0, 3));
+ expect(label()).toContain('exportPreview.narrating');
+ expect(percent()).toBe('0%');
+
+ act(() => hooks().onVoiceProgress(2, 3));
+ expect(percent()).toBe('20%');
+ });
+
+ it('stops saying it is narrating once frames start, however narration ended', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onVoiceProgress(1, 3));
+ expect(label()).toContain('exportPreview.narrating');
+
+ act(() => hooks().onProgress(1, 300));
+ expect(label()).toBe('exportPreview.encodingVideo');
+ });
+
+ it('leaves the whole bar to encoding when narration produced no clips', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onProgress(0, 300));
+ expect(percent()).toBe('0%');
+
+ act(() => hooks().onProgress(150, 300));
+ expect(percent()).toBe('50%');
+ });
+
+ it('leaves the whole bar to encoding when narration gave up partway', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onVoiceProgress(2, 3));
+ act(() => hooks().onProgress(0, 300));
+
+ expect(percent()).toBe('0%');
+ });
+
+ it('reserves the head only once every clip landed', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onVoiceProgress(3, 3));
+ act(() => hooks().onProgress(0, 300));
+
+ expect(percent()).toBe('30%');
+ });
+
+ it('holds back a tail for the audio track instead of claiming to be finished', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onVoiceProgress(3, 3));
+ act(() => hooks().onProgress(300, 300));
+
+ expect(percent()).toBe('90%');
+ });
+
+ it('fills that tail as the audio track is written', async () => {
+ await openNarratedVideo();
+
+ act(() => hooks().onVoiceProgress(3, 3));
+ act(() => hooks().onProgress(300, 300));
+ act(() => hooks().onMuxProgress(20, 40));
+
+ expect(percent()).toBe('95%');
+
+ act(() => hooks().onMuxProgress(40, 40));
+ expect(percent()).toBe('100%');
+ });
+
+ it('leaves the preview label alone while a download runs beside it', async () => {
+ await openNarratedVideo();
+ act(() => hooks().onVoiceProgress(1, 3));
+ expect(label()).toContain('exportPreview.narrating');
+ const preview = hooks();
+
+ fireEvent.click(screen.getByRole('button', { name: 'exportPreview.download[exportMenu.video]' }));
+ await waitFor(() => expect(exportGuideAsVideo).toHaveBeenCalledTimes(2));
+ act(() => hooks().onVoiceProgress(3, 3));
+ act(() => hooks().onProgress(1, 300));
+
+ expect(label()).toContain('exportPreview.narrating');
+ expect(hooks()).not.toBe(preview);
+ });
+
+ it('still reaches the end on a silent export, which writes no audio track', async () => {
+ renderModal(3);
+ fireEvent.click(await screen.findByRole('button', { name: 'exportPreview.modeVideo' }));
+ await waitFor(() => expect(exportGuideAsVideo).toHaveBeenCalled());
+
+ act(() => hooks().onProgress(300, 300));
+
+ expect(percent()).toBe('100%');
+ });
+});
diff --git a/src/ui/fullview/__tests__/export-progress.test.ts b/src/ui/fullview/__tests__/export-progress.test.ts
new file mode 100644
index 0000000..d72cc78
--- /dev/null
+++ b/src/ui/fullview/__tests__/export-progress.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it } from 'vitest';
+import {
+ encodeProgress,
+ MUX_PROGRESS_SHARE,
+ muxProgress,
+ narrateProgress,
+ VOICE_PROGRESS_SHARE,
+} from '@/ui/fullview/export-progress';
+
+describe('VOICE_PROGRESS_SHARE', () => {
+ it('leaves narration a visible head of the bar and encoding the majority', () => {
+ expect(VOICE_PROGRESS_SHARE).toBe(0.3);
+ });
+});
+
+describe('narrateProgress', () => {
+ it('fills only the head of the bar, since encoding still has to run', () => {
+ expect(narrateProgress(0, 10, 0.3)).toBe(0);
+ expect(narrateProgress(5, 10, 0.3)).toBeCloseTo(0.15, 6);
+ expect(narrateProgress(10, 10, 0.3)).toBeCloseTo(0.3, 6);
+ });
+
+ it('stays at zero when there is nothing to synthesise, rather than dividing by zero', () => {
+ expect(narrateProgress(0, 0, 0.3)).toBe(0);
+ });
+});
+
+describe('MUX_PROGRESS_SHARE', () => {
+ it('keeps a slice back for writing the audio track, which runs after the last frame', () => {
+ expect(MUX_PROGRESS_SHARE).toBe(0.1);
+ });
+});
+
+describe('encodeProgress', () => {
+ it('starts where narration left off so the bar never goes backwards', () => {
+ expect(encodeProgress(0, 300, 0.3)).toBeCloseTo(0.3, 6);
+ expect(encodeProgress(150, 300, 0.3)).toBeCloseTo(0.65, 6);
+ expect(encodeProgress(300, 300, 0.3)).toBeCloseTo(1, 6);
+ });
+
+ it('owns the whole bar when nothing was narrated', () => {
+ expect(encodeProgress(0, 300, 0)).toBe(0);
+ expect(encodeProgress(150, 300, 0)).toBeCloseTo(0.5, 6);
+ expect(encodeProgress(300, 300, 0)).toBeCloseTo(1, 6);
+ });
+
+ it('stops short of the end when an audio track still has to be written', () => {
+ expect(encodeProgress(300, 300, 0.3, 0.1)).toBeCloseTo(0.9, 6);
+ expect(encodeProgress(150, 300, 0.3, 0.1)).toBeCloseTo(0.6, 6);
+ });
+});
+
+describe('muxProgress', () => {
+ it('fills the tail the encode left behind', () => {
+ expect(muxProgress(0, 40, 0.1)).toBeCloseTo(0.9, 6);
+ expect(muxProgress(20, 40, 0.1)).toBeCloseTo(0.95, 6);
+ expect(muxProgress(40, 40, 0.1)).toBeCloseTo(1, 6);
+ });
+
+ it('picks up exactly where a narrated encode stopped', () => {
+ expect(muxProgress(0, 40, 0.1)).toBeCloseTo(encodeProgress(300, 300, 0.3, 0.1), 6);
+ });
+});
diff --git a/src/ui/fullview/export-progress.ts b/src/ui/fullview/export-progress.ts
new file mode 100644
index 0000000..9c932f2
--- /dev/null
+++ b/src/ui/fullview/export-progress.ts
@@ -0,0 +1,15 @@
+export const VOICE_PROGRESS_SHARE = 0.3;
+
+export const MUX_PROGRESS_SHARE = 0.1;
+
+export function narrateProgress(done: number, total: number, share: number): number {
+ return total > 0 ? (done / total) * share : 0;
+}
+
+export function encodeProgress(encoded: number, frames: number, share: number, tail = 0): number {
+ return share + (frames > 0 ? encoded / frames : 0) * (1 - share - tail);
+}
+
+export function muxProgress(done: number, total: number, tail: number): number {
+ return 1 - tail + (total > 0 ? done / total : 0) * tail;
+}