1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { connect } from "react-redux";
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";
13 CollectionPanelDirectory,
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";
27 const memoizedMapStateToProps = () => {
28 let prevState: CollectionPanelFilesState;
29 let prevTree: Array<TreeItem<FileTreeData>>;
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));
43 const mapDispatchToProps = (dispatch: Dispatch): Pick<CollectionPanelFilesProps, 'onUploadDataClick' | 'onCollapseToggle' | 'onSelectionToggle' | 'onItemMenuOpen' | 'onOptionsMenuOpen'> => ({
44 onUploadDataClick: () => {
45 dispatch<any>(openUploadCollectionFilesDialog());
47 onCollapseToggle: (id) => {
48 dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id }));
50 onSelectionToggle: (event, item) => {
51 dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: item.id }));
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: '' }));
56 onOptionsMenuOpen: (event) => {
57 dispatch<any>(openCollectionFilesContextMenu(event));
62 export const CollectionPanelFiles = connect(memoizedMapStateToProps(), mapDispatchToProps)(Component);
64 const collectionItemToTreeItem = (tree: Tree<CollectionPanelDirectory | CollectionPanelFile>) =>
65 (id: string): TreeItem<FileTreeData> => {
66 const node = getNode(id)(tree) || {
72 type: CollectionFileType.DIRECTORY,
80 name: node.value.name,
81 size: node.value.type === CollectionFileType.FILE ? node.value.size : undefined,
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