55 CollectionView ,
66 NestingDelimiter ,
77} from "@bitwarden/admin-console/common" ;
8+ import { OrganizationId } from "@bitwarden/common/types/guid" ;
89import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node" ;
910import { 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
4047export function cloneCollection ( collection : CollectionView ) : CollectionView ;
0 commit comments