Merge branch '13865-repositories'
[arvados-workbench2.git] / src / views-components / collection-panel-files / collection-panel-files.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import {
7     CollectionPanelFiles as Component,
8     CollectionPanelFilesProps
9 } from "~/components/collection-panel-files/collection-panel-files";
10 import { RootState } from "~/store/store";
11 import { TreeItem, TreeItemStatus } from "~/components/tree/tree";
12 import {
13     CollectionPanelDirectory,
14     CollectionPanelFile,
15     CollectionPanelFilesState
16 } from "~/store/collection-panel/collection-panel-files/collection-panel-files-state";
17 import { FileTreeData } from "~/components/file-tree/file-tree-data";
18 import { Dispatch } from "redux";
19 import { collectionPanelFilesAction } from "~/store/collection-panel/collection-panel-files/collection-panel-files-actions";
20 import { ContextMenuKind } from "../context-menu/context-menu";
21 import { getNode, getNodeChildrenIds, Tree } from "~/models/tree";
22 import { CollectionFileType } from "~/models/collection-file";
23 import { openContextMenu, openCollectionFilesContextMenu } from '~/store/context-menu/context-menu-actions';
24 import { openUploadCollectionFilesDialog } from '~/store/collections/collection-upload-actions';
25 import { ResourceKind } from "~/models/resource";
26
27 const memoizedMapStateToProps = () => {
28     let prevState: CollectionPanelFilesState;
29     let prevTree: Array<TreeItem<FileTreeData>>;
30
31     return (state: RootState): Pick<CollectionPanelFilesProps, "items"> => {
32         if (prevState !== state.collectionPanelFiles) {
33             prevState = state.collectionPanelFiles;
34             prevTree = getNodeChildrenIds('')(state.collectionPanelFiles)
35                 .map(collectionItemToTreeItem(state.collectionPanelFiles));
36         }
37         return {
38             items: prevTree
39         };
40     };
41 };
42
43 const mapDispatchToProps = (dispatch: Dispatch): Pick<CollectionPanelFilesProps, 'onUploadDataClick' | 'onCollapseToggle' | 'onSelectionToggle' | 'onItemMenuOpen' | 'onOptionsMenuOpen'> => ({
44     onUploadDataClick: () => {
45         dispatch<any>(openUploadCollectionFilesDialog());
46     },
47     onCollapseToggle: (id) => {
48         dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id }));
49     },
50     onSelectionToggle: (event, item) => {
51         dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: item.id }));
52     },
53     onItemMenuOpen: (event, item) => {
54         dispatch<any>(openContextMenu(event, { menuKind: ContextMenuKind.COLLECTION_FILES_ITEM, kind: ResourceKind.COLLECTION, name: item.data.name, uuid: item.id, ownerUuid: '' }));
55     },
56     onOptionsMenuOpen: (event) => {
57         dispatch<any>(openCollectionFilesContextMenu(event));
58     },
59 });
60
61
62 export const CollectionPanelFiles = connect(memoizedMapStateToProps(), mapDispatchToProps)(Component);
63
64 const collectionItemToTreeItem = (tree: Tree<CollectionPanelDirectory | CollectionPanelFile>) =>
65     (id: string): TreeItem<FileTreeData> => {
66         const node = getNode(id)(tree) || {
67             id: '',
68             children: [],
69             parent: '',
70             value: {
71                 name: 'Invalid node',
72                 type: CollectionFileType.DIRECTORY,
73                 selected: false,
74                 collapsed: true
75             }
76         };
77         return {
78             active: false,
79             data: {
80                 name: node.value.name,
81                 size: node.value.type === CollectionFileType.FILE ? node.value.size : undefined,
82                 type: node.value.type
83             },
84             id: node.id,
85             items: getNodeChildrenIds(node.id)(tree)
86                 .map(collectionItemToTreeItem(tree)),
87             open: node.value.type === CollectionFileType.DIRECTORY ? !node.value.collapsed : false,
88             selected: node.value.selected,
89             status: TreeItemStatus.LOADED
90         };
91     };