From 54914c04f5c9b016cc26c1111b2eab1025de0294 Mon Sep 17 00:00:00 2001 From: Viktor Rozaev Date: Mon, 25 Nov 2024 16:17:51 +0100 Subject: [PATCH] fix(Groups): expand group tree on non-empty group filter [#853] --- packages/ui/src/ui/store/actions/groups.ts | 2 +- packages/ui/src/ui/store/selectors/groups.ts | 33 +++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/ui/src/ui/store/actions/groups.ts b/packages/ui/src/ui/store/actions/groups.ts index 2beb63677..df94d6127 100644 --- a/packages/ui/src/ui/store/actions/groups.ts +++ b/packages/ui/src/ui/store/actions/groups.ts @@ -75,7 +75,7 @@ export function toggleGroupExpand(groupName: string) { const expanded = {...getGroupsExpanded(getState())}; const current = expanded[groupName]; if (current) { - delete expanded[groupName]; + expanded[groupName] = false; } else { expanded[groupName] = true; } diff --git a/packages/ui/src/ui/store/selectors/groups.ts b/packages/ui/src/ui/store/selectors/groups.ts index 24f4f1c45..42541dcf5 100644 --- a/packages/ui/src/ui/store/selectors/groups.ts +++ b/packages/ui/src/ui/store/selectors/groups.ts @@ -88,15 +88,38 @@ const getGroupsTreeFiltered = createSelector( ); const getGroupsTreeFilteredAndExpanded = createSelector( - [getGroupsTreeFiltered, getGroupsExpanded], - (root, expanded) => { - const res = cloneDeep_(root); + [getGroupsTree, getGroupsTreeFiltered, getGroupsExpanded, getGroupsNameFilter], + (groupTree, groupsTreeFiltered, expandedByUser, groupNameFilter) => { + const expandedBySearch: Record = {}; + + const res = cloneDeep_(groupsTreeFiltered); + hammer.treeList.treeForEach(res.children, (node: GroupsTreeNode) => { - const {name} = node; - if (!expanded[name]) { + const isNodeMatchedFilter = groupNameFilter && node.name.includes(groupNameFilter); + + if (isNodeMatchedFilter) { + let parentName = node.parent!; + + while (groupTree[parentName]) { + expandedBySearch[parentName] = true; + parentName = groupTree[parentName].parent; + } + } + }); + + hammer.treeList.treeForEach(res.children, (node: GroupsTreeNode) => { + const userInteractedWithNode = typeof expandedByUser[node.name] !== 'undefined' + + const expanded = userInteractedWithNode ? expandedByUser : expandedBySearch; + + if (!expanded[node.name]) { + node.expanded = false; node.children = []; + } else { + node.expanded = true; } }); + return res; }, );