From 371243de31b9bcdaa77439c3adc79f18ca490fce Mon Sep 17 00:00:00 2001 From: Reece Date: Sat, 31 Jan 2026 14:49:20 +0000 Subject: [PATCH] Fix bugs --- .../pageEditor/hooks/usePageDocument.ts | 19 +++++++++++++++++- .../src/core/contexts/PageEditorContext.tsx | 20 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/frontend/src/core/components/pageEditor/hooks/usePageDocument.ts b/frontend/src/core/components/pageEditor/hooks/usePageDocument.ts index 85ee311d60..279d71d505 100644 --- a/frontend/src/core/components/pageEditor/hooks/usePageDocument.ts +++ b/frontend/src/core/components/pageEditor/hooks/usePageDocument.ts @@ -43,6 +43,11 @@ export function usePageDocument(): PageDocumentHook { return []; } const selectedSet = new Set(state.ui.selectedFileIds); + console.log('[usePageDocument] File selection:', { + totalActive: activeFileIds.length, + selectedInContext: selectedSet.size, + selectedActive: activeFileIds.filter((id) => selectedSet.has(id)).length, + }); if (selectedSet.size === 0) { return []; } @@ -122,7 +127,18 @@ export function usePageDocument(): PageDocumentHook { }, [currentPages]); const mergedPdfDocument = useMemo((): PDFDocument | null => { - if (activeFileIds.length === 0) return null; + console.log('[usePageDocument] Building document:', { + activeFileIds: activeFileIds.length, + selectedActiveFileIds: selectedActiveFileIds.length, + hasPersistedDoc: !!persistedDocument, + persistedSig: persistedDocumentSignature?.substring(0, 50), + currentSig: currentPagesSignature.substring(0, 50), + }); + + if (activeFileIds.length === 0) { + console.log('[usePageDocument] No active files, returning null'); + return null; + } if ( persistedDocument && @@ -130,6 +146,7 @@ export function usePageDocument(): PageDocumentHook { persistedDocumentSignature === currentPagesSignature && currentPagesSignature.length > 0 ) { + console.log('[usePageDocument] Using persisted document'); return persistedDocument; } diff --git a/frontend/src/core/contexts/PageEditorContext.tsx b/frontend/src/core/contexts/PageEditorContext.tsx index 2acee3ec29..a091f41258 100644 --- a/frontend/src/core/contexts/PageEditorContext.tsx +++ b/frontend/src/core/contexts/PageEditorContext.tsx @@ -154,8 +154,10 @@ export function PageEditorProvider({ children }: PageEditorProviderProps) { }, []); const clearPersistedDocument = useCallback(() => { + console.log('[PageEditorContext] Clearing persisted document and currentPages'); setPersistedDocument(null); setPersistedDocumentSignature(null); + setCurrentPages(null); // Also clear currentPages to avoid stale signature comparison }, []); // Page editor's own file order (independent of FileContext) @@ -166,14 +168,23 @@ export function PageEditorProvider({ children }: PageEditorProviderProps) { const { state } = useFileState(); const fileContextSignature = useMemo(() => { - return state.files.ids + const sig = state.files.ids .map(id => `${id}:${state.files.byId[id]?.versionNumber ?? 0}`) .join(','); + console.log('[PageEditorContext] File signature computed:', { + fileCount: state.files.ids.length, + signature: sig.substring(0, 100), + }); + return sig; }, [state.files.ids, state.files.byId]); const prevFileContextSignature = useRef(null); useEffect(() => { if (prevFileContextSignature.current !== fileContextSignature) { + console.log('[PageEditorContext] File signature changed, clearing persisted document:', { + prev: prevFileContextSignature.current?.substring(0, 50), + current: fileContextSignature.substring(0, 50), + }); prevFileContextSignature.current = fileContextSignature; clearPersistedDocument(); } @@ -202,6 +213,13 @@ export function PageEditorProvider({ children }: PageEditorProviderProps) { return; } + console.log('[PageEditorContext] FileContext files changed:', { + prevCount: prevFileIds.length, + currentCount: currentFileIds.length, + added: currentFileIds.filter(id => !prevFileIds.includes(id)).length, + removed: prevFileIds.filter(id => !currentFileIds.includes(id)).length, + }); + prevFileContextIdsRef.current = currentFileIds; // Collect new file IDs outside the setState callback so we can clear them after