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 { dataExplorerActions } from "../data-explorer/data-explorer-action";
11 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
12 import { RootState } from "../store";
13 import { Resource, ResourceKind } from "../../models/resource";
15 export const getResourceUrl = <T extends Resource>(resource: T): string => {
16 switch (resource.kind) {
17 case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
18 case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
19 default: return resource.href;
23 export enum ItemMode {
29 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
30 (dispatch: Dispatch, getState: () => RootState) => {
31 const { projects, router } = getState();
32 const treeItem = findTreeItem(projects.items, itemId);
36 const resourceUrl = getResourceUrl(treeItem.data);
38 if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
39 if (router.location && !router.location.pathname.includes(resourceUrl)) {
40 dispatch(push(resourceUrl));
42 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
45 const promise = treeItem.status === TreeItemStatus.LOADED
47 : dispatch<any>(getProjectList(itemId));
50 .then(() => dispatch<any>(() => {
51 if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
52 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
54 dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
55 dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));