// SPDX-License-Identifier: AGPL-3.0
import { Tree, createTree, setNode, TreeNodeStatus } from './tree';
+import { head, split, pipe, join } from 'lodash/fp';
export type CollectionFilesTree = Tree<CollectionDirectory | CollectionFile>;
...data
});
-export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>) => {
+export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>, joinParents: Boolean = true) => {
const directories = data.filter(item => item.type === CollectionFileType.DIRECTORY);
directories.sort((a, b) => a.path.localeCompare(b.path));
const files = data.filter(item => item.type === CollectionFileType.FILE);
.reduce((tree, item) => setNode({
children: [],
id: item.id,
- parent: item.path,
+ parent: joinParents ? getParentId(item) : '',
value: item,
active: false,
selected: false,
expanded: false,
status: TreeNodeStatus.INITIAL
-
})(tree), createTree<CollectionDirectory | CollectionFile>());
-};
\ No newline at end of file
+};
+
+const getParentId = (item: CollectionDirectory | CollectionFile) =>
+ item.path
+ ? join('', [getCollectionId(item.id), item.path])
+ : item.path;
+
+const getCollectionId = pipe(
+ split('/'),
+ head,
+);
\ No newline at end of file