1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { getProjectList, projectActions } from "../project/project-action";
7 import { push } from "react-router-redux";
8 import { TreeItemStatus } from "~/components/tree/tree";
9 import { findTreeItem } from "../project/project-reducer";
10 import { RootState } from "../store";
11 import { ResourceKind, Resource } from '~/models/resource';
12 import { projectPanelActions } from "../project-panel/project-panel-action";
13 import { getCollectionUrl } from "~/models/collection";
14 import { getProjectUrl, ProjectResource } from "~/models/project";
15 import { ProjectService } from "~/services/project-service/project-service";
16 import { ServiceRepository } from "~/services/services";
17 import { sidePanelActions } from "../side-panel/side-panel-action";
18 import { SidePanelId } from "../side-panel/side-panel-reducer";
19 import { getUuidObjectType, ObjectTypes } from "~/models/object-types";
20 import { getResource } from '~/store/resources/resources';
21 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
22 import { loadCollection } from '~/store/collection-panel/collection-panel-action';
23 import { GroupContentsResource } from "~/services/groups-service/groups-service";
24 import { snackbarActions } from '../snackbar/snackbar-actions';
25 import { resourceLabel } from "~/common/labels";
27 export const getResourceUrl = (resourceKind: ResourceKind, resourceUuid: string): string => {
28 switch (resourceKind) {
29 case ResourceKind.PROJECT: return getProjectUrl(resourceUuid);
30 case ResourceKind.COLLECTION: return getCollectionUrl(resourceUuid);
36 export enum ItemMode {
42 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
43 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44 const { projects, router } = getState();
45 const treeItem = findTreeItem(projects.items, itemId);
48 const resourceUrl = getResourceUrl(treeItem.data.kind, treeItem.data.uuid);
50 if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
51 if (router.location && !router.location.pathname.includes(resourceUrl)) {
52 dispatch(push(resourceUrl));
54 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
57 const promise = treeItem.status === TreeItemStatus.LOADED
59 : dispatch<any>(getProjectList(itemId));
62 .then(() => dispatch<any>(() => {
63 if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
64 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
66 dispatch(projectPanelActions.RESET_PAGINATION());
67 dispatch(projectPanelActions.REQUEST_ITEMS());
70 const uuid = services.authService.getUuid();
71 if (itemId === uuid) {
72 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
73 dispatch(projectPanelActions.RESET_PAGINATION());
74 dispatch(projectPanelActions.REQUEST_ITEMS());
79 export const restoreBranch = (itemId: string) =>
80 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
81 const ancestors = await loadProjectAncestors(itemId, services.projectService);
82 const uuids = ancestors.map(ancestor => ancestor.uuid);
83 await loadBranch(uuids, dispatch);
84 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelId.PROJECTS));
85 uuids.forEach(uuid => {
86 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
90 export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
91 if (getUuidObjectType(uuid) === ObjectTypes.USER) {
94 const currentProject = await projectService.get(uuid);
95 const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
96 return [...ancestors, currentProject];
100 const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
101 const [uuid, ...rest] = uuids;
103 await dispatch<any>(getProjectList(uuid));
104 return loadBranch(rest, dispatch);
108 export const navigateToResource = (uuid: string) =>
109 (dispatch: Dispatch, getState: () => RootState) => {
110 const resource = getResource(uuid)(getState().resources);
112 ? dispatch<any>(getResourceNavigationAction(resource))
113 : dispatch<any>(resourceIsNotLoaded(uuid));
116 const getResourceNavigationAction = (resource: Resource) => {
117 switch (resource.kind) {
118 case ResourceKind.COLLECTION:
119 return navigateToCollection(resource);
120 case ResourceKind.PROJECT:
121 return navigateToProject(resource);
123 return cannotNavigateToResource(resource);
127 export const navigateToProject = ({ uuid }: Resource) =>
128 (dispatch: Dispatch) => {
129 dispatch<any>(setProjectItem(uuid, ItemMode.BOTH));
130 dispatch(loadDetailsPanel(uuid));
133 export const navigateToCollection = ({ uuid }: Resource) =>
134 (dispatch: Dispatch) => {
135 dispatch<any>(loadCollection(uuid));
136 dispatch(push(getCollectionUrl(uuid)));
139 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
140 snackbarActions.OPEN_SNACKBAR({
141 message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`,
146 export const resourceIsNotLoaded = (uuid: string) =>
147 snackbarActions.OPEN_SNACKBAR({
148 message: `Resource identified by ${uuid} is not loaded.`,