From 307f960a8a148d65e718a121a10c938ada5fb83d Mon Sep 17 00:00:00 2001 From: Connor Yoh Date: Thu, 11 Sep 2025 16:00:43 +0100 Subject: [PATCH] Check for orphans --- frontend/src/contexts/FileManagerContext.tsx | 33 +++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/frontend/src/contexts/FileManagerContext.tsx b/frontend/src/contexts/FileManagerContext.tsx index 55d13612b..4e1c88a9c 100644 --- a/frontend/src/contexts/FileManagerContext.tsx +++ b/frontend/src/contexts/FileManagerContext.tsx @@ -219,11 +219,42 @@ export const FileManagerProvider: React.FC = ({ } // Final list: files to delete minus files that must be preserved - const safeToDelete = Array.from(filesToDelete).filter(fileId => !filesToPreserve.has(fileId)); + let safeToDelete = Array.from(filesToDelete).filter(fileId => !filesToPreserve.has(fileId)); + + // Check for orphaned non-leaf files after main deletion + const remainingFiles = allStoredStubs.filter(file => !safeToDelete.includes(file.id as string)); + const orphanedNonLeafFiles: string[] = []; + + for (const file of remainingFiles) { + // Only check non-leaf files (files that have been processed and have children) + if (file.isLeaf === false) { + const fileOriginalId = file.originalFileId || file.id; + + // Check if this non-leaf file has any living descendants + const hasLivingDescendants = remainingFiles.some(otherFile => { + // Check if otherFile is a descendant of this file + const otherOriginalId = otherFile.originalFileId || otherFile.id; + return ( + // Direct parent relationship + otherFile.parentFileId === file.id || + // Same lineage but different from this file + (otherOriginalId === fileOriginalId && otherFile.id !== file.id) + ); + }); + + if (!hasLivingDescendants) { + orphanedNonLeafFiles.push(file.id as string); + } + } + } + + // Add orphaned non-leaf files to deletion list + safeToDelete = [...safeToDelete, ...orphanedNonLeafFiles]; console.log('Deletion analysis:', { candidatesForDeletion: Array.from(filesToDelete), mustPreserve: Array.from(filesToPreserve), + orphanedNonLeafFiles, safeToDelete });