Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update folder count and delete queries to include subfolder contents (#1695)
Leendert de Borst98f31cc5af6a
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsxindex 450842fab..e9cf8c576 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx@@ -463,6 +463,7 @@ const ItemsList: React.FC = () => {* Get folders with item counts.* - At root level: show only root folders (ParentFolderId IS NULL)* - Inside a folder: show only direct subfolders (ParentFolderId = currentFolderId)+ * - Counts include items in the folder AND all subfolders recursively*/const getFoldersWithCounts = (): FolderWithCount[] => {if (searchTerm) {@@ -490,24 +491,71 @@ const ItemsList: React.FC = () => {}});- // Count items per folder- const folderCounts = new Map<string, number>();+ // Count items per folder (direct items only)+ const directFolderCounts = new Map<string, number>();items.forEach((item: Item) => {if (item.FolderId) {- folderCounts.set(item.FolderId, (folderCounts.get(item.FolderId) || 0) + 1);+ directFolderCounts.set(item.FolderId, (directFolderCounts.get(item.FolderId) || 0) + 1);}});- // Build result with counts+ /**+ * Recursively count items in a folder and all its subfolders.+ * @param folderId - The folder ID to count items for+ * @returns Total count of items in this folder and all descendant folders+ */+ const getRecursiveItemCount = (folderId: string): number => {+ // Start with direct items in this folder+ let count = directFolderCounts.get(folderId) || 0;++ // Add counts from all child folders recursively+ const childFolderIds = getAllChildFolderIds(folderId);+ for (const childId of childFolderIds) {+ count += directFolderCounts.get(childId) || 0;+ }++ return count;+ };++ // Build result with recursive countsconst result = relevantFolders.map((folder: Folder) => ({id: folder.Id,name: folder.Name,- itemCount: folderCounts.get(folder.Id) || 0+ itemCount: getRecursiveItemCount(folder.Id)})).sort((a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name));return result;};+ /**+ * Get all child folder IDs recursively for a given folder.+ * @param folderId - The parent folder ID to get children for+ * @returns Array of all descendant folder IDs+ */+ const getAllChildFolderIds = useCallback((folderId: string): string[] => {+ if (!dbContext?.sqliteClient) {+ return [];+ }++ const allFolders = dbContext.sqliteClient.folders.getAll();+ const childIds: string[] = [];++ /**+ * Recursively find all child folders.+ * @param parentId - The parent folder ID+ */+ const findChildren = (parentId: string): void => {+ const children = allFolders.filter(f => f.ParentFolderId === parentId);+ for (const child of children) {+ childIds.push(child.Id);+ findChildren(child.Id); // Recursively find descendants+ }+ };++ findChildren(folderId);+ return childIds;+ }, [dbContext]);+/*** Filter items based on current view (folder, search, filter type)*/@@ -595,6 +643,25 @@ const ItemsList: React.FC = () => {const folders = getFoldersWithCounts();+ /**+ * Calculate total item count including items in current folder and all child folders.+ * Used for the delete folder modal to show accurate count.+ */+ const totalItemCountInFolderTree = useMemo(() => {+ if (!currentFolderId) {+ return filteredItems.length;+ }++ // Get all child folder IDs+ const childFolderIds = getAllChildFolderIds(currentFolderId);+ const allFolderIds = [currentFolderId, ...childFolderIds];++ // Count items in current folder and all child folders+ return items.filter(item =>+ item.FolderId && allFolderIds.includes(item.FolderId)+ ).length;+ }, [currentFolderId, items, getAllChildFolderIds, filteredItems.length]);+/*** Check if the current folder can have subfolders (not at max depth).* At root level (currentFolderId = null), we can always create folders.@@ -1105,7 +1172,7 @@ const ItemsList: React.FC = () => {onClose={() => setShowDeleteFolderModal(false)}onDeleteFolderOnly={handleDeleteFolderOnly}onDeleteFolderAndContents={handleDeleteFolderAndContents}- itemCount={filteredItems.length}+ itemCount={totalItemCountInFolderTree}/></div>);diff --git a/apps/browser-extension/src/utils/db/queries/ItemQueries.ts b/apps/browser-extension/src/utils/db/queries/ItemQueries.tsindex 5fd94e614..d0b4b06fb 100644--- a/apps/browser-extension/src/utils/db/queries/ItemQueries.ts+++ b/apps/browser-extension/src/utils/db/queries/ItemQueries.ts@@ -78,36 +78,16 @@ export class ItemQueries {/*** Get all recently deleted items (in trash).+ * Note: Trashed items have FolderId = NULL (severed from folder structure),+ * so we don't need the recursive CTE for folder paths.*/public static readonly GET_RECENTLY_DELETED = `- WITH RECURSIVE FolderPath AS (- SELECT- Id,- Name,- ParentFolderId,- Name as Path,- 0 as Depth- FROM Folders- WHERE ParentFolderId IS NULL AND IsDeleted = 0-- UNION ALL-- SELECT- f.Id,- f.Name,- f.ParentFolderId,- fp.Path || ' > ' || f.Name as Path,- fp.Depth + 1 as Depth- FROM Folders f- INNER JOIN FolderPath fp ON f.ParentFolderId = fp.Id- WHERE f.IsDeleted = 0 AND fp.Depth < 10- )- SELECT DISTINCT+ SELECTi.Id,i.Name,i.ItemType,i.FolderId,- fp.Path as FolderPath,+ NULL as FolderPath,l.FileData as Logo,CASE WHEN EXISTS (SELECT 1 FROM Passkeys pk WHERE pk.ItemId = i.Id AND pk.IsDeleted = 0) THEN 1 ELSE 0 END as HasPasskey,CASE WHEN EXISTS (SELECT 1 FROM Attachments att WHERE att.ItemId = i.Id AND att.IsDeleted = 0) THEN 1 ELSE 0 END as HasAttachment,@@ -117,7 +97,6 @@ export class ItemQueries {i.DeletedAtFROM Items iLEFT JOIN Logos l ON i.LogoId = l.Id- LEFT JOIN FolderPath fp ON i.FolderId = fp.IdWHERE i.IsDeleted = 0 AND i.DeletedAt IS NOT NULLORDER BY i.DeletedAt DESC`;diff --git a/apps/browser-extension/src/utils/db/repositories/FolderRepository.ts b/apps/browser-extension/src/utils/db/repositories/FolderRepository.tsindex f968fc462..b59ede8bf 100644--- a/apps/browser-extension/src/utils/db/repositories/FolderRepository.ts+++ b/apps/browser-extension/src/utils/db/repositories/FolderRepository.ts@@ -75,6 +75,23 @@ const FolderQueries = {FolderId = NULLWHERE FolderId = ? AND IsDeleted = 0 AND DeletedAt IS NULL`,+ /**+ * Get all child folder IDs (direct children only).+ */+ GET_CHILD_FOLDER_IDS: `+ SELECT Id+ FROM Folders+ WHERE ParentFolderId = ? AND IsDeleted = 0`,++ /**+ * Update parent folder for child folders.+ */+ UPDATE_PARENT_FOLDER: `+ UPDATE Folders+ SET ParentFolderId = ?,+ UpdatedAt = ?+ WHERE ParentFolderId = ?`,+/*** Move item to folder.*/@@ -158,9 +175,34 @@ export class FolderRepository extends BaseRepository {});}+ /**+ * Get all child folder IDs recursively.+ * @param folderId - The parent folder ID+ * @returns Array of all descendant folder IDs+ */+ private getAllChildFolderIds(folderId: string): string[] {+ const directChildren = this.client.executeQuery<{ Id: string }>(+ FolderQueries.GET_CHILD_FOLDER_IDS,+ [folderId]+ );++ const allChildIds: string[] = [];++ for (const child of directChildren) {+ allChildIds.push(child.Id);+ // Recursively get all descendants+ const descendants = this.getAllChildFolderIds(child.Id);+ allChildIds.push(...descendants);+ }++ return allChildIds;+ }+/*** Delete a folder (soft delete).- * Note: Items in the folder will have their FolderId set to NULL.+ * Recursively handles child folders and items:+ * - All items in this folder and child folders are moved to root (FolderId = NULL)+ * - All child folders are moved to the parent of the deleted folder* @param folderId - The ID of the folder to delete* @returns The number of rows updated*/@@ -168,12 +210,33 @@ export class FolderRepository extends BaseRepository {return this.withTransaction(async () => {const currentDateTime = this.now();- // Remove folder reference from all items in this folder+ // Get the parent folder of the folder being deleted+ const folder = this.getById(folderId);+ const targetParentId = folder?.ParentFolderId || null;++ // Get all child folder IDs recursively+ const allChildFolderIds = this.getAllChildFolderIds(folderId);++ // Move all items in this folder and all child folders to rootthis.client.executeUpdate(FolderQueries.CLEAR_ITEMS_FOLDER, [currentDateTime,folderId]);+ for (const childFolderId of allChildFolderIds) {+ this.client.executeUpdate(FolderQueries.CLEAR_ITEMS_FOLDER, [+ currentDateTime,+ childFolderId+ ]);+ }++ // Move direct child folders to the parent of the deleted folder+ this.client.executeUpdate(FolderQueries.UPDATE_PARENT_FOLDER, [+ targetParentId,+ currentDateTime,+ folderId+ ]);+// Soft delete the folderreturn this.client.executeUpdate(FolderQueries.SOFT_DELETE, [currentDateTime,@@ -184,7 +247,9 @@ export class FolderRepository extends BaseRepository {/*** Delete a folder and all items within it (soft delete both folder and items).- * Items are moved to "Recently Deleted" (trash).+ * Recursively handles child folders:+ * - All items in this folder and child folders are moved to "Recently Deleted" (trash)+ * - All child folders are also deleted* @param folderId - The ID of the folder to delete* @returns The number of items trashed*/@@ -192,20 +257,42 @@ export class FolderRepository extends BaseRepository {return this.withTransaction(async () => {const currentDateTime = this.now();- // Move all items in this folder to trash and clear FolderId- const itemsDeleted = this.client.executeUpdate(FolderQueries.TRASH_ITEMS_IN_FOLDER, [+ // Get all child folder IDs recursively+ const allChildFolderIds = this.getAllChildFolderIds(folderId);++ let totalItemsDeleted = 0;++ // Move all items in this folder to trash+ totalItemsDeleted += this.client.executeUpdate(FolderQueries.TRASH_ITEMS_IN_FOLDER, [currentDateTime,currentDateTime,folderId]);- // Soft delete the folder+ // Move all items in child folders to trash+ for (const childFolderId of allChildFolderIds) {+ totalItemsDeleted += this.client.executeUpdate(FolderQueries.TRASH_ITEMS_IN_FOLDER, [+ currentDateTime,+ currentDateTime,+ childFolderId+ ]);+ }++ // Soft delete all child folders+ for (const childFolderId of allChildFolderIds) {+ this.client.executeUpdate(FolderQueries.SOFT_DELETE, [+ currentDateTime,+ childFolderId+ ]);+ }++ // Soft delete the parent folderthis.client.executeUpdate(FolderQueries.SOFT_DELETE, [currentDateTime,folderId]);- return itemsDeleted;+ return totalItemsDeleted;});}diff --git a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts b/apps/browser-extension/src/utils/db/repositories/ItemRepository.tsindex b64777da2..092f21d4e 100644--- a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts+++ b/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts@@ -298,14 +298,12 @@ export class ItemRepository extends BaseRepository {public getRecentlyDeleted(): ItemWithDeletedAt[] {let itemRows: (ItemRow & { DeletedAt: string })[];try {- // Need a modified query that includes DeletedAt in the SELECTconst query = `- SELECT DISTINCT+ SELECTi.Id,i.Name,i.ItemType,i.FolderId,- f.Name as FolderPath,l.FileData as Logo,i.DeletedAt,CASE WHEN EXISTS (SELECT 1 FROM Passkeys pk WHERE pk.ItemId = i.Id AND pk.IsDeleted = 0) THEN 1 ELSE 0 END as HasPasskey,@@ -315,7 +313,6 @@ export class ItemRepository extends BaseRepository {i.UpdatedAtFROM Items iLEFT JOIN Logos l ON i.LogoId = l.Id- LEFT JOIN Folders f ON i.FolderId = f.IdWHERE i.IsDeleted = 0 AND i.DeletedAt IS NOT NULLORDER BY i.DeletedAt DESC`;