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';
18 import { extractUuidKind } from '~/models/resource';
20 export const BREADCRUMBS = 'breadcrumbs';
22 export interface ResourceBreadcrumb extends Breadcrumb {
26 export const setBreadcrumbs = (breadcrumbs: any, currentItem?: any) => {
28 const addLastItem = { label: currentItem.name, uuid: currentItem.uuid };
29 breadcrumbs.push(addLastItem);
31 return propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
35 const getSidePanelTreeBreadcrumbs = (uuid: string) => (treePicker: TreePicker): ResourceBreadcrumb[] => {
36 const nodes = getSidePanelTreeBranch(uuid)(treePicker);
37 return nodes.map(node =>
38 typeof node.value === 'string'
39 ? { label: node.value, uuid: node.id }
40 : { label: node.value.name, uuid: node.value.uuid });
43 export const setSidePanelBreadcrumbs = (uuid: string) =>
44 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45 const { treePicker } = getState();
46 const breadcrumbs = getSidePanelTreeBreadcrumbs(uuid)(treePicker);
47 const path = getState().router.location!.pathname;
48 const currentUuid = path.split('/')[2];
49 const uuidKind = extractUuidKind(currentUuid);
51 if (uuidKind === ResourceKind.COLLECTION) {
52 const collectionItem = await services.collectionService.get(currentUuid);
53 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
54 } else if (uuidKind === ResourceKind.PROCESS) {
55 const processItem = await services.containerRequestService.get(currentUuid);
56 dispatch(setBreadcrumbs(breadcrumbs, processItem));
58 dispatch(setBreadcrumbs(breadcrumbs));
61 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
62 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
64 export const setTrashBreadcrumbs = (uuid: string) =>
65 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
67 export const setCategoryBreadcrumbs = (uuid: string, category: SidePanelTreeCategory) =>
68 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
69 const ancestors = await services.ancestorsService.ancestors(uuid, '');
70 dispatch(updateResources(ancestors));
71 const initialBreadcrumbs: ResourceBreadcrumb[] = [
72 { label: category, uuid: category }
74 const path = getState().router.location!.pathname;
75 const currentUuid = path.split('/')[2];
76 const uuidKind = extractUuidKind(currentUuid);
77 const breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
78 ancestor.kind === ResourceKind.GROUP
79 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
82 if (uuidKind === ResourceKind.COLLECTION) {
83 const collectionItem = await services.collectionService.get(currentUuid);
84 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
85 } else if (uuidKind === ResourceKind.PROCESS) {
86 const processItem = await services.containerRequestService.get(currentUuid);
87 dispatch(setBreadcrumbs(breadcrumbs, processItem));
89 dispatch(setBreadcrumbs(breadcrumbs));
92 export const setProjectBreadcrumbs = (uuid: string) =>
93 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
94 const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
95 const rootUuid = services.authService.getUuid();
96 if (uuid === rootUuid || ancestors.find(uuid => uuid === rootUuid)) {
97 dispatch(setSidePanelBreadcrumbs(uuid));
99 dispatch(setSharedWithMeBreadcrumbs(uuid));
100 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
104 export const setProcessBreadcrumbs = (processUuid: string) =>
105 (dispatch: Dispatch, getState: () => RootState) => {
106 const { resources } = getState();
107 const process = getProcess(processUuid)(resources);
109 dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
113 export const GROUPS_PANEL_LABEL = 'Groups';
115 export const setGroupsBreadcrumbs = () =>
116 setBreadcrumbs([{ label: GROUPS_PANEL_LABEL }]);
118 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
119 (dispatch: Dispatch, getState: () => RootState) => {
121 const group = getResource<GroupResource>(groupUuid)(getState().resources);
123 const breadcrumbs: ResourceBreadcrumb[] = [
124 { label: GROUPS_PANEL_LABEL, uuid: GROUPS_PANEL_LABEL },
125 { label: group ? group.name : groupUuid, uuid: groupUuid },
128 dispatch(setBreadcrumbs(breadcrumbs));