Switch shared with me root item endpoint
[arvados-workbench2.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 { RootState } from '../store';
8 import { ServiceRepository } from '~/services/services';
9 import { FilterBuilder, joinFilters } 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';
15 import { ResourceKind } from '~/models/resource';
16 import { GroupContentsResourcePrefix } from '../../services/groups-service/groups-service';
17 import { GroupClass } from '../../models/group';
18
19 export enum SidePanelTreeCategory {
20     PROJECTS = 'Projects',
21     SHARED_WITH_ME = 'Shared with me',
22     WORKFLOWS = 'Workflows',
23     RECENT_OPEN = 'Recently open',
24     FAVORITES = 'Favorites',
25     TRASH = 'Trash'
26 }
27
28 export const SIDE_PANEL_TREE = 'sidePanelTree';
29
30 export const getSidePanelTree = (treePicker: TreePicker) =>
31     getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
32
33 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
34     const tree = getSidePanelTree(treePicker);
35     if (tree) {
36         const ancestors = getNodeAncestors(uuid)(tree);
37         const node = getNode(uuid)(tree);
38         if (node) {
39             return [...ancestors, node];
40         }
41     }
42     return [];
43 };
44
45 const SIDE_PANEL_CATEGORIES = [
46     SidePanelTreeCategory.WORKFLOWS,
47     SidePanelTreeCategory.RECENT_OPEN,
48     SidePanelTreeCategory.FAVORITES,
49     SidePanelTreeCategory.TRASH,
50 ];
51
52 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
53
54 export const initSidePanelTree = () =>
55     (dispatch: Dispatch, _: () => RootState, { authService }: ServiceRepository) => {
56         const rootProjectUuid = authService.getUuid() || '';
57         const nodes = SIDE_PANEL_CATEGORIES.map(id => initTreeNode({ id, value: id }));
58         const projectsNode = initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
59         const sharedNode = initTreeNode({ id: SidePanelTreeCategory.SHARED_WITH_ME, value: SidePanelTreeCategory.SHARED_WITH_ME });
60         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
61             id: '',
62             pickerId: SIDE_PANEL_TREE,
63             nodes: [projectsNode, sharedNode, ...nodes]
64         }));
65         SIDE_PANEL_CATEGORIES.forEach(category => {
66             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
67                 id: category,
68                 pickerId: SIDE_PANEL_TREE,
69                 nodes: []
70             }));
71         });
72     };
73
74 export const loadSidePanelTreeProjects = (projectUuid: string) =>
75     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76         const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
77         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
78         if (projectUuid === SidePanelTreeCategory.SHARED_WITH_ME) {
79             await dispatch<any>(loadSharedRoot);
80         } else if (node || projectUuid === '') {
81             await dispatch<any>(loadProject(projectUuid));
82         }
83     };
84
85 const loadProject = (projectUuid: string) =>
86     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
87         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
88         const params = {
89             filters: new FilterBuilder()
90                 .addEqual('ownerUuid', projectUuid)
91                 .getFilters(),
92             order: new OrderBuilder<ProjectResource>()
93                 .addAsc('name')
94                 .getOrder()
95         };
96         const { items } = await services.projectService.list(params);
97         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
98             id: projectUuid,
99             pickerId: SIDE_PANEL_TREE,
100             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
101         }));
102         dispatch(resourcesActions.SET_RESOURCES(items));
103     };
104
105 const loadSharedRoot = async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
106     dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.SHARED_WITH_ME, pickerId: SIDE_PANEL_TREE }));
107
108     const params = {
109         filters:  `[${new FilterBuilder()
110             .addIsA('uuid', ResourceKind.PROJECT)
111             .addEqual('groupClass', GroupClass.PROJECT)
112             .getFilters()}]`,
113         order: new OrderBuilder<ProjectResource>()
114             .addAsc('name', GroupContentsResourcePrefix.PROJECT)
115             .getOrder(),
116     };
117
118     const { items } = await services.groupsService.shared(params);
119
120     dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
121         id: SidePanelTreeCategory.SHARED_WITH_ME,
122         pickerId: SIDE_PANEL_TREE,
123         nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
124     }));
125
126     dispatch(resourcesActions.SET_RESOURCES(items));
127 };
128
129 export const activateSidePanelTreeItem = (id: string) =>
130     async (dispatch: Dispatch, getState: () => RootState) => {
131         const node = getSidePanelTreeNode(id)(getState().treePicker);
132         if (node && !node.active) {
133             dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
134         }
135         if (!isSidePanelTreeCategory(id)) {
136             await dispatch<any>(activateSidePanelTreeProject(id));
137         }
138     };
139
140 export const activateSidePanelTreeProject = (id: string) =>
141     async (dispatch: Dispatch, getState: () => RootState) => {
142         const { treePicker } = getState();
143         const node = getSidePanelTreeNode(id)(treePicker);
144         if (node && node.status !== TreeNodeStatus.LOADED) {
145             await dispatch<any>(loadSidePanelTreeProjects(id));
146         } else if (node === undefined) {
147             await dispatch<any>(activateSidePanelTreeBranch(id));
148         }
149         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
150             ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
151             pickerId: SIDE_PANEL_TREE
152         }));
153         dispatch<any>(expandSidePanelTreeItem(id));
154     };
155
156 export const activateSidePanelTreeBranch = (id: string) =>
157     async (dispatch: Dispatch, _: void, services: ServiceRepository) => {
158         const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
159         const isShared = ancestors.every(({ uuid }) => uuid !== services.authService.getUuid());
160         if (isShared) {
161             await dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.SHARED_WITH_ME));
162         }
163         for (const ancestor of ancestors) {
164             await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
165         }
166         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
167             ids: [
168                 ...(isShared ? [SidePanelTreeCategory.SHARED_WITH_ME] : []),
169                 ...ancestors.map(ancestor => ancestor.uuid)
170             ],
171             pickerId: SIDE_PANEL_TREE
172         }));
173         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
174     };
175
176 export const toggleSidePanelTreeItemCollapse = (id: string) =>
177     async (dispatch: Dispatch, getState: () => RootState) => {
178         const node = getSidePanelTreeNode(id)(getState().treePicker);
179         if (node && node.status === TreeNodeStatus.INITIAL) {
180             await dispatch<any>(loadSidePanelTreeProjects(node.id));
181         }
182         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
183     };
184
185 export const expandSidePanelTreeItem = (id: string) =>
186     async (dispatch: Dispatch, getState: () => RootState) => {
187         const node = getSidePanelTreeNode(id)(getState().treePicker);
188         if (node && !node.expanded) {
189             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
190         }
191     };
192
193 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
194     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
195     return sidePanelTree
196         ? getNode(id)(sidePanelTree)
197         : undefined;
198 };
199
200 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
201     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
202     return sidePanelTree
203         ? getNodeAncestorsIds(id)(sidePanelTree)
204         : [];
205 };