mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 13:53:34 +00:00
group collection in getNestedCollectionTree
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
|||||||
CollectionView,
|
CollectionView,
|
||||||
NestingDelimiter,
|
NestingDelimiter,
|
||||||
} from "@bitwarden/admin-console/common";
|
} from "@bitwarden/admin-console/common";
|
||||||
|
import { OrganizationId } from "@bitwarden/common/types/guid";
|
||||||
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
||||||
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
|
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
|
||||||
|
|
||||||
@@ -26,15 +27,23 @@ export function getNestedCollectionTree(
|
|||||||
.sort((a, b) => a.name.localeCompare(b.name))
|
.sort((a, b) => a.name.localeCompare(b.name))
|
||||||
.map(cloneCollection);
|
.map(cloneCollection);
|
||||||
|
|
||||||
const nodes: TreeNode<CollectionView | CollectionAdminView>[] = [];
|
const all: TreeNode<CollectionView | CollectionAdminView>[] = [];
|
||||||
clonedCollections.forEach((collection) => {
|
const groupedByOrg = new Map<OrganizationId, CollectionView[]>();
|
||||||
const parts =
|
clonedCollections.map((c) => {
|
||||||
collection.name != null
|
const key = c.organizationId;
|
||||||
? collection.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter)
|
(groupedByOrg.get(key) ?? groupedByOrg.set(key, []).get(key)!).push(c);
|
||||||
: [];
|
|
||||||
ServiceUtils.nestedTraverse(nodes, 0, parts, collection, null, NestingDelimiter);
|
|
||||||
});
|
});
|
||||||
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;
|
export function cloneCollection(collection: CollectionView): CollectionView;
|
||||||
|
|||||||
Reference in New Issue
Block a user