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";
20 export const getResourceUrl = <T extends Resource>(resource: T): string => {
21 switch (resource.kind) {
22 case ResourceKind.PROJECT: return getProjectUrl(resource.uuid);
23 case ResourceKind.COLLECTION: return getCollectionUrl(resource.uuid);
24 default: return resource.href;
28 export enum ItemMode {
34 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
35 (dispatch: Dispatch, getState: () => RootState) => {
36 const { projects, router } = getState();
37 const treeItem = findTreeItem(projects.items, itemId);
41 const resourceUrl = getResourceUrl(treeItem.data);
43 if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
44 if (router.location && !router.location.pathname.includes(resourceUrl)) {
45 dispatch(push(resourceUrl));
47 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
50 const promise = treeItem.status === TreeItemStatus.LOADED
52 : dispatch<any>(getProjectList(itemId));
55 .then(() => dispatch<any>(() => {
56 if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
57 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
59 dispatch(projectPanelActions.RESET_PAGINATION());
60 dispatch(projectPanelActions.REQUEST_ITEMS());
66 export const restoreBranch = (itemId: string) =>
67 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
68 const ancestors = await loadProjectAncestors(itemId, services.projectService);
69 const uuids = ancestors.map(ancestor => ancestor.uuid);
70 await loadBranch(uuids, dispatch);
71 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelIdentifiers.PROJECTS));
72 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
73 uuids.forEach(uuid => {
74 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
78 const USER_UUID_REGEX = /.*tpzed.*/;
80 export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
81 if (USER_UUID_REGEX.test(uuid)) {
84 const currentProject = await projectService.get(uuid);
85 const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
86 return [...ancestors, currentProject];
90 const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
91 const [uuid, ...rest] = uuids;
93 await dispatch<any>(getProjectList(uuid));
94 return loadBranch(rest, dispatch);