This commit is contained in:
Reece
2026-01-31 14:49:20 +00:00
parent de017e0751
commit 371243de31
2 changed files with 37 additions and 2 deletions
@@ -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;
}
@@ -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<string | null>(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