1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
18 export const BREADCRUMBS = 'breadcrumbs';
20 export interface ResourceBreadcrumb extends Breadcrumb {
24 export const setBreadcrumbs = (breadcrumbs: Breadcrumb[]) =>
25 propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
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 });
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));
42 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
43 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
45 export const setTrashBreadcrumbs = (uuid: string) =>
46 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
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 }
55 const breadrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
56 ancestor.kind === ResourceKind.GROUP
57 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
61 dispatch(setBreadcrumbs(breadrumbs));
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));
71 dispatch(setSharedWithMeBreadcrumbs(uuid));
72 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
76 export const setCollectionBreadcrumbs = (collectionUuid: string) =>
77 (dispatch: Dispatch, getState: () => RootState) => {
78 const { resources } = getState();
79 const collection = getResource(collectionUuid)(resources);
81 dispatch<any>(setProjectBreadcrumbs(collection.ownerUuid));
84 export const setProcessBreadcrumbs = (processUuid: string) =>
85 (dispatch: Dispatch, getState: () => RootState) => {
86 const { resources } = getState();
87 const process = getProcess(processUuid)(resources);
89 dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));