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';
17 import { GroupResource } from '~/models/group';
19 export const BREADCRUMBS = 'breadcrumbs';
21 export interface ResourceBreadcrumb extends Breadcrumb {
25 export const setBreadcrumbs = (breadcrumbs: Breadcrumb[]) =>
26 propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
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 });
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));
43 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
44 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
46 export const setTrashBreadcrumbs = (uuid: string) =>
47 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
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 }
56 const breadrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
57 ancestor.kind === ResourceKind.GROUP
58 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
62 dispatch(setBreadcrumbs(breadrumbs));
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));
72 dispatch(setSharedWithMeBreadcrumbs(uuid));
73 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
77 export const setCollectionBreadcrumbs = (collectionUuid: string) =>
78 (dispatch: Dispatch, getState: () => RootState) => {
79 const { resources } = getState();
80 const collection = getResource(collectionUuid)(resources);
82 dispatch<any>(setProjectBreadcrumbs(collection.ownerUuid));
85 export const setProcessBreadcrumbs = (processUuid: string) =>
86 (dispatch: Dispatch, getState: () => RootState) => {
87 const { resources } = getState();
88 const process = getProcess(processUuid)(resources);
90 dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
94 export const GROUPS_PANEL_LABEL = 'Groups';
96 export const setGroupsBreadcrumbs = () =>
97 setBreadcrumbs([{ label: GROUPS_PANEL_LABEL }]);
99 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
100 (dispatch: Dispatch, getState: () => RootState) => {
102 const group = getResource<GroupResource>(groupUuid)(getState().resources);
104 const breadcrumbs: ResourceBreadcrumb[] = [
105 { label: GROUPS_PANEL_LABEL, uuid: GROUPS_PANEL_LABEL },
106 { label: group ? group.name : groupUuid, uuid: groupUuid },
109 dispatch(setBreadcrumbs(breadcrumbs));