Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: derive useVideoFileSrc's resolving flag synchronously (#2702)

[._.]/ Adam Eivy7cd3be127a16
diff --git a/client/src/hooks/useVideoFileSrc.js b/client/src/hooks/useVideoFileSrc.js
index a905dcaae7..68273cbd39 100644
--- a/client/src/hooks/useVideoFileSrc.js
+++ b/client/src/hooks/useVideoFileSrc.js
@@ -31,32 +31,35 @@ import { listVideoHistory } from '../services/apiImageVideo.js';
* so a card resolves only once the user actually presses play.
*/
export function useVideoFileSrc(jobId, { enabled = true } = {}) {
- const [src, setSrc] = useState(null);
- const [resolving, setResolving] = useState(false);
+ // Keyed by the id it was resolved FOR, so `resolving` can be derived
+ // synchronously rather than set from an effect. An effect-set flag would
+ // still be false for the first render after `enabled` flips true (effects run
+ // after commit) — long enough for a caller gating autoplay to mount a player
+ // against the unresolved fallback path, fire a doomed request, and flash
+ // "media missing" before the real src lands.
+ const [resolved, setResolved] = useState({ jobId: null, src: null });
+ const active = Boolean(enabled && jobId);
+ const settled = resolved.jobId === jobId;
+ const resolving = active && !settled;
useEffect(() => {
- setSrc(null);
- if (!enabled || !jobId) {
- setResolving(false);
- return undefined;
- }
- setResolving(true);
+ if (!active || settled) return undefined;
let cancelled = false;
// Silent: a failed lookup is not a user-facing error — ScenePreview's
// reconstruction fallback (and its own missing-media UI) covers it, so a
// toast here would be noise on a page that already degrades gracefully.
+ // Both paths settle on THIS jobId so `resolving` can never latch on.
listVideoHistory({ silent: true })
.then((entries) => {
if (cancelled) return;
const list = Array.isArray(entries) ? entries : [];
const entry = list.find((e) => e?.id === jobId);
const filename = typeof entry?.filename === 'string' ? entry.filename.trim() : '';
- if (filename) setSrc(`/data/videos/${filename}`);
+ setResolved({ jobId, src: filename ? `/data/videos/${filename}` : null });
})
- .catch(() => {})
- .finally(() => { if (!cancelled) setResolving(false); });
+ .catch(() => { if (!cancelled) setResolved({ jobId, src: null }); });
return () => { cancelled = true; };
- }, [jobId, enabled]);
+ }, [jobId, active, settled]);
- return { src, resolving };
+ return { src: settled ? resolved.src : null, resolving };
}
diff --git a/client/src/hooks/useVideoFileSrc.test.js b/client/src/hooks/useVideoFileSrc.test.js
new file mode 100644
index 0000000000..1cc4114c43
--- /dev/null
+++ b/client/src/hooks/useVideoFileSrc.test.js
@@ -0,0 +1,106 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { renderHook, waitFor, act } from '@testing-library/react';
+
+const listVideoHistory = vi.fn();
+vi.mock('../services/apiImageVideo.js', () => ({
+ listVideoHistory: (...args) => listVideoHistory(...args),
+}));
+
+const { useVideoFileSrc } = await import('./useVideoFileSrc.js');
+
+// Obviously-fake entries. The two shapes that matter: a timeline render, whose
+// filename is unrelated to its id, and a clip render, whose filename happens to
+// be `<id>.mp4`.
+const HISTORY = [
+ { id: 'final-1', filename: 'timeline-abcd1234-1700000000000.mp4' },
+ { id: 'scene-1', filename: 'scene-1.mp4' },
+];
+
+beforeEach(() => {
+ listVideoHistory.mockReset();
+ listVideoHistory.mockResolvedValue(HISTORY);
+});
+
+describe('useVideoFileSrc', () => {
+ it('resolves a timeline id to its real, unrelated filename', async () => {
+ const { result } = renderHook(() => useVideoFileSrc('final-1'));
+ await waitFor(() => expect(result.current.resolving).toBe(false));
+ expect(result.current.src).toBe('/data/videos/timeline-abcd1234-1700000000000.mp4');
+ });
+
+ it('reports `resolving` synchronously on the first render, before any effect runs', async () => {
+ // The regression this guards: an effect-set flag is false on the first
+ // render, so a caller gating autoplay would mount a player for one frame
+ // against the unresolved path and fire a doomed request.
+ const { result } = renderHook(() => useVideoFileSrc('final-1'));
+ expect(result.current.resolving).toBe(true);
+ expect(result.current.src).toBeNull();
+ await act(async () => {}); // settle the in-flight lookup
+ });
+
+ it('reports `resolving` synchronously the moment `enabled` flips true', async () => {
+ const { result, rerender } = renderHook(
+ ({ enabled }) => useVideoFileSrc('final-1', { enabled }),
+ { initialProps: { enabled: false } },
+ );
+ expect(result.current.resolving).toBe(false);
+ expect(listVideoHistory).not.toHaveBeenCalled();
+
+ rerender({ enabled: true });
+ expect(result.current.resolving).toBe(true); // synchronous, not after commit
+ await waitFor(() => expect(result.current.resolving).toBe(false));
+ expect(result.current.src).toBe('/data/videos/timeline-abcd1234-1700000000000.mp4');
+ });
+
+ it('never fetches while disabled — the grid must stay light', () => {
+ renderHook(() => useVideoFileSrc('final-1', { enabled: false }));
+ expect(listVideoHistory).not.toHaveBeenCalled();
+ });
+
+ it('does not fetch without a jobId', () => {
+ const { result } = renderHook(() => useVideoFileSrc(null));
+ expect(listVideoHistory).not.toHaveBeenCalled();
+ expect(result.current.resolving).toBe(false);
+ expect(result.current.src).toBeNull();
+ });
+
+ it('settles with a null src for an id missing from history (deleted media)', async () => {
+ const { result } = renderHook(() => useVideoFileSrc('gone-1'));
+ await waitFor(() => expect(result.current.resolving).toBe(false));
+ // Null, not a guess — the caller falls back to ScenePreview's own
+ // reconstruction + missing-media UI.
+ expect(result.current.src).toBeNull();
+ });
+
+ it('settles instead of latching when the lookup fails', async () => {
+ listVideoHistory.mockRejectedValue(new Error('network down'));
+ const { result } = renderHook(() => useVideoFileSrc('final-1'));
+ await waitFor(() => expect(result.current.resolving).toBe(false));
+ expect(result.current.src).toBeNull();
+ });
+
+ it('tolerates a non-array history payload', async () => {
+ listVideoHistory.mockResolvedValue({ oops: true });
+ const { result } = renderHook(() => useVideoFileSrc('final-1'));
+ await waitFor(() => expect(result.current.resolving).toBe(false));
+ expect(result.current.src).toBeNull();
+ });
+
+ it('re-resolves when the jobId changes', async () => {
+ const { result, rerender } = renderHook(({ id }) => useVideoFileSrc(id), {
+ initialProps: { id: 'final-1' },
+ });
+ await waitFor(() => expect(result.current.src).toBe('/data/videos/timeline-abcd1234-1700000000000.mp4'));
+
+ rerender({ id: 'scene-1' });
+ // Must not keep serving the previous id's file while the new one resolves.
+ expect(result.current.src).toBeNull();
+ await waitFor(() => expect(result.current.src).toBe('/data/videos/scene-1.mp4'));
+ });
+
+ it('requests silently — the caller owns the failure UI', async () => {
+ const { result } = renderHook(() => useVideoFileSrc('final-1'));
+ await waitFor(() => expect(result.current.resolving).toBe(false));
+ expect(listVideoHistory).toHaveBeenCalledWith({ silent: true });
+ });
+});