Merge branch '21128-toolbar-context-menu'
[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/store';
8 import { getUserUuid } from "common/getuser";
9 import { ServiceRepository } from 'services/services';
10 import { FilterBuilder } from 'services/api/filter-builder';
11 import { resourcesActions } from 'store/resources/resources-actions';
12 import { getTreePicker, TreePicker } from 'store/tree-picker/tree-picker';
13 import { getNodeAncestors, getNodeAncestorsIds, getNode, TreeNode, initTreeNode, TreeNodeStatus } from 'models/tree';
14 import { ProjectResource } from 'models/project';
15 import { OrderBuilder } from 'services/api/order-builder';
16 import { ResourceKind } from 'models/resource';
17 import { CategoriesListReducer } from 'common/plugintypes';
18 import { pluginConfig } from 'plugins';
19 import { LinkClass } from 'models/link';
20
21 export enum SidePanelTreeCategory {
22     PROJECTS = 'Home Projects',
23     FAVORITES = 'My Favorites',
24     PUBLIC_FAVORITES = 'Public Favorites',
25     SHARED_WITH_ME = 'Shared with me',
26     ALL_PROCESSES = 'All Processes',
27     SHELL_ACCESS = 'Shell Access',
28     GROUPS = 'Groups',
29     TRASH = 'Trash',
30 }
31
32 export const SIDE_PANEL_TREE = 'sidePanelTree';
33 const SIDEPANEL_TREE_NODE_LIMIT = 50
34
35 export const getSidePanelTree = (treePicker: TreePicker) =>
36     getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
37
38 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
39     const tree = getSidePanelTree(treePicker);
40     if (tree) {
41         const ancestors = getNodeAncestors(uuid)(tree);
42         const node = getNode(uuid)(tree);
43         if (node) {
44             return [...ancestors, node];
45         }
46     }
47     return [];
48 };
49
50 let SIDE_PANEL_CATEGORIES: string[] = [
51     SidePanelTreeCategory.PROJECTS,
52     SidePanelTreeCategory.FAVORITES,
53     SidePanelTreeCategory.PUBLIC_FAVORITES,
54     SidePanelTreeCategory.SHARED_WITH_ME,
55     SidePanelTreeCategory.ALL_PROCESSES,
56     SidePanelTreeCategory.SHELL_ACCESS,
57     SidePanelTreeCategory.GROUPS,
58     SidePanelTreeCategory.TRASH
59 ];
60
61 const reduceCatsFn: (a: string[],
62     b: CategoriesListReducer) => string[] = (a, b) => b(a);
63
64 SIDE_PANEL_CATEGORIES = pluginConfig.sidePanelCategories.reduce(reduceCatsFn, SIDE_PANEL_CATEGORIES);
65
66 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
67
68
69 export const initSidePanelTree = () =>
70     (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
71         const rootProjectUuid = getUserUuid(getState());
72         if (!rootProjectUuid) { return; }
73         const nodes = SIDE_PANEL_CATEGORIES.map(id => {
74             if (id === SidePanelTreeCategory.PROJECTS) {
75                 return initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
76             } else {
77                 return initTreeNode({ id, value: id });
78             }
79         });
80         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
81             id: '',
82             pickerId: SIDE_PANEL_TREE,
83             nodes
84         }));
85         SIDE_PANEL_CATEGORIES.forEach(category => {
86                 if (category !== SidePanelTreeCategory.PROJECTS && category !== SidePanelTreeCategory.FAVORITES && category !== SidePanelTreeCategory.PUBLIC_FAVORITES ) {
87                 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
88                     id: category,
89                     pickerId: SIDE_PANEL_TREE,
90                     nodes: []
91                 }));
92             }
93         });
94     };
95
96 export const loadSidePanelTreeProjects = (projectUuid: string) =>
97     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
98         const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
99         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
100         if (projectUuid === SidePanelTreeCategory.PUBLIC_FAVORITES) {
101             await dispatch<any>(loadPublicFavoritesTree());
102         } else if (projectUuid === SidePanelTreeCategory.FAVORITES) {
103             await dispatch<any>(loadFavoritesTree());
104         } else if (node || projectUuid !== '') {
105             await dispatch<any>(loadProject(projectUuid));
106         }
107     };
108
109 const loadProject = (projectUuid: string) =>
110     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
111         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
112         const params = {
113             filters: new FilterBuilder()
114                 .addEqual('owner_uuid', projectUuid)
115                 .getFilters(),
116             order: new OrderBuilder<ProjectResource>()
117                 .addDesc('createdAt')
118                 .getOrder(),
119             limit: SIDEPANEL_TREE_NODE_LIMIT,
120         };
121
122         const { items } = await services.projectService.list(params);
123         
124         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
125             id: projectUuid,
126             pickerId: SIDE_PANEL_TREE,
127             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
128         }));
129         dispatch(resourcesActions.SET_RESOURCES(items));
130     };
131
132 export const loadFavoritesTree = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
133     dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.FAVORITES, pickerId: SIDE_PANEL_TREE }));
134
135     const params = {
136         filters: new FilterBuilder()
137             .addEqual('link_class', LinkClass.STAR)
138             .addEqual('tail_uuid', getUserUuid(getState()))
139             .addEqual('tail_kind', ResourceKind.USER)
140             .getFilters(),
141         order: new OrderBuilder<ProjectResource>().addDesc('createdAt').getOrder(),
142         limit: SIDEPANEL_TREE_NODE_LIMIT,
143     };
144
145     const { items } = await services.linkService.list(params);
146
147     dispatch(
148         treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
149             id: SidePanelTreeCategory.FAVORITES,
150             pickerId: SIDE_PANEL_TREE,
151             nodes: items.map(item => initTreeNode({ id: item.headUuid, value: item })),
152         })
153     );
154
155     dispatch(resourcesActions.SET_RESOURCES(items));
156 };
157
158 export const loadPublicFavoritesTree = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
159     dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.PUBLIC_FAVORITES, pickerId: SIDE_PANEL_TREE }));
160
161     const uuidPrefix = getState().auth.config.uuidPrefix;
162     const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
163     const typeFilters = [ResourceKind.COLLECTION, ResourceKind.CONTAINER_REQUEST, ResourceKind.GROUP, ResourceKind.WORKFLOW];
164
165     const params = {
166         filters: new FilterBuilder()
167             .addEqual('link_class', LinkClass.STAR)
168             .addEqual('owner_uuid', publicProjectUuid)
169             .addIsA('head_uuid', typeFilters)
170             .getFilters(),
171         order: new OrderBuilder<ProjectResource>().addDesc('createdAt').getOrder(),
172         limit: SIDEPANEL_TREE_NODE_LIMIT,
173     };
174
175     const { items } = await services.linkService.list(params);
176
177     dispatch(
178         treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
179             id: SidePanelTreeCategory.PUBLIC_FAVORITES,
180             pickerId: SIDE_PANEL_TREE,
181             nodes: items.map(item => initTreeNode({ id: item.headUuid, value: item })),
182         })
183     );
184
185     dispatch(resourcesActions.SET_RESOURCES(items));
186 };
187
188 export const activateSidePanelTreeItem = (id: string) =>
189     async (dispatch: Dispatch, getState: () => RootState) => {
190         const node = getSidePanelTreeNode(id)(getState().treePicker);
191         if (node && !node.active) {
192         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
193     }
194     if (!isSidePanelTreeCategory(id)) {
195             await dispatch<any>(activateSidePanelTreeProject(id));
196         }
197     };
198
199 export const activateSidePanelTreeProject = (id: string) =>
200     async (dispatch: Dispatch, getState: () => RootState) => {
201         const { treePicker } = getState();
202         const node = getSidePanelTreeNode(id)(treePicker);
203         if (node && node.status !== TreeNodeStatus.LOADED) {
204             await dispatch<any>(loadSidePanelTreeProjects(id));
205         } else if (node === undefined) {
206             await dispatch<any>(activateSidePanelTreeBranch(id));
207         }
208         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
209             ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
210             pickerId: SIDE_PANEL_TREE
211         }));
212         dispatch<any>(expandSidePanelTreeItem(id));
213     };
214
215 export const activateSidePanelTreeBranch = (id: string) =>
216     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
217         const userUuid = getUserUuid(getState());
218         if (!userUuid) { return; }
219         const ancestors = await services.ancestorsService.ancestors(id, userUuid);
220         for (const ancestor of ancestors) {
221             await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
222         }
223         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
224             ids: ancestors.map(ancestor => ancestor.uuid),
225             pickerId: SIDE_PANEL_TREE
226         }));
227         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
228     };
229
230 export const toggleSidePanelTreeItemCollapse = (id: string) =>
231     async (dispatch: Dispatch, getState: () => RootState) => {
232         const node = getSidePanelTreeNode(id)(getState().treePicker);
233         if (node && node.status === TreeNodeStatus.INITIAL) {
234             await dispatch<any>(loadSidePanelTreeProjects(node.id));
235         }
236             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
237     };
238
239 export const expandSidePanelTreeItem = (id: string) =>
240     async (dispatch: Dispatch, getState: () => RootState) => {
241         const node = getSidePanelTreeNode(id)(getState().treePicker);
242         if (node && !node.expanded) {
243             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
244         }
245     };
246
247 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
248     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
249     return sidePanelTree
250         ? getNode(id)(sidePanelTree)
251         : undefined;
252 };
253
254 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
255     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
256     return sidePanelTree
257         ? getNodeAncestorsIds(id)(sidePanelTree)
258         : [];
259 };