1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { isEqual } from 'lodash/fp';
9 import { TreeItem, TreeItemStatus } from 'components/tree/tree';
10 import { ProjectResource } from "models/project";
11 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
12 import { ListItemTextIcon } from "components/list-item-text-icon/list-item-text-icon";
13 import { ProjectIcon, FileInputIcon, IconType, CollectionIcon } from 'components/icon/icon';
14 import { loadProject, loadCollection } from 'store/tree-picker/tree-picker-actions';
15 import { ProjectsTreePickerItem, ProjectsTreePickerRootItem } from 'store/tree-picker/tree-picker-middleware';
16 import { ResourceKind } from 'models/resource';
17 import { TreePickerProps, TreePicker } from "views-components/tree-picker/tree-picker";
18 import { CollectionFileType } from 'models/collection-file';
21 type PickedTreePickerProps = Pick<TreePickerProps<ProjectsTreePickerItem>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
23 export interface ProjectsTreePickerDataProps {
24 includeCollections?: boolean;
25 includeFiles?: boolean;
26 rootItemIcon: IconType;
27 showSelection?: boolean;
28 relatedTreePickers?: string[];
29 disableActivation?: string[];
30 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
31 loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string,
32 includeCollections?: boolean, includeFiles?: boolean, options?: { showOnlyOwned: boolean, showOnlyWritable: boolean }) => void;
35 export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;
37 const mapStateToProps = (_: any, { rootItemIcon, showSelection }: ProjectsTreePickerProps) => ({
38 render: renderTreeItem(rootItemIcon),
39 showSelection: isSelectionVisible(showSelection),
42 const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeFiles, relatedTreePickers, options, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
43 onContextMenu: () => { return; },
44 toggleItemActive: (event, item, pickerId) => {
46 const { disableActivation = [] } = props;
47 if (disableActivation.some(isEqual(item.id))) {
51 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId, relatedTreePickers }));
52 if (props.toggleItemActive) {
53 props.toggleItemActive(event, item, pickerId);
56 toggleItemOpen: (_, item, pickerId) => {
57 const { id, data, status } = item;
58 if (status === TreeItemStatus.INITIAL) {
61 data.kind === ResourceKind.COLLECTION
62 ? loadCollection(id, pickerId)
63 : loadProject({ id, pickerId, includeCollections, includeFiles, options })
65 } else if (!('type' in data) && loadRootItem) {
66 loadRootItem(item as TreeItem<ProjectsTreePickerRootItem>, pickerId, includeCollections, includeFiles, options);
68 } else if (status === TreeItemStatus.LOADED) {
69 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
72 toggleItemSelection: (event, item, pickerId) => {
73 dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: item.id, pickerId }));
74 if (props.toggleItemSelection) {
75 props.toggleItemSelection(event, item, pickerId);
80 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(TreePicker);
82 const getProjectPickerIcon = ({ data }: TreeItem<ProjectsTreePickerItem>, rootIcon: IconType): IconType => {
83 if ('headKind' in data) {
84 switch (data.headKind) {
85 case ResourceKind.COLLECTION:
86 return CollectionIcon;
93 case ResourceKind.COLLECTION:
94 return CollectionIcon;
98 } else if ('type' in data) {
100 case CollectionFileType.FILE:
101 return FileInputIcon;
110 const isSelectionVisible = (shouldBeVisible?: boolean) =>
111 ({ status, items }: TreeItem<ProjectsTreePickerItem>): boolean => {
112 if (shouldBeVisible) {
113 if (items && items.length > 0) {
114 return items.every(isSelectionVisible(shouldBeVisible));
116 return status === TreeItemStatus.LOADED;
121 const renderTreeItem = (rootItemIcon: IconType) => (item: TreeItem<ProjectResource>) =>
123 icon={getProjectPickerIcon(item, rootItemIcon)}
124 name={item.data.name}
125 isActive={item.active}