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 cascadeSelection: boolean;
25 includeCollections?: boolean;
26 includeDirectories?: boolean;
27 includeFiles?: boolean;
28 rootItemIcon: IconType;
29 showSelection?: boolean;
30 relatedTreePickers?: string[];
31 disableActivation?: string[];
32 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
33 loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string,
34 includeCollections?: boolean, includeDirectories?: boolean, includeFiles?: boolean, options?: { showOnlyOwned: boolean, showOnlyWritable: boolean }) => void;
37 export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;
39 const mapStateToProps = (_: any, { rootItemIcon, showSelection, cascadeSelection }: ProjectsTreePickerProps) => ({
40 render: renderTreeItem(rootItemIcon),
41 showSelection: isSelectionVisible(showSelection, cascadeSelection),
44 const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeDirectories, includeFiles, relatedTreePickers, options, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
45 onContextMenu: () => { return; },
46 toggleItemActive: (event, item, pickerId) => {
48 const { disableActivation = [] } = props;
49 if (disableActivation.some(isEqual(item.id))) {
53 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId, relatedTreePickers }));
54 if (props.toggleItemActive) {
55 props.toggleItemActive(event, item, pickerId);
58 toggleItemOpen: (_, item, pickerId) => {
59 const { id, data, status } = item;
60 if (status === TreeItemStatus.INITIAL) {
63 data.kind === ResourceKind.COLLECTION
64 ? loadCollection(id, pickerId, includeDirectories, includeFiles)
65 : loadProject({ id, pickerId, includeCollections, includeDirectories, includeFiles, options })
67 } else if (!('type' in data) && loadRootItem) {
68 loadRootItem(item as TreeItem<ProjectsTreePickerRootItem>, pickerId, includeCollections, includeDirectories, includeFiles, options);
70 } else if (status === TreeItemStatus.LOADED) {
71 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
74 toggleItemSelection: (event, item, pickerId) => {
75 dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: item.id, pickerId, cascade: props.cascadeSelection }));
76 if (props.toggleItemSelection) {
77 props.toggleItemSelection(event, item, pickerId);
82 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(TreePicker);
84 const getProjectPickerIcon = ({ data }: TreeItem<ProjectsTreePickerItem>, rootIcon: IconType): IconType => {
85 if ('headKind' in data) {
86 switch (data.headKind) {
87 case ResourceKind.COLLECTION:
88 return CollectionIcon;
95 case ResourceKind.COLLECTION:
96 return CollectionIcon;
100 } else if ('type' in data) {
102 case CollectionFileType.FILE:
103 return FileInputIcon;
112 const isSelectionVisible = (shouldBeVisible: boolean | undefined, cascadeSelection: boolean) =>
113 ({ status, items, data }: TreeItem<ProjectsTreePickerItem>): boolean => {
114 if (shouldBeVisible) {
115 if (!cascadeSelection && 'kind' in data && data.kind === ResourceKind.COLLECTION) {
116 // In non-casecade mode collections are selectable without being loaded
118 } else if (items && items.length > 0) {
119 return items.every(isSelectionVisible(shouldBeVisible, cascadeSelection));
121 return status === TreeItemStatus.LOADED;
126 const renderTreeItem = (rootItemIcon: IconType) => (item: TreeItem<ProjectResource>) =>
128 icon={getProjectPickerIcon(item, rootItemIcon)}
129 name={item.data.name}
130 isActive={item.active}