Move common operations to tree model
[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
18 export const BREADCRUMBS = 'breadcrumbs';
19
20 export interface ResourceBreadcrumb extends Breadcrumb {
21     uuid: string;
22 }
23
24 export const setBreadcrumbs = (breadcrumbs: Breadcrumb[]) =>
25     propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
26
27 const getSidePanelTreeBreadcrumbs = (uuid: string) => (treePicker: TreePicker): ResourceBreadcrumb[] => {
28     const nodes = getSidePanelTreeBranch(uuid)(treePicker);
29     return nodes.map(node =>
30         typeof node.value === 'string'
31             ? { label: node.value, uuid: node.id }
32             : { label: node.value.name, uuid: node.value.uuid });
33 };
34
35 export const setSidePanelBreadcrumbs = (uuid: string) =>
36     (dispatch: Dispatch, getState: () => RootState) => {
37         const { treePicker } = getState();
38         const breadcrumbs = getSidePanelTreeBreadcrumbs(uuid)(treePicker);
39         dispatch(setBreadcrumbs(breadcrumbs));
40     };
41
42 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
43     setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
44
45 export const setTrashBreadcrumbs = (uuid: string) =>
46     setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
47
48 export const setCategoryBreadcrumbs = (uuid: string, category: SidePanelTreeCategory) =>
49     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50         const ancestors = await services.ancestorsService.ancestors(uuid, '');
51         dispatch(updateResources(ancestors));
52         const initialBreadcrumbs: ResourceBreadcrumb[] = [
53             { label: category, uuid: category }
54         ];
55         const breadrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
56             ancestor.kind === ResourceKind.GROUP
57                 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
58                 : breadcrumbs,
59             initialBreadcrumbs);
60
61         dispatch(setBreadcrumbs(breadrumbs));
62     };
63
64 export const setProjectBreadcrumbs = (uuid: string) =>
65     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
66         const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
67         const rootUuid = services.authService.getUuid();
68         if (uuid === rootUuid || ancestors.find(uuid => uuid === rootUuid)) {
69             dispatch(setSidePanelBreadcrumbs(uuid));
70         } else {
71             dispatch(setSharedWithMeBreadcrumbs(uuid));
72             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
73         }
74     };
75
76 export const setCollectionBreadcrumbs = (collectionUuid: string) =>
77     (dispatch: Dispatch, getState: () => RootState) => {
78         const { resources } = getState();
79         const collection = getResource(collectionUuid)(resources);
80         if (collection) {
81             dispatch<any>(setProjectBreadcrumbs(collection.ownerUuid));
82         }
83     };
84 export const setProcessBreadcrumbs = (processUuid: string) =>
85     (dispatch: Dispatch, getState: () => RootState) => {
86         const { resources } = getState();
87         const process = getProcess(processUuid)(resources);
88         if (process) {
89             dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
90         }
91     };