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