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 { 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";
19 export interface ProjectsTreePickerRootItem {
24 export type ProjectsTreePickerItem = ProjectsTreePickerRootItem | GroupContentsResource | CollectionDirectory | CollectionFile;
25 type PickedTreePickerProps = Pick<TreePickerProps<ProjectsTreePickerItem>, 'onContextMenu' | 'toggleItemActive' | 'toggleItemOpen' | 'toggleItemSelection'>;
27 export interface ProjectsTreePickerDataProps {
28 includeCollections?: boolean;
29 includeFiles?: boolean;
30 rootItemIcon: IconType;
31 showSelection?: boolean;
32 relatedTreePickers?: string[];
33 loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string, includeCollections?: boolean, inlcudeFiles?: boolean) => void;
36 export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;
38 const mapStateToProps = (_: any, { rootItemIcon, showSelection }: ProjectsTreePickerProps) => ({
39 render: renderTreeItem(rootItemIcon),
40 showSelection: isSelectionVisible(showSelection),
43 const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeFiles, relatedTreePickers, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
44 onContextMenu: () => { return; },
45 toggleItemActive: (event, item, pickerId) => {
46 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId, relatedTreePickers }));
47 if (props.toggleItemActive) {
48 props.toggleItemActive(event, item, pickerId);
51 toggleItemOpen: (_, item, pickerId) => {
52 const { id, data, status } = item;
53 if (status === TreeItemStatus.INITIAL) {
56 data.kind === ResourceKind.COLLECTION
57 ? loadCollection(id, pickerId)
58 : loadProject({ id, pickerId, includeCollections, includeFiles })
60 } else if (!('type' in data) && loadRootItem) {
61 loadRootItem(item as TreeItem<ProjectsTreePickerRootItem>, pickerId, includeCollections, includeFiles);
63 } else if (status === TreeItemStatus.LOADED) {
64 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
67 toggleItemSelection: (event, item, pickerId) => {
68 dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: item.id, pickerId }));
69 if (props.toggleItemSelection) {
70 props.toggleItemSelection(event, item, pickerId);
75 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(TreePicker);
77 const getProjectPickerIcon = ({ data }: TreeItem<ProjectsTreePickerItem>, rootIcon: IconType): IconType => {
80 case ResourceKind.COLLECTION:
81 return CollectionIcon;
85 } else if ('type' in data) {
87 case CollectionFileType.FILE:
97 const isSelectionVisible = (shouldBeVisible?: boolean) =>
98 ({ status, items }: TreeItem<ProjectsTreePickerItem>): boolean => {
99 if (shouldBeVisible) {
100 if (items && items.length > 0) {
101 return items.every(isSelectionVisible(shouldBeVisible));
103 return status === TreeItemStatus.LOADED;
108 const renderTreeItem = (rootItemIcon: IconType) => (item: TreeItem<ProjectResource>) =>
110 icon={getProjectPickerIcon(item, rootItemIcon)}
111 name={item.data.name}
112 isActive={item.active}