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 { RootState } from '../store';
8 import { ServiceRepository } from '~/services/services';
9 import { FilterBuilder } from '~/services/api/filter-builder';
10 import { resourcesActions } from '../resources/resources-actions';
11 import { getTreePicker, TreePicker } from '../tree-picker/tree-picker';
12 import { getNodeAncestors, getNodeAncestorsIds, getNode, TreeNode, initTreeNode, TreeNodeStatus } from '~/models/tree';
13 import { ProjectResource } from '~/models/project';
14 import { OrderBuilder } from '../../services/api/order-builder';
16 export enum SidePanelTreeCategory {
17 PROJECTS = 'Projects',
18 SHARED_WITH_ME = 'Shared with me',
19 WORKFLOWS = 'Workflows',
20 RECENT_OPEN = 'Recently open',
21 FAVORITES = 'Favorites',
25 export const SIDE_PANEL_TREE = 'sidePanelTree';
27 export const getSidePanelTree = (treePicker: TreePicker) =>
28 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
30 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
31 const tree = getSidePanelTree(treePicker);
33 const ancestors = getNodeAncestors(uuid)(tree);
34 const node = getNode(uuid)(tree);
36 return [...ancestors, node];
42 const SIDE_PANEL_CATEGORIES = [
43 SidePanelTreeCategory.SHARED_WITH_ME,
44 SidePanelTreeCategory.WORKFLOWS,
45 SidePanelTreeCategory.RECENT_OPEN,
46 SidePanelTreeCategory.FAVORITES,
47 SidePanelTreeCategory.TRASH,
50 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
52 export const initSidePanelTree = () =>
53 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
54 const rootProjectUuid = authService.getUuid() || '';
55 const nodes = SIDE_PANEL_CATEGORIES.map(id => initTreeNode({ id, value: id }));
56 const projectsNode = initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
57 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
59 pickerId: SIDE_PANEL_TREE,
60 nodes: [projectsNode, ...nodes]
62 SIDE_PANEL_CATEGORIES.forEach(category => {
63 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
65 pickerId: SIDE_PANEL_TREE,
71 export const loadSidePanelTreeProjects = (projectUuid: string) =>
72 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
73 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
74 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
75 if (node || projectUuid === '') {
76 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
78 filters: new FilterBuilder()
79 .addEqual('ownerUuid', projectUuid)
81 order: new OrderBuilder<ProjectResource>()
85 const { items } = await services.projectService.list(params);
86 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
88 pickerId: SIDE_PANEL_TREE,
89 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
91 dispatch(resourcesActions.SET_RESOURCES(items));
95 export const activateSidePanelTreeItem = (id: string) =>
96 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
97 const node = getSidePanelTreeNode(id)(getState().treePicker);
98 if (node && !node.active) {
99 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
101 if (!isSidePanelTreeCategory(id)) {
102 await dispatch<any>(activateSidePanelTreeProject(id));
106 export const activateSidePanelTreeProject = (id: string) =>
107 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108 const { treePicker } = getState();
109 const node = getSidePanelTreeNode(id)(treePicker);
110 if (node && node.status !== TreeNodeStatus.LOADED) {
111 await dispatch<any>(loadSidePanelTreeProjects(id));
112 } else if (node === undefined) {
113 await dispatch<any>(activateSidePanelTreeBranch(id));
115 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
116 ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
117 pickerId: SIDE_PANEL_TREE
119 dispatch<any>(expandSidePanelTreeItem(id));
122 export const activateSidePanelTreeBranch = (id: string) =>
123 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124 const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
125 for (const ancestor of ancestors) {
126 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
128 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
129 ids: ancestors.map(ancestor => ancestor.uuid),
130 pickerId: SIDE_PANEL_TREE
132 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
135 export const toggleSidePanelTreeItemCollapse = (id: string) =>
136 async (dispatch: Dispatch, getState: () => RootState) => {
137 const node = getSidePanelTreeNode(id)(getState().treePicker);
138 if (node && node.status === TreeNodeStatus.INITIAL) {
139 await dispatch<any>(loadSidePanelTreeProjects(node.id));
141 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
144 export const expandSidePanelTreeItem = (id: string) =>
145 async (dispatch: Dispatch, getState: () => RootState) => {
146 const node = getSidePanelTreeNode(id)(getState().treePicker);
147 if (node && !node.expanded) {
148 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
152 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
153 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
155 ? getNode(id)(sidePanelTree)
159 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
160 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
162 ? getNodeAncestorsIds(id)(sidePanelTree)