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