18284: Resolve bugs in VM listing and add login administration functions
[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, collectionPanel: { item } } = 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 = item ? item : 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: string) =>
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 { collectionPanel: { item } } = getState();
76         const path = getState().router.location!.pathname;
77         const currentUuid = path.split('/')[2];
78         const uuidKind = extractUuidKind(currentUuid);
79         const breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
80             ancestor.kind === ResourceKind.GROUP
81                 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
82                 : breadcrumbs,
83             initialBreadcrumbs);
84         if (uuidKind === ResourceKind.COLLECTION) {
85             const collectionItem = item ? item : await services.collectionService.get(currentUuid);
86             dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
87         } else if (uuidKind === ResourceKind.PROCESS) {
88             const processItem = await services.containerRequestService.get(currentUuid);
89             dispatch(setBreadcrumbs(breadcrumbs, processItem));
90         }
91         dispatch(setBreadcrumbs(breadcrumbs));
92     };
93
94 export const setProjectBreadcrumbs = (uuid: string) =>
95     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
96         const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
97         const rootUuid = getUserUuid(getState());
98         if (uuid === rootUuid || ancestors.find(uuid => uuid === rootUuid)) {
99             dispatch(setSidePanelBreadcrumbs(uuid));
100         } else {
101             dispatch(setSharedWithMeBreadcrumbs(uuid));
102             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
103         }
104     };
105
106 export const setProcessBreadcrumbs = (processUuid: string) =>
107     (dispatch: Dispatch, getState: () => RootState) => {
108         const { resources } = getState();
109         const process = getProcess(processUuid)(resources);
110         if (process) {
111             dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
112         }
113     };
114
115 export const GROUPS_PANEL_LABEL = 'Groups';
116
117 export const setGroupsBreadcrumbs = () =>
118     setBreadcrumbs([{ label: GROUPS_PANEL_LABEL }]);
119
120 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
121     (dispatch: Dispatch, getState: () => RootState) => {
122
123         const group = getResource<GroupResource>(groupUuid)(getState().resources);
124
125         const breadcrumbs: ResourceBreadcrumb[] = [
126             { label: GROUPS_PANEL_LABEL, uuid: GROUPS_PANEL_LABEL },
127             { label: group ? group.name : groupUuid, uuid: groupUuid },
128         ];
129
130         dispatch(setBreadcrumbs(breadcrumbs));
131
132     };