From f641e3d3c0839e4df05e413216f618989eae8346 Mon Sep 17 00:00:00 2001 From: abhay-codes07 Date: Sat, 15 Aug 2026 14:53:47 +0530 Subject: [PATCH] fix(web): clear document selection when the active space changes The bulk-selection set (`selectedDocumentIds`) lived in AppExperience and was only cleared on explicit "clear", on successful bulk delete, or never. Switching spaces left the previous space's selected ids in the set while the grid rendered a different space, so the selection toolbar still showed them and "Delete selected" would permanently delete documents from another space that the user could no longer see. Clear the selection (and exit selection mode) whenever the active space changes. The functional updates return the previous reference when there is nothing to clear, so the initial mount doesn't cause needless re-renders. --- apps/web/components/app-experience.tsx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apps/web/components/app-experience.tsx b/apps/web/components/app-experience.tsx index 77695b10..63a48d3f 100644 --- a/apps/web/components/app-experience.tsx +++ b/apps/web/components/app-experience.tsx @@ -294,6 +294,17 @@ export function AppExperience() { ) const [isSelectionMode, setIsSelectionMode] = useState(false) + // Selection is scoped to the documents currently on screen. When the active + // space changes the previous selection points at documents from another + // space that are no longer visible, so clear it — otherwise "Delete selected" + // would delete documents the user can't see anymore. + const activeSpaceKey = selectedProjects.join("|") + // biome-ignore lint/correctness/useExhaustiveDependencies: reset only when the space changes, not on selection edits + useEffect(() => { + setSelectedDocumentIds((prev) => (prev.size === 0 ? prev : new Set())) + setIsSelectionMode((prev) => (prev ? false : prev)) + }, [activeSpaceKey]) + const handleToggleSelection = useCallback((documentId: string) => { setSelectedDocumentIds((prev) => { const next = new Set(prev)