Merge branch 'master' into 13860-process-statuses-filters
[arvados-workbench2.git] / src / models / collection-file.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Tree, createTree, setNode } from './tree';
6
7 export type CollectionFilesTree = Tree<CollectionDirectory | CollectionFile>;
8
9 export enum CollectionFileType {
10     DIRECTORY = 'directory',
11     FILE = 'file'
12 }
13
14 export interface CollectionDirectory {
15     path: string;
16     url: string;
17     id: string;
18     name: string;
19     type: CollectionFileType.DIRECTORY;
20 }
21
22 export interface CollectionFile {
23     path: string;
24     url: string;
25     id: string;
26     name: string;
27     size: number;
28     type: CollectionFileType.FILE;
29 }
30
31 export interface CollectionUploadFile {
32     name: string;
33 }
34
35 export const createCollectionDirectory = (data: Partial<CollectionDirectory>): CollectionDirectory => ({
36     id: '',
37     name: '',
38     path: '',
39     url: '',
40     type: CollectionFileType.DIRECTORY,
41     ...data
42 });
43
44 export const createCollectionFile = (data: Partial<CollectionFile>): CollectionFile => ({
45     id: '',
46     name: '',
47     path: '',
48     url: '',
49     size: 0,
50     type: CollectionFileType.FILE,
51     ...data
52 });
53
54 export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>) => {
55     const directories = data.filter(item => item.type === CollectionFileType.DIRECTORY);
56     directories.sort((a, b) => a.path.localeCompare(b.path));
57     const files = data.filter(item => item.type === CollectionFileType.FILE);
58     return [...directories, ...files]
59         .reduce((tree, item) => setNode({
60             children: [],
61             id: item.id,
62             parent: item.path,
63             value: item
64         })(tree), createTree<CollectionDirectory | CollectionFile>());
65 };