1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Tree, createTree, setNode, TreeNodeStatus } from './tree';
6 import { head, split, pipe, join } from 'lodash/fp';
8 export type CollectionFilesTree = Tree<CollectionDirectory | CollectionFile>;
10 export enum CollectionFileType {
11 DIRECTORY = 'directory',
15 export interface CollectionDirectory {
20 type: CollectionFileType.DIRECTORY;
23 export interface CollectionFile {
29 type: CollectionFileType.FILE;
32 export interface CollectionUploadFile {
36 export const createCollectionDirectory = (data: Partial<CollectionDirectory>): CollectionDirectory => ({
41 type: CollectionFileType.DIRECTORY,
45 export const createCollectionFile = (data: Partial<CollectionFile>): CollectionFile => ({
51 type: CollectionFileType.FILE,
55 export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>, joinParents: Boolean = true) => {
56 const directories = data.filter(item => item.type === CollectionFileType.DIRECTORY);
57 directories.sort((a, b) => a.path.localeCompare(b.path));
58 const files = data.filter(item => item.type === CollectionFileType.FILE);
59 return [...directories, ...files]
60 .reduce((tree, item) => setNode({
63 parent: joinParents ? getParentId(item) : '',
68 status: TreeNodeStatus.INITIAL
69 })(tree), createTree<CollectionDirectory | CollectionFile>());
72 const getParentId = (item: CollectionDirectory | CollectionFile) =>
74 ? join('', [getCollectionId(item.id), item.path])
77 const getCollectionId = pipe(