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 { getUserUuid } from "~/common/getuser";
8 import { Breadcrumb } from '~/components/breadcrumbs/breadcrumbs';
9 import { getResource } from '~/store/resources/resources';
10 import { TreePicker } from '../tree-picker/tree-picker';
11 import { getSidePanelTreeBranch, getSidePanelTreeNodeAncestorsIds } from '../side-panel-tree/side-panel-tree-actions';
12 import { propertiesActions } from '../properties/properties-actions';
13 import { getProcess } from '~/store/processes/process';
14 import { ServiceRepository } from '~/services/services';
15 import { SidePanelTreeCategory, activateSidePanelTreeItem } from '~/store/side-panel-tree/side-panel-tree-actions';
16 import { updateResources } from '../resources/resources-actions';
17 import { ResourceKind } from '~/models/resource';
18 import { GroupResource } from '~/models/group';
19 import { extractUuidKind } from '~/models/resource';
21 export const BREADCRUMBS = 'breadcrumbs';
23 export interface ResourceBreadcrumb extends Breadcrumb {
27 export const setBreadcrumbs = (breadcrumbs: any, currentItem?: any) => {
29 const addLastItem = { label: currentItem.name, uuid: currentItem.uuid };
30 breadcrumbs.push(addLastItem);
32 return propertiesActions.SET_PROPERTY({ key: BREADCRUMBS, value: breadcrumbs });
36 const getSidePanelTreeBreadcrumbs = (uuid: string) => (treePicker: TreePicker): ResourceBreadcrumb[] => {
37 const nodes = getSidePanelTreeBranch(uuid)(treePicker);
38 return nodes.map(node =>
39 typeof node.value === 'string'
40 ? { label: node.value, uuid: node.id }
41 : { label: node.value.name, uuid: node.value.uuid });
44 export const setSidePanelBreadcrumbs = (uuid: string) =>
45 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46 const { treePicker } = getState();
47 const breadcrumbs = getSidePanelTreeBreadcrumbs(uuid)(treePicker);
48 const path = getState().router.location!.pathname;
49 const currentUuid = path.split('/')[2];
50 const uuidKind = extractUuidKind(currentUuid);
52 if (uuidKind === ResourceKind.COLLECTION) {
53 const collectionItem = await services.collectionService.get(currentUuid);
54 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
55 } else if (uuidKind === ResourceKind.PROCESS) {
56 const processItem = await services.containerRequestService.get(currentUuid);
57 dispatch(setBreadcrumbs(breadcrumbs, processItem));
59 dispatch(setBreadcrumbs(breadcrumbs));
62 export const setSharedWithMeBreadcrumbs = (uuid: string) =>
63 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.SHARED_WITH_ME);
65 export const setTrashBreadcrumbs = (uuid: string) =>
66 setCategoryBreadcrumbs(uuid, SidePanelTreeCategory.TRASH);
68 export const setCategoryBreadcrumbs = (uuid: string, category: SidePanelTreeCategory) =>
69 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70 const ancestors = await services.ancestorsService.ancestors(uuid, '');
71 dispatch(updateResources(ancestors));
72 const initialBreadcrumbs: ResourceBreadcrumb[] = [
73 { label: category, uuid: category }
75 const path = getState().router.location!.pathname;
76 const currentUuid = path.split('/')[2];
77 const uuidKind = extractUuidKind(currentUuid);
78 const breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
79 ancestor.kind === ResourceKind.GROUP
80 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
83 if (uuidKind === ResourceKind.COLLECTION) {
84 const collectionItem = await services.collectionService.get(currentUuid);
85 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
86 } else if (uuidKind === ResourceKind.PROCESS) {
87 const processItem = await services.containerRequestService.get(currentUuid);
88 dispatch(setBreadcrumbs(breadcrumbs, processItem));
90 dispatch(setBreadcrumbs(breadcrumbs));
93 export const setProjectBreadcrumbs = (uuid: string) =>
94 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
95 const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
96 const rootUuid = getUserUuid(getState());
97 if (uuid === rootUuid || ancestors.find(uuid => uuid === rootUuid)) {
98 dispatch(setSidePanelBreadcrumbs(uuid));
100 dispatch(setSharedWithMeBreadcrumbs(uuid));
101 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
105 export const setProcessBreadcrumbs = (processUuid: string) =>
106 (dispatch: Dispatch, getState: () => RootState) => {
107 const { resources } = getState();
108 const process = getProcess(processUuid)(resources);
110 dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
114 export const GROUPS_PANEL_LABEL = 'Groups';
116 export const setGroupsBreadcrumbs = () =>
117 setBreadcrumbs([{ label: GROUPS_PANEL_LABEL }]);
119 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
120 (dispatch: Dispatch, getState: () => RootState) => {
122 const group = getResource<GroupResource>(groupUuid)(getState().resources);
124 const breadcrumbs: ResourceBreadcrumb[] = [
125 { label: GROUPS_PANEL_LABEL, uuid: GROUPS_PANEL_LABEL },
126 { label: group ? group.name : groupUuid, uuid: groupUuid },
129 dispatch(setBreadcrumbs(breadcrumbs));