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, collectionPanel: { item } } = 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 = item ? item : 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: string) =>
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 { collectionPanel: { item } } = getState();
76 const path = getState().router.location!.pathname;
77 const currentUuid = path.split('/')[2];
78 const uuidKind = extractUuidKind(currentUuid);
79 const breadcrumbs = ancestors.reduce((breadcrumbs, ancestor) =>
80 ancestor.kind === ResourceKind.GROUP
81 ? [...breadcrumbs, { label: ancestor.name, uuid: ancestor.uuid }]
84 if (uuidKind === ResourceKind.COLLECTION) {
85 const collectionItem = item ? item : await services.collectionService.get(currentUuid);
86 dispatch(setBreadcrumbs(breadcrumbs, collectionItem));
87 } else if (uuidKind === ResourceKind.PROCESS) {
88 const processItem = await services.containerRequestService.get(currentUuid);
89 dispatch(setBreadcrumbs(breadcrumbs, processItem));
91 dispatch(setBreadcrumbs(breadcrumbs));
94 export const setProjectBreadcrumbs = (uuid: string) =>
95 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
96 const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
97 const rootUuid = getUserUuid(getState());
98 if (uuid === rootUuid || ancestors.find(uuid => uuid === rootUuid)) {
99 dispatch(setSidePanelBreadcrumbs(uuid));
101 dispatch(setSharedWithMeBreadcrumbs(uuid));
102 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
106 export const setProcessBreadcrumbs = (processUuid: string) =>
107 (dispatch: Dispatch, getState: () => RootState) => {
108 const { resources } = getState();
109 const process = getProcess(processUuid)(resources);
111 dispatch<any>(setProjectBreadcrumbs(process.containerRequest.ownerUuid));
115 export const GROUPS_PANEL_LABEL = 'Groups';
117 export const setGroupsBreadcrumbs = () =>
118 setBreadcrumbs([{ label: GROUPS_PANEL_LABEL }]);
120 export const setGroupDetailsBreadcrumbs = (groupUuid: string) =>
121 (dispatch: Dispatch, getState: () => RootState) => {
123 const group = getResource<GroupResource>(groupUuid)(getState().resources);
125 const breadcrumbs: ResourceBreadcrumb[] = [
126 { label: GROUPS_PANEL_LABEL, uuid: GROUPS_PANEL_LABEL },
127 { label: group ? group.name : groupUuid, uuid: groupUuid },
130 dispatch(setBreadcrumbs(breadcrumbs));