1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { connect } from "react-redux";
6 import { CollectionPanelFiles as Component, CollectionPanelFilesProps } from "~/components/collection-panel-files/collection-panel-files";
7 import { RootState } from "~/store/store";
8 import { TreeItemStatus, TreeItem } from "~/components/tree/tree";
9 import { CollectionPanelFilesState, CollectionPanelDirectory, CollectionPanelFile } from "~/store/collection-panel/collection-panel-files/collection-panel-files-state";
10 import { FileTreeData } from "~/components/file-tree/file-tree-data";
11 import { Dispatch } from "redux";
12 import { collectionPanelFilesAction } from "~/store/collection-panel/collection-panel-files/collection-panel-files-actions";
13 import { contextMenuActions } from "~/store/context-menu/context-menu-actions";
14 import { ContextMenuKind } from "../context-menu/context-menu";
15 import { Tree, getNodeChildrenIds, getNode } from "~/models/tree";
16 import { CollectionFileType } from "~/models/collection-file";
17 import { openUploadCollectionFilesDialog } from '~/store/collections/uploader/collection-uploader-actions';
19 const memoizedMapStateToProps = () => {
20 let prevState: CollectionPanelFilesState;
21 let prevTree: Array<TreeItem<FileTreeData>>;
23 return (state: RootState): Pick<CollectionPanelFilesProps, "items"> => {
24 if (prevState !== state.collectionPanelFiles) {
25 prevState = state.collectionPanelFiles;
26 prevTree = getNodeChildrenIds('')(state.collectionPanelFiles)
27 .map(collectionItemToTreeItem(state.collectionPanelFiles));
35 const mapDispatchToProps = (dispatch: Dispatch): Pick<CollectionPanelFilesProps, 'onUploadDataClick' | 'onCollapseToggle' | 'onSelectionToggle' | 'onItemMenuOpen' | 'onOptionsMenuOpen'> => ({
36 onUploadDataClick: () => {
37 dispatch<any>(openUploadCollectionFilesDialog());
39 onCollapseToggle: (id) => {
40 dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id }));
42 onSelectionToggle: (event, item) => {
43 dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: item.id }));
45 onItemMenuOpen: (event, item) => {
46 event.preventDefault();
47 dispatch(contextMenuActions.OPEN_CONTEXT_MENU({
48 position: { x: event.clientX, y: event.clientY },
49 resource: { kind: ContextMenuKind.COLLECTION_FILES_ITEM, name: item.data.name, uuid: item.id }
52 onOptionsMenuOpen: (event) =>
53 dispatch(contextMenuActions.OPEN_CONTEXT_MENU({
54 position: { x: event.clientX, y: event.clientY },
55 resource: { kind: ContextMenuKind.COLLECTION_FILES, name: '', uuid: '' }
60 export const CollectionPanelFiles = connect(memoizedMapStateToProps(), mapDispatchToProps)(Component);
62 const collectionItemToTreeItem = (tree: Tree<CollectionPanelDirectory | CollectionPanelFile>) =>
63 (id: string): TreeItem<FileTreeData> => {
64 const node = getNode(id)(tree) || {
70 type: CollectionFileType.DIRECTORY,
78 name: node.value.name,
79 size: node.value.type === CollectionFileType.FILE ? node.value.size : undefined,
83 items: getNodeChildrenIds(node.id)(tree)
84 .map(collectionItemToTreeItem(tree)),
85 open: node.value.type === CollectionFileType.DIRECTORY ? !node.value.collapsed : false,
86 selected: node.value.selected,
87 status: TreeItemStatus.LOADED