Add related tree pickers to projects tree picker
[arvados-workbench2.git] / src / views-components / projects-tree-picker / generic-projects-tree-picker.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { TreeItem, TreeItemStatus } from '~/components/tree/tree';
9 import { ProjectResource } from "~/models/project";
10 import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
11 import { ListItemTextIcon } from "~/components/list-item-text-icon/list-item-text-icon";
12 import { ProjectIcon, InputIcon, IconType, CollectionIcon } from '~/components/icon/icon';
13 import { loadProject, loadCollection } from '~/store/tree-picker/tree-picker-actions';
14 import { GroupContentsResource } from '~/services/groups-service/groups-service';
15 import { CollectionDirectory, CollectionFile, CollectionFileType } from '~/models/collection-file';
16 import { ResourceKind } from '~/models/resource';
17 import { TreePickerProps, TreePicker } from "~/views-components/tree-picker/tree-picker";
18
19 export interface ProjectsTreePickerRootItem {
20     id: string;
21     name: string;
22 }
23
24 export type ProjectsTreePickerItem = ProjectsTreePickerRootItem | GroupContentsResource | CollectionDirectory | CollectionFile;
25 type PickedTreePickerProps = Pick<TreePickerProps<ProjectsTreePickerItem>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
26
27 export interface ProjectsTreePickerDataProps {
28     includeCollections?: boolean;
29     includeFiles?: boolean;
30     rootItemIcon: IconType;
31     showSelection?: boolean;
32     relatedTreePickers?: string[];
33     loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string, includeCollections?: boolean, inlcudeFiles?: boolean) => void;
34 }
35
36 export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;
37
38 const mapStateToProps = (_: any, { rootItemIcon, showSelection }: ProjectsTreePickerProps) => ({
39     render: renderTreeItem(rootItemIcon),
40     showSelection: isSelectionVisible(showSelection),
41 });
42
43 const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeFiles, relatedTreePickers, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
44     onContextMenu: () => { return; },
45     toggleItemActive: (event, item, pickerId) => {
46         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId, relatedTreePickers }));
47         if (props.toggleItemActive) {
48             props.toggleItemActive(event, item, pickerId);
49         }
50     },
51     toggleItemOpen: (_, item, pickerId) => {
52         const { id, data, status } = item;
53         if (status === TreeItemStatus.INITIAL) {
54             if ('kind' in data) {
55                 dispatch<any>(
56                     data.kind === ResourceKind.COLLECTION
57                         ? loadCollection(id, pickerId)
58                         : loadProject({ id, pickerId, includeCollections, includeFiles })
59                 );
60             } else if (!('type' in data) && loadRootItem) {
61                 loadRootItem(item as TreeItem<ProjectsTreePickerRootItem>, pickerId, includeCollections, includeFiles);
62             }
63         } else if (status === TreeItemStatus.LOADED) {
64             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
65         }
66     },
67     toggleItemSelection: (event, item, pickerId) => {
68         dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: item.id, pickerId }));
69         if (props.toggleItemSelection) {
70             props.toggleItemSelection(event, item, pickerId);
71         }
72     },
73 });
74
75 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(TreePicker);
76
77 const getProjectPickerIcon = ({ data }: TreeItem<ProjectsTreePickerItem>, rootIcon: IconType): IconType => {
78     if ('kind' in data) {
79         switch (data.kind) {
80             case ResourceKind.COLLECTION:
81                 return CollectionIcon;
82             default:
83                 return ProjectIcon;
84         }
85     } else if ('type' in data) {
86         switch (data.type) {
87             case CollectionFileType.FILE:
88                 return InputIcon;
89             default:
90                 return ProjectIcon;
91         }
92     } else {
93         return rootIcon;
94     }
95 };
96
97 const isSelectionVisible = (shouldBeVisible?: boolean) =>
98     ({ status, items }: TreeItem<ProjectsTreePickerItem>): boolean => {
99         if (shouldBeVisible) {
100             if (items && items.length > 0) {
101                 return items.every(isSelectionVisible(shouldBeVisible));
102             }
103             return status === TreeItemStatus.LOADED;
104         }
105         return false;
106     };
107
108 const renderTreeItem = (rootItemIcon: IconType) => (item: TreeItem<ProjectResource>) =>
109     <ListItemTextIcon
110         icon={getProjectPickerIcon(item, rootItemIcon)}
111         name={item.data.name}
112         isActive={item.active}
113         hasMargin={true} />;