From 50f4ccd4d5d14e56c4052242dd7ca2d4410bddc0 Mon Sep 17 00:00:00 2001 From: Reece Browne Date: Tue, 19 Aug 2025 21:35:31 +0100 Subject: [PATCH] Fix loop --- frontend/src/contexts/FileContext.tsx | 84 ++++++++++------------- frontend/src/contexts/file/fileActions.ts | 9 +-- 2 files changed, 36 insertions(+), 57 deletions(-) diff --git a/frontend/src/contexts/FileContext.tsx b/frontend/src/contexts/FileContext.tsx index 136585f53..421c78e33 100644 --- a/frontend/src/contexts/FileContext.tsx +++ b/frontend/src/contexts/FileContext.tsx @@ -74,18 +74,7 @@ function FileContextInner({ // File operations using unified addFiles helper with persistence const addRawFiles = useCallback(async (files: File[]): Promise => { - // Get IndexedDB metadata for comprehensive deduplication - let indexedDBMetadata: Array<{ name: string; size: number; lastModified: number }> | undefined; - if (indexedDB && enablePersistence) { - try { - const metadata = await indexedDB.loadAllMetadata(); - indexedDBMetadata = metadata.map(m => ({ name: m.name, size: m.size, lastModified: m.lastModified })); - } catch (error) { - console.warn('Failed to load IndexedDB metadata for deduplication:', error); - } - } - - const addedFilesWithIds = await addFiles('raw', { files }, stateRef, filesRef, dispatch, indexedDBMetadata); + const addedFilesWithIds = await addFiles('raw', { files }, stateRef, filesRef, dispatch); // Persist to IndexedDB if enabled - pass existing thumbnail to prevent double generation if (indexedDB && enablePersistence && addedFilesWithIds.length > 0) { @@ -193,12 +182,37 @@ function FileContextInner({ trackPdfDocument: lifecycleManager.trackPdfDocument, cleanupFile: (fileId: string) => lifecycleManager.cleanupFile(fileId, stateRef), scheduleCleanup: (fileId: string, delay?: number) => - lifecycleManager.scheduleCleanup(fileId, delay, stateRef), + lifecycleManager.scheduleCleanup(fileId, delay, stateRef) + }), [ + baseActions, + addRawFiles, + addProcessedFiles, + addStoredFiles, + lifecycleManager, + setHasUnsavedChanges, + consumeFilesWrapper, + pinFileWrapper, + unpinFileWrapper, + indexedDB, + enablePersistence + ]); + + // Split context values to minimize re-renders + const stateValue = useMemo(() => ({ + state, + selectors + }), [state, selectors]); + + const actionsValue = useMemo(() => ({ + actions, + dispatch + }), [actions]); + + // Load files from persistence on mount + useEffect(() => { + if (!enablePersistence || !indexedDB) return; - // Persistence operations - load file list from IndexedDB - loadFromPersistence: async () => { - if (!indexedDB || !enablePersistence) return; - + const loadFromPersistence = async () => { try { // Load metadata to populate file list (actual File objects loaded on-demand) const metadata = await indexedDB.loadAllMetadata(); @@ -232,38 +246,10 @@ function FileContextInner({ } catch (error) { console.error('Failed to load files from persistence:', error); } - } - }), [ - baseActions, - addRawFiles, - addProcessedFiles, - addStoredFiles, - lifecycleManager, - setHasUnsavedChanges, - consumeFilesWrapper, - pinFileWrapper, - unpinFileWrapper, - indexedDB, - enablePersistence - ]); - - // Split context values to minimize re-renders - const stateValue = useMemo(() => ({ - state, - selectors - }), [state, selectors]); - - const actionsValue = useMemo(() => ({ - actions, - dispatch - }), [actions]); - - // Load files from persistence on mount - useEffect(() => { - if (enablePersistence && indexedDB) { - actions.loadFromPersistence(); - } - }, [enablePersistence, indexedDB]); // Only run once on mount + }; + + loadFromPersistence(); + }, [enablePersistence, indexedDB]); // Only run when these change // Cleanup on unmount useEffect(() => { diff --git a/frontend/src/contexts/file/fileActions.ts b/frontend/src/contexts/file/fileActions.ts index cc251f7bf..f9b848841 100644 --- a/frontend/src/contexts/file/fileActions.ts +++ b/frontend/src/contexts/file/fileActions.ts @@ -59,8 +59,7 @@ export async function addFiles( options: AddFileOptions, stateRef: React.MutableRefObject, filesRef: React.MutableRefObject>, - dispatch: React.Dispatch, - indexedDBMetadata?: Array<{ name: string; size: number; lastModified: number }> + dispatch: React.Dispatch ): Promise> { const fileRecords: FileRecord[] = []; const addedFiles: Array<{ file: File; id: FileId; thumbnail?: string }> = []; @@ -68,12 +67,6 @@ export async function addFiles( // Build quickKey lookup from existing files for deduplication const existingQuickKeys = buildQuickKeySet(stateRef.current.files.byId); - // Add IndexedDB quickKeys if metadata provided for comprehensive deduplication - if (indexedDBMetadata) { - const indexedDBQuickKeys = buildQuickKeySetFromMetadata(indexedDBMetadata); - indexedDBQuickKeys.forEach(key => existingQuickKeys.add(key)); - } - switch (kind) { case 'raw': { const { files = [] } = options;