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