Skip to content

Commit 60aaa39

Browse files
re-do getNestedCollectionTree (#16760)
1 parent fb2bd01 commit 60aaa39

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

apps/web/src/app/admin-console/organizations/collections/utils/collection-utils.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CollectionView,
66
NestingDelimiter,
77
} from "@bitwarden/admin-console/common";
8+
import { OrganizationId } from "@bitwarden/common/types/guid";
89
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
910
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
1011

@@ -26,15 +27,21 @@ export function getNestedCollectionTree(
2627
.sort((a, b) => a.name.localeCompare(b.name))
2728
.map(cloneCollection);
2829

29-
const nodes: TreeNode<CollectionView | CollectionAdminView>[] = [];
30-
clonedCollections.forEach((collection) => {
31-
const parts =
32-
collection.name != null
33-
? collection.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter)
34-
: [];
35-
ServiceUtils.nestedTraverse(nodes, 0, parts, collection, null, NestingDelimiter);
30+
const all: TreeNode<CollectionView | CollectionAdminView>[] = [];
31+
const groupedByOrg = new Map<OrganizationId, (CollectionView | CollectionAdminView)[]>();
32+
clonedCollections.map((c) => {
33+
const key = c.organizationId;
34+
(groupedByOrg.get(key) ?? groupedByOrg.set(key, []).get(key)!).push(c);
3635
});
37-
return nodes;
36+
for (const group of groupedByOrg.values()) {
37+
const nodes: TreeNode<CollectionView | CollectionAdminView>[] = [];
38+
for (const c of group) {
39+
const parts = c.name ? c.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter) : [];
40+
ServiceUtils.nestedTraverse(nodes, 0, parts, c, undefined, NestingDelimiter);
41+
}
42+
all.push(...nodes);
43+
}
44+
return all;
3845
}
3946

4047
export function cloneCollection(collection: CollectionView): CollectionView;

0 commit comments

Comments
 (0)