1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { projectActions, getProjectList } 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 { Resource, ResourceKind } 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 { SidePanelIdentifiers } from "../side-panel/side-panel-reducer";
19 import { getUuidObjectType, ObjectTypes } from "../../models/object-types";
21 export const getResourceUrl = <T extends Resource>(resource: T): string => {
22 switch (resource.kind) {
23 case ResourceKind.PROJECT: return getProjectUrl(resource.uuid);
24 case ResourceKind.COLLECTION: return getCollectionUrl(resource.uuid);
25 default: return resource.href;
29 export enum ItemMode {
35 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
36 (dispatch: Dispatch, getState: () => RootState) => {
37 const { projects, router } = getState();
38 const treeItem = findTreeItem(projects.items, itemId);
42 const resourceUrl = getResourceUrl(treeItem.data);
44 if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
45 if (router.location && !router.location.pathname.includes(resourceUrl)) {
46 dispatch(push(resourceUrl));
48 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
51 const promise = treeItem.status === TreeItemStatus.LOADED
53 : dispatch<any>(getProjectList(itemId));
56 .then(() => dispatch<any>(() => {
57 if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
58 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
60 dispatch(projectPanelActions.RESET_PAGINATION());
61 dispatch(projectPanelActions.REQUEST_ITEMS());
67 export const restoreBranch = (itemId: string) =>
68 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
69 const ancestors = await loadProjectAncestors(itemId, services.projectService);
70 const uuids = ancestors.map(ancestor => ancestor.uuid);
71 await loadBranch(uuids, dispatch);
72 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelIdentifiers.PROJECTS));
73 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
74 uuids.forEach(uuid => {
75 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
79 export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
80 if (getUuidObjectType(uuid) === ObjectTypes.USER) {
83 const currentProject = await projectService.get(uuid);
84 const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
85 return [...ancestors, currentProject];
89 const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
90 const [uuid, ...rest] = uuids;
92 await dispatch<any>(getProjectList(uuid));
93 return loadBranch(rest, dispatch);