Merge branch 'github-pr-224'
[arvados.git] / services / workbench2 / src / views-components / side-panel-tree / side-panel-tree.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { TreePicker, TreePickerProps } from "../tree-picker/tree-picker";
9 import { TreeItem } from "components/tree/tree";
10 import { ProjectResource } from "models/project";
11 import { ListItemTextIcon } from "components/list-item-text-icon/list-item-text-icon";
12 import { ProcessIcon, ProjectIcon, FilterGroupIcon, FavoriteIcon, ProjectsIcon, ShareMeIcon, TrashIcon, PublicFavoriteIcon, GroupsIcon, TerminalIcon, ResourceIcon } from 'components/icon/icon';
13 import { activateSidePanelTreeItem, toggleSidePanelTreeItemCollapse, SIDE_PANEL_TREE, SidePanelTreeCategory } from 'store/side-panel-tree/side-panel-tree-actions';
14 import { openSidePanelContextMenu } from 'store/context-menu/context-menu-actions';
15 import { noop } from 'lodash';
16 import { ResourceKind } from "models/resource";
17 import { IllegalNamingWarning } from "components/warning/warning";
18 import { GroupClass } from "models/group";
19
20 export interface SidePanelTreeProps {
21     onItemActivation: (id: string) => void;
22     sidePanelProgress?: boolean;
23     isCollapsed?: boolean
24     setCurrentSideWidth: (width: number) => void
25 }
26
27 type SidePanelTreeActionProps = Pick<TreePickerProps<ProjectResource | string>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
28
29 const mapDispatchToProps = (dispatch: Dispatch, props: SidePanelTreeProps): SidePanelTreeActionProps => ({
30     onContextMenu: (event, { id }) => {
31         dispatch<any>(openSidePanelContextMenu(event, id));
32     },
33     toggleItemActive: (_, { id }) => {
34         dispatch<any>(activateSidePanelTreeItem(id));
35         props.onItemActivation(id);
36     },
37     toggleItemOpen: (_, { id }) => {
38         dispatch<any>(toggleSidePanelTreeItemCollapse(id));
39     },
40     toggleItemSelection: noop,
41 });
42
43 export const SidePanelTree = connect(undefined, mapDispatchToProps)(
44     (props: SidePanelTreeActionProps) =>
45         <div data-cy="side-panel-tree">
46             <TreePicker {...props} render={renderSidePanelItem} pickerId={SIDE_PANEL_TREE} />
47         </div>);
48
49 const renderSidePanelItem = (item: TreeItem<ProjectResource>) => {
50     const name = typeof item.data === 'string' ? item.data : item.data.name;
51     const warn = typeof item.data !== 'string' && item.data.kind === ResourceKind.PROJECT
52         ? <IllegalNamingWarning name={name} />
53         : undefined;
54     return <ListItemTextIcon
55         icon={getProjectPickerIcon(item)}
56         name={name}
57         nameDecorator={warn}
58         isActive={item.active}
59         hasMargin={true}
60     />;
61 };
62
63 const getProjectPickerIcon = (item: TreeItem<ProjectResource | string>) =>
64     typeof item.data === 'string'
65         ? getSidePanelIcon(item.data)
66         : (item.data && item.data.groupClass === GroupClass.FILTER)
67             ? FilterGroupIcon
68             : ProjectsIcon;
69
70 export const getSidePanelIcon = (category: string) => {
71     switch (category) {
72         case SidePanelTreeCategory.FAVORITES:
73             return FavoriteIcon;
74         case SidePanelTreeCategory.PROJECTS:
75             return ProjectsIcon;
76         case SidePanelTreeCategory.SHARED_WITH_ME:
77             return ShareMeIcon;
78         case SidePanelTreeCategory.TRASH:
79             return TrashIcon;
80         case SidePanelTreeCategory.PUBLIC_FAVORITES:
81             return PublicFavoriteIcon;
82         case SidePanelTreeCategory.ALL_PROCESSES:
83             return ProcessIcon;
84         case SidePanelTreeCategory.INSTANCE_TYPES:
85             return ResourceIcon;
86         case SidePanelTreeCategory.GROUPS:
87             return GroupsIcon;
88         case SidePanelTreeCategory.SHELL_ACCESS:
89             return TerminalIcon
90         default:
91             return ProjectIcon;
92     }
93 };