1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
7 import { createTreePickerNode, TreePickerNode } from '~/store/tree-picker/tree-picker';
8 import { RootState } from '../store';
9 import { ServiceRepository } from '~/services/services';
10 import { FilterBuilder } from '~/services/api/filter-builder';
11 import { resourcesActions } from '../resources/resources-actions';
12 import { getTreePicker, TreePicker } from '../tree-picker/tree-picker';
13 import { TreeItemStatus } from "~/components/tree/tree";
14 import { getNodeAncestors, getNodeValue, getNodeAncestorsIds, getNode } from '~/models/tree';
15 import { ProjectResource } from '~/models/project';
17 export enum SidePanelTreeCategory {
18 PROJECTS = 'Projects',
19 SHARED_WITH_ME = 'Shared with me',
20 WORKFLOWS = 'Workflows',
21 RECENT_OPEN = 'Recently open',
22 FAVORITES = 'Favorites',
26 export const SIDE_PANEL_TREE = 'sidePanelTree';
28 export const getSidePanelTree = (treePicker: TreePicker) =>
29 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
31 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreePickerNode<ProjectResource | string>> => {
32 const tree = getSidePanelTree(treePicker);
34 const ancestors = getNodeAncestors(uuid)(tree).map(node => node.value);
35 const node = getNodeValue(uuid)(tree);
37 return [...ancestors, node];
43 const SIDE_PANEL_CATEGORIES = [
44 SidePanelTreeCategory.SHARED_WITH_ME,
45 SidePanelTreeCategory.WORKFLOWS,
46 SidePanelTreeCategory.RECENT_OPEN,
47 SidePanelTreeCategory.FAVORITES,
48 SidePanelTreeCategory.TRASH,
51 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
53 export const initSidePanelTree = () =>
54 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
55 const rootProjectUuid = authService.getUuid() || '';
56 const nodes = SIDE_PANEL_CATEGORIES.map(nodeId => createTreePickerNode({ nodeId, value: nodeId }));
57 const projectsNode = createTreePickerNode({ nodeId: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
58 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
60 pickerId: SIDE_PANEL_TREE,
61 nodes: [projectsNode, ...nodes]
63 SIDE_PANEL_CATEGORIES.forEach(category => {
64 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
66 pickerId: SIDE_PANEL_TREE,
72 export const loadSidePanelTreeProjects = (projectUuid: string) =>
73 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
75 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
76 if (node || projectUuid === '') {
77 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId: projectUuid, pickerId: SIDE_PANEL_TREE }));
79 filters: new FilterBuilder()
80 .addEqual('ownerUuid', projectUuid)
83 const { items } = await services.projectService.list(params);
84 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
86 pickerId: SIDE_PANEL_TREE,
87 nodes: items.map(item => createTreePickerNode({ nodeId: item.uuid, value: item })),
89 dispatch(resourcesActions.SET_RESOURCES(items));
93 export const activateSidePanelTreeItem = (nodeId: string) =>
94 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
95 const node = getSidePanelTreeNode(nodeId)(getState().treePicker);
96 if (node && !node.selected) {
97 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId: SIDE_PANEL_TREE }));
99 if (!isSidePanelTreeCategory(nodeId)) {
100 await dispatch<any>(activateSidePanelTreeProject(nodeId));
104 export const activateSidePanelTreeProject = (nodeId: string) =>
105 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
106 const { treePicker } = getState();
107 const node = getSidePanelTreeNode(nodeId)(treePicker);
108 if (node && node.status !== TreeItemStatus.LOADED) {
109 await dispatch<any>(loadSidePanelTreeProjects(nodeId));
110 } else if (node === undefined) {
111 await dispatch<any>(activateSidePanelTreeBranch(nodeId));
113 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
114 nodeIds: getSidePanelTreeNodeAncestorsIds(nodeId)(treePicker),
115 pickerId: SIDE_PANEL_TREE
117 dispatch<any>(expandSidePanelTreeItem(nodeId));
120 export const activateSidePanelTreeBranch = (nodeId: string) =>
121 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
122 const ancestors = await services.ancestorsService.ancestors(nodeId, services.authService.getUuid() || '');
123 for (const ancestor of ancestors) {
124 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
126 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
127 nodeIds: ancestors.map(ancestor => ancestor.uuid),
128 pickerId: SIDE_PANEL_TREE
130 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId: SIDE_PANEL_TREE }));
133 export const toggleSidePanelTreeItemCollapse = (nodeId: string) =>
134 async (dispatch: Dispatch, getState: () => RootState) => {
135 const node = getSidePanelTreeNode(nodeId)(getState().treePicker);
136 if (node && node.status === TreeItemStatus.INITIAL) {
137 await dispatch<any>(loadSidePanelTreeProjects(node.nodeId));
139 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId: SIDE_PANEL_TREE }));
142 export const expandSidePanelTreeItem = (nodeId: string) =>
143 async (dispatch: Dispatch, getState: () => RootState) => {
144 const node = getSidePanelTreeNode(nodeId)(getState().treePicker);
145 if (node && node.collapsed) {
146 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId: SIDE_PANEL_TREE }));
150 const getSidePanelTreeNode = (nodeId: string) => (treePicker: TreePicker) => {
151 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
153 ? getNodeValue(nodeId)(sidePanelTree)
157 const getSidePanelTreeNodeAncestorsIds = (nodeId: string) => (treePicker: TreePicker) => {
158 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
160 ? getNodeAncestorsIds(nodeId)(sidePanelTree)