From 7247edc029efba7f52250cd29786ebfbff2d6bcb Mon Sep 17 00:00:00 2001 From: Reece Browne Date: Wed, 20 Aug 2025 23:17:17 +0100 Subject: [PATCH] clean up --- frontend/src/contexts/FileContext.tsx | 4 +- frontend/src/contexts/IndexedDBContext.tsx | 110 +-------------------- frontend/src/contexts/file/fileActions.ts | 5 +- 3 files changed, 9 insertions(+), 110 deletions(-) diff --git a/frontend/src/contexts/FileContext.tsx b/frontend/src/contexts/FileContext.tsx index 2c8b0bada..c382cce78 100644 --- a/frontend/src/contexts/FileContext.tsx +++ b/frontend/src/contexts/FileContext.tsx @@ -25,7 +25,7 @@ import { // Import modular components import { fileContextReducer, initialFileContextState } from './file/FileReducer'; -import { createFileSelectors, buildQuickKeySetFromMetadata } from './file/fileSelectors'; +import { createFileSelectors } from './file/fileSelectors'; import { addFiles, consumeFiles, createFileActions } from './file/fileActions'; import { FileLifecycleManager } from './file/lifecycle'; import { FileStateContext, FileActionsContext } from './file/contexts'; @@ -76,7 +76,7 @@ function FileContextInner({ const addRawFiles = useCallback(async (files: File[]): Promise => { const addedFilesWithIds = await addFiles('raw', { files }, stateRef, filesRef, dispatch, lifecycleManager); - // Persist to IndexedDB if enabled - pass existing thumbnail to prevent double generation + // Persist to IndexedDB if enabled if (indexedDB && enablePersistence && addedFilesWithIds.length > 0) { await Promise.all(addedFilesWithIds.map(async ({ file, id, thumbnail }) => { try { diff --git a/frontend/src/contexts/IndexedDBContext.tsx b/frontend/src/contexts/IndexedDBContext.tsx index 6b528fc06..f713c85aa 100644 --- a/frontend/src/contexts/IndexedDBContext.tsx +++ b/frontend/src/contexts/IndexedDBContext.tsx @@ -10,11 +10,10 @@ import { fileStorage, StoredFile } from '../services/fileStorage'; import { FileId } from '../types/fileContext'; import { FileMetadata } from '../types/file'; import { generateThumbnailForFile } from '../utils/thumbnailUtils'; -import { pdfWorkerManager } from '../services/pdfWorkerManager'; interface IndexedDBContextValue { // Core CRUD operations - saveFile: (file: File, fileId: FileId, thumbnail?: string) => Promise; + saveFile: (file: File, fileId: FileId, existingThumbnail?: string) => Promise; loadFile: (fileId: FileId) => Promise; loadMetadata: (fileId: FileId) => Promise; deleteFile: (fileId: FileId) => Promise; @@ -26,9 +25,6 @@ interface IndexedDBContextValue { // Utilities getStorageStats: () => Promise<{ used: number; available: number; fileCount: number }>; - - // Draft operations - loadAllDraftMetadata: () => Promise; } const IndexedDBContext = createContext(null); @@ -60,44 +56,7 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) { }, []); const saveFile = useCallback(async (file: File, fileId: FileId, existingThumbnail?: string): Promise => { - // Check for duplicate at IndexedDB level before saving - const quickKey = `${file.name}|${file.size}|${file.lastModified}`; - const existingFiles = await fileStorage.getAllFileMetadata(); - const duplicate = existingFiles.find(stored => - `${stored.name}|${stored.size}|${stored.lastModified}` === quickKey - ); - - if (duplicate) { - if (DEBUG) console.log(`🔍 SAVE: Skipping IndexedDB duplicate - using existing record:`, duplicate.name); - // Return the existing file's metadata instead of saving duplicate - return { - id: duplicate.id, - name: duplicate.name, - type: duplicate.type, - size: duplicate.size, - lastModified: duplicate.lastModified, - thumbnail: duplicate.thumbnail - }; - } - - // DEBUG: Check original file before saving - if (DEBUG && file.type === 'application/pdf') { - try { - const arrayBuffer = await file.arrayBuffer(); - const pdf = await pdfWorkerManager.createDocument(arrayBuffer); - console.log(`🔍 BEFORE SAVE - Original file:`, { - name: file.name, - size: file.size, - arrayBufferSize: arrayBuffer.byteLength, - pages: pdf.numPages - }); - pdfWorkerManager.destroyDocument(pdf); - } catch (error) { - console.error(`🔍 Error validating file before save:`, error); - } - } - - // Use existing thumbnail or generate new one + // Use existing thumbnail or generate new one if none provided const thumbnail = existingThumbnail || await generateThumbnailForFile(file); // Store in IndexedDB @@ -127,12 +86,9 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) { return cached.file; } - // Load from IndexedDB using the internal fileStorage (which wraps indexedDBManager) + // Load from IndexedDB const storedFile = await fileStorage.getFile(fileId); - if (!storedFile) { - if (DEBUG) console.log(`📁 File not found in IndexedDB: ${fileId}`); - return null; - } + if (!storedFile) return null; // Reconstruct File object const file = new File([storedFile.data], storedFile.name, { @@ -140,27 +96,6 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) { lastModified: storedFile.lastModified }); - // DEBUG: Check if file reconstruction is working - if (DEBUG && file.type === 'application/pdf') { - console.log(`🔍 AFTER LOAD - Reconstructed file:`, { - name: file.name, - originalSize: storedFile.size, - reconstructedSize: file.size, - dataLength: storedFile.data.byteLength, - sizesMatch: storedFile.size === file.size - }); - - // Quick PDF validation - try { - const arrayBuffer = await file.arrayBuffer(); - const pdf = await pdfWorkerManager.createDocument(arrayBuffer); - console.log(`🔍 AFTER LOAD - PDF validation: ${pdf.numPages} pages in reconstructed file`); - pdfWorkerManager.destroyDocument(pdf); - } catch (error) { - console.error(`🔍 AFTER LOAD - PDF reconstruction error:`, error); - } - } - // Cache for future use with LRU eviction fileCache.current.set(fileId, { file, lastAccessed: Date.now() }); evictLRUEntries(); @@ -239,40 +174,6 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) { return await fileStorage.getStorageStats(); }, []); - const loadAllDraftMetadata = useCallback(async (): Promise => { - try { - const { indexedDBManager, DATABASE_CONFIGS } = await import('../services/indexedDBManager'); - const db = await indexedDBManager.openDatabase(DATABASE_CONFIGS.DRAFTS); - - return new Promise((resolve, reject) => { - const transaction = db.transaction(['drafts'], 'readonly'); - const store = transaction.objectStore('drafts'); - const request = store.getAll(); - - request.onsuccess = () => { - const drafts = request.result || []; - const draftMetadata: FileMetadata[] = drafts.map((draft: any) => ({ - id: draft.id, - name: draft.name || `Draft ${draft.id}`, - type: 'application/pdf', - size: draft.size || 0, - lastModified: draft.timestamp || Date.now(), - thumbnail: draft.thumbnail, - isDraft: true - })); - resolve(draftMetadata); - }; - - request.onerror = () => reject(request.error); - }); - } catch (error) { - console.warn('Failed to load draft metadata:', error); - return []; - } - }, []); - - // No periodic cleanup needed - LRU eviction happens on-demand when cache fills - const value: IndexedDBContextValue = { saveFile, loadFile, @@ -281,8 +182,7 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) { loadAllMetadata, deleteMultiple, clearAll, - getStorageStats, - loadAllDraftMetadata + getStorageStats }; return ( diff --git a/frontend/src/contexts/file/fileActions.ts b/frontend/src/contexts/file/fileActions.ts index d1c9d798c..3f63753d2 100644 --- a/frontend/src/contexts/file/fileActions.ts +++ b/frontend/src/contexts/file/fileActions.ts @@ -217,7 +217,7 @@ export async function addFiles( try { if (DEBUG) console.log(`📄 addFiles(stored): Generating PDF metadata for stored file ${file.name}`); - // Use PDF worker manager directly for page count (avoids fileProcessingService conflicts) + // Get page count from PDF const arrayBuffer = await file.arrayBuffer(); const { pdfWorkerManager } = await import('../../services/pdfWorkerManager'); const pdf = await pdfWorkerManager.createDocument(arrayBuffer); @@ -243,7 +243,6 @@ export async function addFiles( fileRecords.push(record); addedFiles.push({ file, id: fileId, thumbnail: metadata.thumbnail }); - // Note: No background fileProcessingService call for stored files - we already processed them above } break; } @@ -326,4 +325,4 @@ export const createFileActions = (dispatch: React.Dispatch) = pinFile: (fileId: FileId) => dispatch({ type: 'PIN_FILE', payload: { fileId } }), unpinFile: (fileId: FileId) => dispatch({ type: 'UNPIN_FILE', payload: { fileId } }), resetContext: () => dispatch({ type: 'RESET_CONTEXT' }) -}); \ No newline at end of file +});