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: flush timeline autosave on exit (#8095)

[._.]/ Adam Eivy501d9af433d9
diff --git a/client/src/pages/VideoTimelineEditor.jsx b/client/src/pages/VideoTimelineEditor.jsx
index b75d77ffe0..b9f449d813 100644
--- a/client/src/pages/VideoTimelineEditor.jsx
+++ b/client/src/pages/VideoTimelineEditor.jsx
@@ -307,20 +307,43 @@ export default function VideoTimelineEditor() {
updatedAtRef.current = updated.updatedAt;
setProject((p) => ({ ...p, updatedAt: updated.updatedAt }));
return true;
+ }).finally(() => {
+ if (pendingLanesRef.current === next) pendingLanesRef.current = null;
}), [projectId, refresh, queueWrite]);
// Debounced save: trim/fade edits fire many PATCHes per drag if we don't
// batch them. 400ms gives the user time to stop fiddling before we hit the
// server.
const saveTimerRef = useRef(null);
+ const pendingLanesRef = useRef(null);
const queueSave = useCallback((next) => {
clearTimeout(saveTimerRef.current);
- saveTimerRef.current = setTimeout(() => saveTimeline(next), 400);
+ pendingLanesRef.current = next;
+ saveTimerRef.current = setTimeout(() => {
+ saveTimerRef.current = null;
+ saveTimeline(next);
+ }, 400);
}, [saveTimeline]);
- // Drop any pending debounced save when the editor unmounts so a stale
- // timeout doesn't fire after navigation.
- useEffect(() => () => clearTimeout(saveTimerRef.current), []);
+ // Keep the browser's native leave prompt active only while edits await save.
+ useEffect(() => {
+ const preventLeavingWithPendingSave = (event) => {
+ if (pendingLanesRef.current == null) return;
+ event.preventDefault();
+ event.returnValue = '';
+ };
+ window.addEventListener('beforeunload', preventLeavingWithPendingSave);
+ return () => window.removeEventListener('beforeunload', preventLeavingWithPendingSave);
+ }, []);
+
+ // Navigation can unmount the editor before the debounce fires. Flush the
+ // latest lanes so the final edit is persisted instead of silently dropped.
+ useEffect(() => () => {
+ if (saveTimerRef.current == null) return;
+ clearTimeout(saveTimerRef.current);
+ saveTimerRef.current = null;
+ if (pendingLanesRef.current != null) saveTimeline(pendingLanesRef.current);
+ }, [saveTimeline]);
const updateLanes = useCallback((updater) => {
setLanes((prev) => {
diff --git a/client/src/pages/VideoTimelineEditor.test.jsx b/client/src/pages/VideoTimelineEditor.test.jsx
index e8332dc92e..9a747a6ee2 100644
--- a/client/src/pages/VideoTimelineEditor.test.jsx
+++ b/client/src/pages/VideoTimelineEditor.test.jsx
@@ -38,6 +38,7 @@ const api = vi.hoisted(() => ({
gallery: [],
music: { tracks: [] },
galleryThrows: false,
+ updateTimelineProject: vi.fn(async () => ({ updatedAt: 'u2' })),
}));
vi.mock('../services/api', () => ({
@@ -48,7 +49,7 @@ vi.mock('../services/api', () => ({
return api.gallery.filter(row => filenames.includes(row.filename));
},
listMusicLibrary: async () => api.music,
- updateTimelineProject: async () => ({ updatedAt: 'u2' }),
+ updateTimelineProject: (...args) => api.updateTimelineProject(...args),
renderTimelineProject: async () => ({ jobId: 'j1' }),
}));
@@ -84,6 +85,7 @@ beforeEach(() => {
api.gallery = [{ filename: 'plate.png' }, { filename: 'logo.png' }];
api.music = { tracks: [{ filename: 'bed.mp3', label: 'Bed' }] };
api.galleryThrows = false;
+ api.updateTimelineProject.mockClear();
});
const renderEditor = async () => {
@@ -91,6 +93,45 @@ const renderEditor = async () => {
await awaitPageLoaded('Loading timeline project');
};
+describe('pending lane saves', () => {
+ it('flushes the latest edit exactly once when unmounted before the debounce', async () => {
+ const view = render(<VideoTimelineEditor />);
+ await awaitPageLoaded('Loading timeline project');
+
+ fireEvent.click(screen.getByRole('button', { name: 'Add to timeline' }));
+ view.unmount();
+
+ await waitFor(() => expect(api.updateTimelineProject).toHaveBeenCalledTimes(1));
+ expect(api.updateTimelineProject.mock.calls[0][1].segments).toHaveLength(1);
+ expect(api.updateTimelineProject.mock.calls[0][1].segments[0].clipId).toBe(CLIP_A);
+ });
+
+ it('does not save on unmount when no lane edit is pending', async () => {
+ const view = render(<VideoTimelineEditor />);
+ await awaitPageLoaded('Loading timeline project');
+
+ view.unmount();
+
+ expect(api.updateTimelineProject).not.toHaveBeenCalled();
+ });
+
+ it('prevents unload only while a debounced save is pending', async () => {
+ const view = render(<VideoTimelineEditor />);
+ await awaitPageLoaded('Loading timeline project');
+ fireEvent.click(screen.getByRole('button', { name: 'Add to timeline' }));
+
+ const pendingUnload = new Event('beforeunload', { cancelable: true });
+ window.dispatchEvent(pendingUnload);
+ expect(pendingUnload.defaultPrevented).toBe(true);
+
+ await waitFor(() => expect(api.updateTimelineProject).toHaveBeenCalledTimes(1), { timeout: 1500 });
+ const savedUnload = new Event('beforeunload', { cancelable: true });
+ window.dispatchEvent(savedUnload);
+ expect(savedUnload.defaultPrevented).toBe(false);
+ view.unmount();
+ });
+});
+
describe('lane visibility', () => {
it('shows the empty-timeline hint only when EVERY lane is empty', async () => {
await renderEditor();