22141: Refactoring to reduce circular import dependencies
[arvados.git] / services / workbench2 / src / store / breadcrumbs / breadcrumbs-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 { RootState } from 'store/store';
7 import { getUserUuid } from "common/getuser";
8 import { getResource } from 'store/resources/resources';
9 import { propertiesActions } from '../properties/properties-actions';
10 import { getProcess } from 'store/processes/process';
11 import { ServiceRepository } from 'services/services';
12 import { SidePanelTreeCategory, activateSidePanelTreeItem } from 'store/side-panel-tree/side-panel-tree-actions';
13 import { updateResources } from '../resources/resources-actions';
14 import { ResourceKind } from 'models/resource';
15 import { GroupResource } from 'models/group';
16 import { extractUuidKind } from 'models/resource';
17 import { UserResource } from 'models/user';
18 import { FilterBuilder } from 'services/api/filter-builder';
19 import { ProcessResource } from 'models/process';
20 import { OrderBuilder } from 'services/api/order-builder';
21 import { Breadcrumb } from 'components/breadcrumbs/breadcrumbs';
22 import { ContainerRequestResource, containerRequestFieldsNoMounts } from 'models/container-request';
23 import { AdminMenuIcon, CollectionIcon, IconType, ProcessIcon, ProjectIcon, ResourceIcon, TerminalIcon, WorkflowIcon } from 'components/icon/icon';
24 import { CollectionResource } from 'models/collection';
25 import { getSidePanelIcon } from 'store/side-panel-tree/side-panel-tree-actions';
26 import { WorkflowResource } from 'models/workflow';
27 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
28
29 export const BREADCRUMBS = 'breadcrumbs';
30
31 export const setBreadcrumbs = (breadcrumbs: any, currentItem?: CollectionResource | ContainerRequestResource | GroupResource | WorkflowResource) => {
32     if (currentItem) {
33         const currentCrumb = resourceToBreadcrumb(currentItem)
34         if (currentCrumb.label.length) breadcrumbs.push(currentCrumb);
35     }
36     return propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
37 };
38
39 const resourceToBreadcrumbIcon = (resource: CollectionResource | ContainerRequestResource | GroupResource | WorkflowResource): IconType | undefined => {
40     switch (resource.kind) {
41         case ResourceKind.PROJECT:
42             return ProjectIcon;
43         case ResourceKind.PROCESS:
44             return ProcessIcon;
45         case ResourceKind.COLLECTION:
46             return CollectionIcon;
47         case ResourceKind.WORKFLOW:
48             return WorkflowIcon;
49         default:
50             return undefined;
51     }
52 }
53
54 const resourceToBreadcrumb = (resource: (CollectionResource | ContainerRequestResource | GroupResource | WorkflowResource) & {fullName?: string}  ): Breadcrumb => ({
55     label: resource.name || resource.fullName || '',
56     uuid: resource.uuid,
57     icon: resourceToBreadcrumbIcon(resource),
58 })
59
60 export const setSidePanelBreadcrumbs = (uuid: string) =>
61     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62         try {
63             dispatch(progressIndicatorActions.START_WORKING(uuid + "-breadcrumbs"));
64             const ancestors = await services.ancestorsService.ancestors(uuid, '');
65             dispatch(updateResources(ancestors));
66
67             let breadcrumbs: Breadcrumb[] = [];
68             const { collectionPanel: { item } } = getState();
69
70             const path = getState().router.location!.pathname;
71             const currentUuid = path.split('/')[2];
72             const uuidKind = extractUuidKind(currentUuid);
73             const rootUuid = getUserUuid(getState());
74
75             if (ancestors.find(ancestor => ancestor.uuid === rootUuid)) {
76                 // Handle home project uuid root
77                 breadcrumbs.push({
78                     label: SidePanelTreeCategory.PROJECTS,
79                     uuid: SidePanelTreeCategory.PROJECTS,
80                     icon: getSidePanelIcon(SidePanelTreeCategory.PROJECTS)
81                 });
82             } else if (uuidKind === ResourceKind.USER) {
83                 // Handle another user root project
84                 const user = getResource<UserResource>(uuid)(getState().resources);
85                 breadcrumbs.push({
86                     label: (user as any)?.fullName || user?.username || uuid,
87                     uuid: user?.uuid || uuid,
88                     icon: getSidePanelIcon(SidePanelTreeCategory.PROJECTS)
89                 });
90             } else if (Object.values(SidePanelTreeCategory).includes(uuid as SidePanelTreeCategory)) {
91                 // Handle SidePanelTreeCategory root
92                 breadcrumbs.push({
93                     label: uuid,
94                     uuid: uuid,
95                     icon: getSidePanelIcon(uuid)
96                 });
97             }
98
99             breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
100                 ancestor.kind === ResourceKind.GROUP
101                     ? [...breadcrumbs, resourceToBreadcrumb(ancestor)]
102                     : breadcrumbs,
103                 breadcrumbs);
104
105             if (uuidKind === ResourceKind.COLLECTION) {
106                 const collectionItem = item ? item : await services.collectionService.get(currentUuid);
107                 const parentProcessItem = await getCollectionParent(collectionItem)(services);
108                 if (parentProcessItem) {
109                     const mainProcessItem = await getProcessParent(parentProcessItem)(services);
110                     mainProcessItem && breadcrumbs.push(resourceToBreadcrumb(mainProcessItem));
111                     breadcrumbs.push(resourceToBreadcrumb(parentProcessItem));
112                 }
113                 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
114             } else if (uuidKind === ResourceKind.PROCESS) {
115                 const processItem = await services.containerRequestService.get(currentUuid);
116                 const parentProcessItem = await getProcessParent(processItem)(services);
117                 if (parentProcessItem) {
118                     breadcrumbs.push(resourceToBreadcrumb(parentProcessItem));
119                 }
120                 dispatch(setBreadcrumbs(breadcrumbs, processItem));
121             } else if (uuidKind === ResourceKind.WORKFLOW) {
122                 const workflowItem = await services.workflowService.get(currentUuid);
123                 dispatch(setBreadcrumbs(breadcrumbs, workflowItem));
124             }
125             dispatch(setBreadcrumbs(breadcrumbs));
126         } catch (e) {
127             console.log("Error setting breadcrumbs "+e);
128         } finally {
129             dispatch(progressIndicatorActions.STOP_WORKING(uuid + "-breadcrumbs"));
130         }
131     };
132
133 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
134     setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
135
136 export const setTrashBreadcrumbs = (uuid: string) =>
137     setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
138
139 export const setCategoryBreadcrumbs = (uuid: string, category: string) =>
140     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
141         try {
142             dispatch(progressIndicatorActions.START_WORKING(uuid + "-breadcrumbs"));
143             const ancestors = await services.ancestorsService.ancestors(uuid, '');
144             dispatch(updateResources(ancestors));
145             const initialBreadcrumbs: Breadcrumb[] = [
146                 {
147                     label: category,
148                     uuid: category,
149                     icon: getSidePanelIcon(category)
150                 }
151             ];
152             const { collectionPanel: { item } } = getState();
153             const path = getState().router.location!.pathname;
154             const currentUuid = path.split('/')[2];
155             const uuidKind = extractUuidKind(currentUuid);
156             let breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
157                 ancestor.kind === ResourceKind.GROUP
158                     ? [...breadcrumbs, resourceToBreadcrumb(ancestor)]
159                     : breadcrumbs,
160                 initialBreadcrumbs);
161             if (uuidKind === ResourceKind.COLLECTION) {
162                 const collectionItem = item ? item : await services.collectionService.get(currentUuid);
163                 const parentProcessItem = await getCollectionParent(collectionItem)(services);
164                 if (parentProcessItem) {
165                     const mainProcessItem = await getProcessParent(parentProcessItem)(services);
166                     mainProcessItem && breadcrumbs.push(resourceToBreadcrumb(mainProcessItem));
167                     breadcrumbs.push(resourceToBreadcrumb(parentProcessItem));
168                 }
169                 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
170             } else if (uuidKind === ResourceKind.PROCESS) {
171                 const processItem = await services.containerRequestService.get(currentUuid);
172                 const parentProcessItem = await getProcessParent(processItem)(services);
173                 if (parentProcessItem) {
174                     breadcrumbs.push(resourceToBreadcrumb(parentProcessItem));
175                 }
176                 dispatch(setBreadcrumbs(breadcrumbs, processItem));
177             } else if (uuidKind === ResourceKind.WORKFLOW) {
178                 const workflowItem = await services.workflowService.get(currentUuid);
179                 dispatch(setBreadcrumbs(breadcrumbs, workflowItem));
180             }
181             dispatch(setBreadcrumbs(breadcrumbs));
182         } finally {
183             dispatch(progressIndicatorActions.STOP_WORKING(uuid + "-breadcrumbs"));
184         }
185     };
186
187 const getProcessParent = (childProcess: ContainerRequestResource) =>
188     async (services: ServiceRepository): Promise<ContainerRequestResource | undefined> => {
189         if (childProcess.requestingContainerUuid) {
190             const parentProcesses = await services.containerRequestService.list({
191                 order: new OrderBuilder<ProcessResource>().addAsc('createdAt').getOrder(),
192                 filters: new FilterBuilder().addEqual('container_uuid', childProcess.requestingContainerUuid).getFilters(),
193                 select: containerRequestFieldsNoMounts,
194             });
195             if (parentProcesses.items.length > 0) {
196                 return parentProcesses.items[0];
197             } else {
198                 return undefined;
199             }
200         } else {
201             return undefined;
202         }
203     }
204
205 const getCollectionParent = (collection: CollectionResource) =>
206     async (services: ServiceRepository): Promise<ContainerRequestResource | undefined> => {
207         const parentOutputPromise = services.containerRequestService.list({
208             order: new OrderBuilder<ProcessResource>().addAsc('createdAt').getOrder(),
209             filters: new FilterBuilder().addEqual('output_uuid', collection.uuid).getFilters(),
210             select: containerRequestFieldsNoMounts,
211         });
212         const parentLogPromise = services.containerRequestService.list({
213             order: new OrderBuilder<ProcessResource>().addAsc('createdAt').getOrder(),
214             filters: new FilterBuilder().addEqual('log_uuid', collection.uuid).getFilters(),
215             select: containerRequestFieldsNoMounts,
216         });
217         const [parentOutput, parentLog] = await Promise.all([parentOutputPromise, parentLogPromise]);
218         return parentOutput.items.length > 0 ?
219             parentOutput.items[0] :
220             parentLog.items.length > 0 ?
221                 parentLog.items[0] :
222                 undefined;
223     }
224
225
226 export const setProjectBreadcrumbs = (uuid: string) =>
227     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
228         const ancestors = await services.ancestorsService.ancestors(uuid, '');
229         const rootUuid = getUserUuid(getState());
230         if (uuid === rootUuid || ancestors.find(ancestor => ancestor.uuid === rootUuid)) {
231             dispatch(setSidePanelBreadcrumbs(uuid));
232         } else {
233             dispatch(setSharedWithMeBreadcrumbs(uuid));
234             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
235         }
236     };
237
238 export const setProcessBreadcrumbs = (processUuid: string) =>
239     (dispatch: Dispatch, getState: () => RootState) => {
240         const { resources } = getState();
241         const process = getProcess(processUuid)(resources);
242         if (process) {
243             dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
244         }
245     };
246
247 export const setGroupsBreadcrumbs = () =>
248     setBreadcrumbs([{
249         label: SidePanelTreeCategory.GROUPS,
250         uuid: SidePanelTreeCategory.GROUPS,
251         icon: getSidePanelIcon(SidePanelTreeCategory.GROUPS)
252     }]);
253
254 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
255     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
256
257         const group = getResource<GroupResource>(groupUuid)(getState().resources);
258
259         const breadcrumbs: Breadcrumb[] = [
260             {
261                 label: SidePanelTreeCategory.GROUPS,
262                 uuid: SidePanelTreeCategory.GROUPS,
263                 icon: getSidePanelIcon(SidePanelTreeCategory.GROUPS)
264             },
265             { label: group ? group.name : (await services.groupsService.get(groupUuid)).name, uuid: groupUuid },
266         ];
267
268         dispatch(setBreadcrumbs(breadcrumbs));
269
270     };
271
272 export const USERS_PANEL_LABEL = 'Users';
273
274 export const setUsersBreadcrumbs = () =>
275     setBreadcrumbs([{ label: USERS_PANEL_LABEL, uuid: USERS_PANEL_LABEL }]);
276
277 export const setUserProfileBreadcrumbs = (userUuid: string) =>
278     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
279         try {
280             const user = getResource<UserResource>(userUuid)(getState().resources)
281                 || (await services.userService.get(userUuid, false));
282             const userProfileBreadcrumbs: Breadcrumb[] = [
283                 { label: USERS_PANEL_LABEL, uuid: USERS_PANEL_LABEL },
284                 { label: user ? `${user.firstName} ${user.lastName}` : userUuid, uuid: userUuid },
285             ];
286             dispatch(setBreadcrumbs(userProfileBreadcrumbs));
287         } catch (e) {
288             const breadcrumbs: Breadcrumb[] = [
289                 { label: USERS_PANEL_LABEL, uuid: USERS_PANEL_LABEL },
290                 { label: userUuid, uuid: userUuid },
291             ];
292             dispatch(setBreadcrumbs(breadcrumbs));
293         }
294     };
295
296 export const MY_ACCOUNT_PANEL_LABEL = 'My Account';
297
298 export const setMyAccountBreadcrumbs = () =>
299     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
300         dispatch(setBreadcrumbs([
301             { label: MY_ACCOUNT_PANEL_LABEL, uuid: MY_ACCOUNT_PANEL_LABEL },
302         ]));
303     };
304
305 export const INSTANCE_TYPES_PANEL_LABEL = 'Instance Types';
306
307 export const setInstanceTypesBreadcrumbs = () =>
308     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
309         dispatch(setBreadcrumbs([
310             { label: INSTANCE_TYPES_PANEL_LABEL, uuid: INSTANCE_TYPES_PANEL_LABEL, icon: ResourceIcon },
311         ]));
312     };
313
314 export const setVirtualMachinesBreadcrumbs = () =>
315     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
316         dispatch(setBreadcrumbs([
317             { label: SidePanelTreeCategory.SHELL_ACCESS, uuid: SidePanelTreeCategory.SHELL_ACCESS, icon: TerminalIcon },
318         ]));
319     };
320
321 export const VIRTUAL_MACHINES_ADMIN_PANEL_LABEL = 'Shell Access Admin';
322
323 export const setVirtualMachinesAdminBreadcrumbs = () =>
324     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
325         dispatch(setBreadcrumbs([
326             { label: VIRTUAL_MACHINES_ADMIN_PANEL_LABEL, uuid: VIRTUAL_MACHINES_ADMIN_PANEL_LABEL, icon: AdminMenuIcon },
327         ]));
328     };
329
330 export const REPOSITORIES_PANEL_LABEL = 'Repositories';
331
332 export const setRepositoriesBreadcrumbs = () =>
333     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
334         dispatch(setBreadcrumbs([
335             { label: REPOSITORIES_PANEL_LABEL, uuid: REPOSITORIES_PANEL_LABEL, icon: AdminMenuIcon },
336         ]));
337     };