1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { Typography } from "@material-ui/core";
9 import { TreePicker, TreePickerProps } from "../tree-picker/tree-picker";
10 import { TreeItem, TreeItemStatus } from "~/components/tree/tree";
11 import { ProjectResource } from "~/models/project";
12 import { treePickerActions, loadProjectTreePickerProjects, loadFavoriteTreePickerProjects, loadPublicFavoriteTreePickerProjects } from "~/store/tree-picker/tree-picker-actions";
13 import { ListItemTextIcon } from "~/components/list-item-text-icon/list-item-text-icon";
14 import { ProjectIcon, FavoriteIcon, ProjectsIcon, ShareMeIcon, PublicFavoriteIcon } from '~/components/icon/icon';
15 import { RootState } from "~/store/store";
16 import { ServiceRepository } from "~/services/services";
17 import { WrappedFieldProps } from 'redux-form';
18 import { TreePickerId } from '~/models/tree';
19 import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
20 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
21 import { PickerIdProp } from '~/store/tree-picker/picker-id';
23 type ProjectTreePickerProps = Pick<TreePickerProps<ProjectResource>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
25 const mapDispatchToProps = (dispatch: Dispatch, props: { onChange: (projectUuid: string) => void }): ProjectTreePickerProps => ({
26 onContextMenu: () => { return; },
27 toggleItemActive: (_, { id }, pickerId) => {
28 getNotSelectedTreePickerKind(pickerId)
29 .forEach(pickerId => dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '', pickerId })));
30 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId }));
34 toggleItemOpen: (_, { id, status }, pickerId) => {
35 dispatch<any>(toggleItemOpen(id, status, pickerId));
37 toggleItemSelection: (_, { id }, pickerId) => {
38 dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
42 const toggleItemOpen = (id: string, status: TreeItemStatus, pickerId: string) =>
43 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44 if (status === TreeItemStatus.INITIAL) {
45 if (pickerId === TreePickerId.PROJECTS) {
46 dispatch<any>(loadProjectTreePickerProjects(id));
47 } else if (pickerId === TreePickerId.FAVORITES) {
48 dispatch<any>(loadFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
49 } else if (pickerId === TreePickerId.PUBLIC_FAVORITES) {
50 dispatch<any>(loadPublicFavoriteTreePickerProjects(id === services.authService.getUuid() ? '' : id));
51 // TODO: load sharedWithMe
54 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
58 const getNotSelectedTreePickerKind = (pickerId: string) => {
59 return [TreePickerId.PROJECTS, TreePickerId.FAVORITES, TreePickerId.SHARED_WITH_ME].filter(nodeId => nodeId !== pickerId);
62 export const ProjectTreePicker = connect(undefined, mapDispatchToProps)((props: ProjectTreePickerProps) =>
63 <div style={{ display: 'flex', flexDirection: 'column' }}>
64 <Typography variant='caption' style={{ flexShrink: 0 }}>
67 <div style={{ flexGrow: 1, overflow: 'auto' }}>
68 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PROJECTS} />
69 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.SHARED_WITH_ME} />
70 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.FAVORITES} />
71 <TreePicker {...props} render={renderTreeItem} pickerId={TreePickerId.PUBLIC_FAVORITES} />
75 const getProjectPickerIcon = (item: TreeItem<ProjectResource>) => {
76 switch (item.data.name) {
77 case TreePickerId.FAVORITES:
79 case TreePickerId.PROJECTS:
81 case TreePickerId.SHARED_WITH_ME:
83 case TreePickerId.PUBLIC_FAVORITES:
84 return PublicFavoriteIcon;
90 const renderTreeItem = (item: TreeItem<ProjectResource>) =>
92 icon={getProjectPickerIcon(item)}
93 name={typeof item.data === 'string' ? item.data : item.data.name}
94 isActive={item.active}
97 export const ProjectTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
98 <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
100 pickerId={props.pickerId}
101 toggleItemActive={handleChange(props)} />
102 {props.meta.dirty && props.meta.error &&
103 <Typography variant='caption' color='error'>
108 const handleChange = (props: WrappedFieldProps) =>
109 (_: any, { id }: TreeItem<ProjectsTreePickerItem>) =>
110 props.input.onChange(id);
112 export const CollectionTreePickerField = (props: WrappedFieldProps & PickerIdProp) =>
113 <div style={{ height: '200px', display: 'flex', flexDirection: 'column' }}>
115 pickerId={props.pickerId}
116 toggleItemActive={handleChange(props)}
117 includeCollections />
118 {props.meta.dirty && props.meta.error &&
119 <Typography variant='caption' color='error'>