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 } from '~/store/tree-picker/tree-picker';
8 import { RootState } from '../store';
9 import { ServiceRepository } from '~/services/services';
10 import { FilterBuilder } from '~/common/api/filter-builder';
11 import { resourcesActions } from '../resources/resources-actions';
12 import { getNodeValue } from '../../models/tree';
13 import { getTreePicker } from '../tree-picker/tree-picker';
14 import { TreeItemStatus } from "~/components/tree/tree";
16 export enum SidePanelTreeCategory {
17 PROJECTS = 'Projects',
18 SHARED_WITH_ME = 'Shared with me',
19 WORKFLOWS = 'Workflows',
20 RECENT_OPEN = 'Recent open',
21 FAVORITES = 'Favorites',
25 export const SIDE_PANEL_TREE = 'sidePanelTree';
27 const SIDE_PANEL_CATEGORIES = [
28 SidePanelTreeCategory.SHARED_WITH_ME,
29 SidePanelTreeCategory.WORKFLOWS,
30 SidePanelTreeCategory.RECENT_OPEN,
31 SidePanelTreeCategory.FAVORITES,
32 SidePanelTreeCategory.TRASH,
35 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
37 export const initSidePanelTree = () =>
38 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
39 const rootProjectUuid = authService.getUuid() || '';
40 const nodes = SIDE_PANEL_CATEGORIES.map(nodeId => createTreePickerNode({ nodeId, value: nodeId }));
41 const projectsNode = createTreePickerNode({ nodeId: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
42 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
44 pickerId: SIDE_PANEL_TREE,
45 nodes: [projectsNode, ...nodes]
47 SIDE_PANEL_CATEGORIES.forEach(category => {
48 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
50 pickerId: SIDE_PANEL_TREE,
56 export const loadSidePanelTreeProjects = (projectUuid: string) =>
57 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId: projectUuid, pickerId: SIDE_PANEL_TREE }));
60 filters: new FilterBuilder()
61 .addEqual('ownerUuid', projectUuid)
64 const { items } = await services.projectService.list(params);
65 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
67 pickerId: SIDE_PANEL_TREE,
68 nodes: items.map(item => createTreePickerNode({ nodeId: item.uuid, value: item })),
70 dispatch(resourcesActions.SET_RESOURCES(items));
73 export const activateSidePanelTreeItem = (nodeId: string) =>
74 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75 const node = getSidePanelTreeNode(nodeId)(getState());
76 if (node && !node.selected) {
77 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId: SIDE_PANEL_TREE }));
79 if (!isSidePanelTreeCategory(nodeId)) {
80 dispatch<any>(activateSidePanelTreeProject(nodeId));
84 export const activateSidePanelTreeProject = (nodeId: string) =>
85 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
86 const node = getSidePanelTreeNode(nodeId)(getState());
87 if (node && node.status !== TreeItemStatus.LOADED) {
88 await dispatch<any>(loadSidePanelTreeProjects(nodeId));
90 dispatch<any>(toggleSidePanelTreeItemCollapse(nodeId));
92 } else if (node === undefined) {
93 dispatch<any>(activateSidePanelTreeBranch(nodeId));
97 export const activateSidePanelTreeBranch = (nodeId: string) =>
98 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
99 const ancestors = await services.ancestorsService.ancestors(nodeId, services.authService.getUuid() || '');
100 for (const ancestor of ancestors) {
101 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
103 for (const ancestor of ancestors) {
104 dispatch<any>(toggleSidePanelTreeItemCollapse(ancestor.uuid));
106 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId: SIDE_PANEL_TREE }));
109 const getSidePanelTreeNode = (nodeId: string) => (state: RootState) => {
110 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(state.treePicker);
112 ? getNodeValue(nodeId)(sidePanelTree)
116 export const toggleSidePanelTreeItemCollapse = (nodeId: string) =>
117 async (dispatch: Dispatch, getState: () => RootState) => {
118 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId: SIDE_PANEL_TREE }));