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