Update navigation handlers for process and process logs panels
[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 } from '../side-panel-tree/side-panel-tree-actions';
11 import { propertiesActions } from '../properties/properties-actions';
12 import { getProcess } from '~/store/processes/process';
13
14 export const BREADCRUMBS = 'breadcrumbs';
15
16 export interface ResourceBreadcrumb extends Breadcrumb {
17     uuid: string;
18 }
19
20 export const setBreadcrumbs = (breadcrumbs: Breadcrumb[]) =>
21     propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
22
23 const getSidePanelTreeBreadcrumbs = (uuid: string) => (treePicker: TreePicker): ResourceBreadcrumb[] => {
24     const nodes = getSidePanelTreeBranch(uuid)(treePicker);
25     return nodes.map(node =>
26         typeof node.value === 'string'
27             ? { label: node.value, uuid: node.nodeId }
28             : { label: node.value.name, uuid: node.value.uuid });
29 };
30
31 export const setSidePanelBreadcrumbs = (uuid: string) =>
32     (dispatch: Dispatch, getState: () => RootState) => {
33         const { treePicker } = getState();
34         const breadcrumbs = getSidePanelTreeBreadcrumbs(uuid)(treePicker);
35         dispatch(setBreadcrumbs(breadcrumbs));
36     };
37
38 export const setProjectBreadcrumbs = setSidePanelBreadcrumbs;
39
40 export const setCollectionBreadcrumbs = (collectionUuid: string) =>
41     (dispatch: Dispatch, getState: () => RootState) => {
42         const { resources } = getState();
43         const collection = getResource(collectionUuid)(resources);
44         if (collection) {
45             dispatch<any>(setProjectBreadcrumbs(collection.ownerUuid));
46         }
47     };
48 export const setProcessBreadcrumbs = (processUuid: string) =>
49     (dispatch: Dispatch, getState: () => RootState) => {
50         const { resources } = getState();
51         const process = getProcess(processUuid)(resources);
52         if (process) {
53             dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
54         }
55     };