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