Add props for controlling which checkboxes should be visible
[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     rootItemSelection?: boolean;
32     projectsSelection?: boolean;
33     collectionsSelection?: boolean;
34     filesSelection?: boolean;
35     loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string, includeCollections?: boolean, inlcudeFiles?: boolean) => void;
36 }
37
38 export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;
39
40 const mapStateToProps = (_: any, { rootItemIcon, ...props }: ProjectsTreePickerProps) => ({
41     render: renderTreeItem(rootItemIcon),
42     showSelection: isSelectionVisible(props),
43 });
44
45 const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeFiles, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
46     onContextMenu: () => { return; },
47     toggleItemActive: (event, item, pickerId) => {
48         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId }));
49         if (props.toggleItemActive) {
50             props.toggleItemActive(event, item, pickerId);
51         }
52     },
53     toggleItemOpen: (_, item, pickerId) => {
54         const { id, data, status } = item;
55         if (status === TreeItemStatus.INITIAL) {
56             if ('kind' in data) {
57                 dispatch<any>(
58                     data.kind === ResourceKind.COLLECTION
59                         ? loadCollection(id, pickerId)
60                         : loadProject({ id, pickerId, includeCollections, includeFiles })
61                 );
62             } else if (!('type' in data) && loadRootItem) {
63                 loadRootItem(item as TreeItem<ProjectsTreePickerRootItem>, pickerId, includeCollections, includeFiles);
64             }
65         } else if (status === TreeItemStatus.LOADED) {
66             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
67         }
68     },
69     toggleItemSelection: (_, { id }, pickerId) => {
70         dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
71     },
72 });
73
74 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(TreePicker);
75
76 const getProjectPickerIcon = ({ data }: TreeItem<ProjectsTreePickerItem>, rootIcon: IconType): IconType => {
77     if ('kind' in data) {
78         switch (data.kind) {
79             case ResourceKind.COLLECTION:
80                 return CollectionIcon;
81             default:
82                 return ProjectIcon;
83         }
84     } else if ('type' in data) {
85         switch (data.type) {
86             case CollectionFileType.FILE:
87                 return InputIcon;
88             default:
89                 return ProjectIcon;
90         }
91     } else {
92         return rootIcon;
93     }
94 };
95 interface IsSelectionVisibleParams {
96     rootItemSelection?: boolean;
97     projectsSelection?: boolean;
98     collectionsSelection?: boolean;
99     filesSelection?: boolean;
100 }
101 const isSelectionVisible = (params: IsSelectionVisibleParams) =>
102     ({ data, status }: TreeItem<ProjectsTreePickerItem>) => {
103         if ('kind' in data) {
104             switch (data.kind) {
105                 case ResourceKind.COLLECTION:
106                     return !!params.collectionsSelection;
107                 default:
108                     return !!params.projectsSelection;
109             }
110         } else if ('type' in data) {
111             return !!params.filesSelection;
112         } else {
113             return !!params.rootItemSelection;
114         }
115     };
116
117 const renderTreeItem = (rootItemIcon: IconType) => (item: TreeItem<ProjectResource>) =>
118     <ListItemTextIcon
119         icon={getProjectPickerIcon(item, rootItemIcon)}
120         name={item.data.name}
121         isActive={item.active}
122         hasMargin={true} />;