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