Merge remote-tracking branch 'origin/main' into 19051-handle-quotes-in-search
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
index dc680215c1cf680db7264e54c73292f02aae7aff..146530cae8e3ffaf530da19ca3be07743026c78f 100644 (file)
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Dispatch } from "redux";
-import { projectActions, getProjectList } from "../project/project-action";
+import { Dispatch, compose, AnyAction } from 'redux';
 import { push } from "react-router-redux";
-import { TreeItemStatus } from "../../components/tree/tree";
-import { findTreeItem } from "../project/project-reducer";
-import { RootState } from "../store";
-import { Resource, ResourceKind } from "../../models/resource";
-import { projectPanelActions } from "../project-panel/project-panel-action";
-import { getCollectionUrl } from "../../models/collection";
-import { getProjectUrl, ProjectResource } from "../../models/project";
-import { ProjectService } from "../../services/project-service/project-service";
-import { ServiceRepository } from "../../services/services";
-import { sidePanelActions } from "../side-panel/side-panel-action";
-import { SidePanelIdentifiers } from "../side-panel/side-panel-reducer";
-import { getUuidObjectType, ObjectTypes } from "../../models/object-types";
-
-export const getResourceUrl = <T extends Resource>(resource: T): string => {
-    switch (resource.kind) {
-        case ResourceKind.PROJECT: return getProjectUrl(resource.uuid);
-        case ResourceKind.COLLECTION: return getCollectionUrl(resource.uuid);
-        default: return resource.href;
-    }
-};
-
-export enum ItemMode {
-    BOTH,
-    OPEN,
-    ACTIVE
-}
+import { ResourceKind, extractUuidKind } from 'models/resource';
+import { SidePanelTreeCategory } from '../side-panel-tree/side-panel-tree-actions';
+import { Routes, getGroupUrl, getNavUrl, getUserProfileUrl } from 'routes/routes';
+import { RootState } from 'store/store';
+import { openDetailsPanel } from 'store/details-panel/details-panel-action';
+import { ServiceRepository } from 'services/services';
+import { pluginConfig } from 'plugins';
+import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
+import { USERS_PANEL_LABEL, MY_ACCOUNT_PANEL_LABEL } from 'store/breadcrumbs/breadcrumbs-actions';
 
-export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
-    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const { projects, router } = getState();
-        const treeItem = findTreeItem(projects.items, itemId);
+export const navigationNotAvailable = (id: string) =>
+    snackbarActions.OPEN_SNACKBAR({
+        message: `${id} not available`,
+        hideDuration: 3000,
+        kind: SnackbarKind.ERROR
+    });
 
-        if (treeItem) {
-            const resourceUrl = getResourceUrl(treeItem.data);
+export const navigateTo = (uuid: string) =>
+    async (dispatch: Dispatch, getState: () => RootState) => {
 
-            if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
-                if (router.location && !router.location.pathname.includes(resourceUrl)) {
-                    dispatch(push(resourceUrl));
-                }
-                dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
+        for (const navToFn of pluginConfig.navigateToHandlers) {
+            if (navToFn(dispatch, getState, uuid)) {
+                return;
             }
+        }
 
-            const promise = treeItem.status === TreeItemStatus.LOADED
-                ? Promise.resolve()
-                : dispatch<any>(getProjectList(itemId));
-
-            promise
-                .then(() => dispatch<any>(() => {
-                    if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
-                        dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
-                    }
-                    dispatch(projectPanelActions.RESET_PAGINATION());
-                    dispatch(projectPanelActions.REQUEST_ITEMS());
-                }));
-
-        } else {
-            const uuid = services.authService.getUuid();
-            if (itemId === uuid) {
-                dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
-                dispatch(projectPanelActions.RESET_PAGINATION());
-                dispatch(projectPanelActions.REQUEST_ITEMS());
-            }
+        const kind = extractUuidKind(uuid);
+        switch (kind) {
+            case ResourceKind.PROJECT:
+            case ResourceKind.USER:
+            case ResourceKind.COLLECTION:
+            case ResourceKind.CONTAINER_REQUEST:
+                dispatch<any>(pushOrGoto(getNavUrl(uuid, getState().auth)));
+                return;
+            case ResourceKind.VIRTUAL_MACHINE:
+                dispatch<any>(navigateToAdminVirtualMachines);
+                return;
+            case ResourceKind.WORKFLOW:
+                dispatch<any>(openDetailsPanel(uuid));
+                return;
+        }
+
+        switch (uuid) {
+            case SidePanelTreeCategory.PROJECTS:
+                const usr = getState().auth.user;
+                if (usr) {
+                    dispatch<any>(pushOrGoto(getNavUrl(usr.uuid, getState().auth)));
+                }
+                return;
+            case SidePanelTreeCategory.FAVORITES:
+                dispatch<any>(navigateToFavorites);
+                return;
+            case SidePanelTreeCategory.PUBLIC_FAVORITES:
+                dispatch(navigateToPublicFavorites);
+                return;
+            case SidePanelTreeCategory.SHARED_WITH_ME:
+                dispatch(navigateToSharedWithMe);
+                return;
+            case SidePanelTreeCategory.TRASH:
+                dispatch(navigateToTrash);
+                return;
+            case SidePanelTreeCategory.GROUPS:
+                dispatch(navigateToGroups);
+                return;
+            case SidePanelTreeCategory.ALL_PROCESSES:
+                dispatch(navigateToAllProcesses);
+                return;
+            case USERS_PANEL_LABEL:
+                dispatch(navigateToUsers);
+                return;
+            case MY_ACCOUNT_PANEL_LABEL:
+                dispatch(navigateToMyAccount);
+                return;
         }
-    };
 
-export const restoreBranch = (itemId: string) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const ancestors = await loadProjectAncestors(itemId, services.projectService);
-        const uuids = ancestors.map(ancestor => ancestor.uuid);
-        await loadBranch(uuids, dispatch);
-        dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelIdentifiers.PROJECTS));
-        dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
-        uuids.forEach(uuid => {
-            dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
-        });
+        dispatch(navigationNotAvailable(uuid));
     };
 
-export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
-    if (getUuidObjectType(uuid) === ObjectTypes.USER) {
-        return [];
+
+export const navigateToNotFound = push(Routes.NO_MATCH);
+
+export const navigateToRoot = push(Routes.ROOT);
+
+export const navigateToFavorites = push(Routes.FAVORITES);
+
+export const navigateToTrash = push(Routes.TRASH);
+
+export const navigateToPublicFavorites = push(Routes.PUBLIC_FAVORITES);
+
+export const navigateToWorkflows = push(Routes.WORKFLOWS);
+
+export const pushOrGoto = (url: string): AnyAction => {
+    if (url === "") {
+        return { type: "noop" };
+    } else if (url[0] === '/') {
+        return push(url);
     } else {
-        const currentProject = await projectService.get(uuid);
-        const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
-        return [...ancestors, currentProject];
+        window.location.href = url;
+        return { type: "noop" };
     }
 };
 
-const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
-    const [uuid, ...rest] = uuids;
-    if (uuid) {
-        await dispatch<any>(getProjectList(uuid));
-        return loadBranch(rest, dispatch);
+
+export const navigateToRootProject = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+    navigateTo(SidePanelTreeCategory.PROJECTS)(dispatch, getState);
+};
+
+export const navigateToSharedWithMe = push(Routes.SHARED_WITH_ME);
+
+export const navigateToRunProcess = push(Routes.RUN_PROCESS);
+
+export const navigateToSearchResults = (searchValue: string) => {
+    if (searchValue !== "") {
+        return push({ pathname: Routes.SEARCH_RESULTS, search: '?q=' + encodeURIComponent(searchValue) });
+    } else {
+        return push({ pathname: Routes.SEARCH_RESULTS });
     }
 };
+
+export const navigateToUserVirtualMachines = push(Routes.VIRTUAL_MACHINES_USER);
+
+export const navigateToAdminVirtualMachines = push(Routes.VIRTUAL_MACHINES_ADMIN);
+
+export const navigateToRepositories = push(Routes.REPOSITORIES);
+
+export const navigateToSshKeysAdmin = push(Routes.SSH_KEYS_ADMIN);
+
+export const navigateToSshKeysUser = push(Routes.SSH_KEYS_USER);
+
+export const navigateToSiteManager = push(Routes.SITE_MANAGER);
+
+export const navigateToMyAccount = push(Routes.MY_ACCOUNT);
+
+export const navigateToLinkAccount = push(Routes.LINK_ACCOUNT);
+
+export const navigateToKeepServices = push(Routes.KEEP_SERVICES);
+
+export const navigateToUsers = push(Routes.USERS);
+
+export const navigateToUserProfile = compose(push, getUserProfileUrl);
+
+export const navigateToApiClientAuthorizations = push(Routes.API_CLIENT_AUTHORIZATIONS);
+
+export const navigateToGroups = push(Routes.GROUPS);
+
+export const navigateToGroupDetails = compose(push, getGroupUrl);
+
+export const navigateToLinks = push(Routes.LINKS);
+
+export const navigateToCollectionsContentAddress = push(Routes.COLLECTIONS_CONTENT_ADDRESS);
+
+export const navigateToAllProcesses = push(Routes.ALL_PROCESSES);