1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-06 00:13:28 +00:00

group collection in getNestedCollectionTree

This commit is contained in:
jaasen-livefront
2025-09-25 13:31:17 -07:00
parent 24f07dc1e1
commit 1578f44add

View File

@@ -5,6 +5,7 @@ import {
CollectionView,
NestingDelimiter,
} from "@bitwarden/admin-console/common";
import { OrganizationId } from "@bitwarden/common/types/guid";
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
@@ -26,15 +27,23 @@ export function getNestedCollectionTree(
.sort((a, b) => a.name.localeCompare(b.name))
.map(cloneCollection);
const nodes: TreeNode<CollectionView | CollectionAdminView>[] = [];
clonedCollections.forEach((collection) => {
const parts =
collection.name != null
? collection.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter)
: [];
ServiceUtils.nestedTraverse(nodes, 0, parts, collection, null, NestingDelimiter);
const all: TreeNode<CollectionView | CollectionAdminView>[] = [];
const groupedByOrg = new Map<OrganizationId, CollectionView[]>();
clonedCollections.map((c) => {
const key = c.organizationId;
(groupedByOrg.get(key) ?? groupedByOrg.set(key, []).get(key)!).push(c);
});
return nodes;
for (const group of groupedByOrg.values()) {
const nodes: TreeNode<CollectionView>[] = [];
for (const c of group) {
const collectionCopy = Object.assign(new CollectionView({ ...c, name: c.name }), c);
const parts = c.name ? c.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter) : [];
ServiceUtils.nestedTraverse(nodes, 0, parts, collectionCopy, undefined, NestingDelimiter);
}
all.push(...nodes);
}
return all;
}
export function cloneCollection(collection: CollectionView): CollectionView;