recent-open-sub-menu-title-does-not-have-the-correct-form
[arvados.git] / src / store / side-panel-tree / side-panel-tree-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
16
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',
23     TRASH = 'Trash'
24 }
25
26 export const SIDE_PANEL_TREE = 'sidePanelTree';
27
28 export const getSidePanelTree = (treePicker: TreePicker) =>
29     getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
30
31 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreePickerNode<ProjectResource | string>> => {
32     const tree = getSidePanelTree(treePicker);
33     if (tree) {
34         const ancestors = getNodeAncestors(uuid)(tree).map(node => node.value);
35         const node = getNodeValue(uuid)(tree);
36         if (node) {
37             return [...ancestors, node];
38         }
39     }
40     return [];
41 };
42
43 const SIDE_PANEL_CATEGORIES = [
44     SidePanelTreeCategory.SHARED_WITH_ME,
45     SidePanelTreeCategory.WORKFLOWS,
46     SidePanelTreeCategory.RECENT_OPEN,
47     SidePanelTreeCategory.FAVORITES,
48     SidePanelTreeCategory.TRASH,
49 ];
50
51 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
52
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({
59             nodeId: '',
60             pickerId: SIDE_PANEL_TREE,
61             nodes: [projectsNode, ...nodes]
62         }));
63         SIDE_PANEL_CATEGORIES.forEach(category => {
64             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
65                 nodeId: category,
66                 pickerId: SIDE_PANEL_TREE,
67                 nodes: []
68             }));
69         });
70     };
71
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 }));
78             const params = {
79                 filters: new FilterBuilder()
80                     .addEqual('ownerUuid', projectUuid)
81                     .getFilters()
82             };
83             const { items } = await services.projectService.list(params);
84             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
85                 nodeId: projectUuid,
86                 pickerId: SIDE_PANEL_TREE,
87                 nodes: items.map(item => createTreePickerNode({ nodeId: item.uuid, value: item })),
88             }));
89             dispatch(resourcesActions.SET_RESOURCES(items));
90         }
91     };
92
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 }));
98         }
99         if (!isSidePanelTreeCategory(nodeId)) {
100             await dispatch<any>(activateSidePanelTreeProject(nodeId));
101         }
102     };
103
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));
112         }
113         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
114             nodeIds: getSidePanelTreeNodeAncestorsIds(nodeId)(treePicker),
115             pickerId: SIDE_PANEL_TREE
116         }));
117         dispatch<any>(expandSidePanelTreeItem(nodeId));
118     };
119
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));
125         }
126         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
127             nodeIds: ancestors.map(ancestor => ancestor.uuid),
128             pickerId: SIDE_PANEL_TREE
129         }));
130         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId, pickerId: SIDE_PANEL_TREE }));
131     };
132
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));
138         }
139         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId, pickerId: SIDE_PANEL_TREE }));
140     };
141
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 }));
147         }
148     };
149
150 const getSidePanelTreeNode = (nodeId: string) => (treePicker: TreePicker) => {
151     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
152     return sidePanelTree
153         ? getNodeValue(nodeId)(sidePanelTree)
154         : undefined;
155 };
156
157 const getSidePanelTreeNodeAncestorsIds = (nodeId: string) => (treePicker: TreePicker) => {
158     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
159     return sidePanelTree
160         ? getNodeAncestorsIds(nodeId)(sidePanelTree)
161         : [];
162 };