15407: Fixes mount handling. Re-run process now shows dialog without erroring.
[arvados.git] / 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 { Breadcrumb } from '~/components/breadcrumbs/breadcrumbs';
8 import { getResource } from '~/store/resources/resources';
9 import { TreePicker } from '../tree-picker/tree-picker';
10 import { getSidePanelTreeBranch, getSidePanelTreeNodeAncestorsIds } from '../side-panel-tree/side-panel-tree-actions';
11 import { propertiesActions } from '../properties/properties-actions';
12 import { getProcess } from '~/store/processes/process';
13 import { ServiceRepository } from '~/services/services';
14 import { SidePanelTreeCategory, activateSidePanelTreeItem } from '~/store/side-panel-tree/side-panel-tree-actions';
15 import { updateResources } from '../resources/resources-actions';
16 import { ResourceKind } from '~/models/resource';
17 import { GroupResource } from '~/models/group';
18 import { extractUuidKind } from '~/models/resource';
19
20 export const BREADCRUMBS = 'breadcrumbs';
21
22 export interface ResourceBreadcrumb extends Breadcrumb {
23     uuid: string;
24 }
25
26 export const setBreadcrumbs = (breadcrumbs: any, currentItem?: any) => {
27     if (currentItem) {
28         const addLastItem = { label: currentItem.name, uuid: currentItem.uuid };
29         breadcrumbs.push(addLastItem);
30     }
31     return propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
32 };
33
34
35 const getSidePanelTreeBreadcrumbs = (uuid: string) => (treePicker: TreePicker): ResourceBreadcrumb[] => {
36     const nodes = getSidePanelTreeBranch(uuid)(treePicker);
37     return nodes.map(node =>
38         typeof node.value === 'string'
39             ? { label: node.value, uuid: node.id }
40             : { label: node.value.name, uuid: node.value.uuid });
41 };
42
43 export const setSidePanelBreadcrumbs = (uuid: string) =>
44     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45         const { treePicker } = getState();
46         const breadcrumbs = getSidePanelTreeBreadcrumbs(uuid)(treePicker);
47         const path = getState().router.location!.pathname;
48         const currentUuid = path.split('/')[2];
49         const uuidKind = extractUuidKind(currentUuid);
50         
51         if (uuidKind === ResourceKind.COLLECTION) {
52             const collectionItem = await services.collectionService.get(currentUuid);
53             dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
54         } else if (uuidKind === ResourceKind.PROCESS) {
55             const processItem = await services.containerRequestService.get(currentUuid);
56             dispatch(setBreadcrumbs(breadcrumbs, processItem));
57         }
58         dispatch(setBreadcrumbs(breadcrumbs));
59     };
60
61 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
62     setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
63
64 export const setTrashBreadcrumbs = (uuid: string) =>
65     setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
66
67 export const setCategoryBreadcrumbs = (uuid: string, category: SidePanelTreeCategory) =>
68     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
69         const ancestors = await services.ancestorsService.ancestors(uuid, '');
70         dispatch(updateResources(ancestors));
71         const initialBreadcrumbs: ResourceBreadcrumb[] = [
72             { label: category, uuid: category }
73         ];
74         const path = getState().router.location!.pathname;
75         const currentUuid = path.split('/')[2];
76         const uuidKind = extractUuidKind(currentUuid);
77         const breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
78             ancestor.kind === ResourceKind.GROUP
79                 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
80                 : breadcrumbs,
81             initialBreadcrumbs);
82         if (uuidKind === ResourceKind.COLLECTION) {
83             const collectionItem = await services.collectionService.get(currentUuid);
84             dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
85         } else if (uuidKind === ResourceKind.PROCESS) {
86             const processItem = await services.containerRequestService.get(currentUuid);
87             dispatch(setBreadcrumbs(breadcrumbs, processItem));
88         }
89         dispatch(setBreadcrumbs(breadcrumbs));
90     };
91
92 export const setProjectBreadcrumbs = (uuid: string) =>
93     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
94         const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
95         const rootUuid = services.authService.getUuid();
96         if (uuid === rootUuid || ancestors.find(uuid => uuid === rootUuid)) {
97             dispatch(setSidePanelBreadcrumbs(uuid));
98         } else {
99             dispatch(setSharedWithMeBreadcrumbs(uuid));
100             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
101         }
102     };
103
104 export const setProcessBreadcrumbs = (processUuid: string) =>
105     (dispatch: Dispatch, getState: () => RootState) => {
106         const { resources } = getState();
107         const process = getProcess(processUuid)(resources);
108         if (process) {
109             dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
110         }
111     };
112
113 export const GROUPS_PANEL_LABEL = 'Groups';
114
115 export const setGroupsBreadcrumbs = () =>
116     setBreadcrumbs([{ label: GROUPS_PANEL_LABEL }]);
117
118 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
119     (dispatch: Dispatch, getState: () => RootState) => {
120
121         const group = getResource<GroupResource>(groupUuid)(getState().resources);
122
123         const breadcrumbs: ResourceBreadcrumb[] = [
124             { label: GROUPS_PANEL_LABEL, uuid: GROUPS_PANEL_LABEL },
125             { label: group ? group.name : groupUuid, uuid: groupUuid },
126         ];
127
128         dispatch(setBreadcrumbs(breadcrumbs));
129
130     };