15814: Fix tabs -> spaces
[arvados.git] / services / workbench2 / src / store / navigation / navigation-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch, compose, AnyAction } from "redux";
6 import { push } from "react-router-redux";
7 import { ResourceKind, extractUuidKind } from "models/resource";
8 import { SidePanelTreeCategory } from "../side-panel-tree/side-panel-tree-actions";
9 import { Routes, getGroupUrl, getNavUrl, getUserProfileUrl } from "routes/routes";
10 import { RootState } from "store/store";
11 import { ServiceRepository } from "services/services";
12 import { pluginConfig } from "plugins";
13 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
14 import { USERS_PANEL_LABEL, MY_ACCOUNT_PANEL_LABEL, INSTANCE_TYPES_PANEL_LABEL, VIRTUAL_MACHINES_ADMIN_PANEL_LABEL, REPOSITORIES_PANEL_LABEL } from "store/breadcrumbs/breadcrumbs-actions";
15
16 export const navigationNotAvailable = (id: string) =>
17     snackbarActions.OPEN_SNACKBAR({
18         message: `${id} not available`,
19         hideDuration: 3000,
20         kind: SnackbarKind.ERROR,
21     });
22
23 export const navigateTo = (uuid: string) => async (dispatch: Dispatch, getState: () => RootState) => {
24     for (const navToFn of pluginConfig.navigateToHandlers) {
25         if (navToFn(dispatch, getState, uuid)) {
26             return;
27         }
28     }
29
30     const kind = extractUuidKind(uuid);
31     switch (kind) {
32         case ResourceKind.PROJECT:
33         case ResourceKind.USER:
34         case ResourceKind.COLLECTION:
35         case ResourceKind.CONTAINER_REQUEST:
36             dispatch<any>(pushOrGoto(getNavUrl(uuid, getState().auth)));
37             return;
38         case ResourceKind.VIRTUAL_MACHINE:
39             dispatch<any>(navigateToAdminVirtualMachines);
40             return;
41         case ResourceKind.WORKFLOW:
42             dispatch<any>(pushOrGoto(getNavUrl(uuid, getState().auth)));
43             return;
44     }
45
46     switch (uuid) {
47         case SidePanelTreeCategory.PROJECTS:
48             const usr = getState().auth.user;
49             if (usr) {
50                 dispatch<any>(pushOrGoto(getNavUrl(usr.uuid, getState().auth)));
51             }
52             return;
53         case SidePanelTreeCategory.FAVORITES:
54             dispatch<any>(navigateToFavorites);
55             return;
56         case SidePanelTreeCategory.PUBLIC_FAVORITES:
57             dispatch(navigateToPublicFavorites);
58             return;
59         case SidePanelTreeCategory.SHARED_WITH_ME:
60             dispatch(navigateToSharedWithMe);
61             return;
62         case SidePanelTreeCategory.TRASH:
63             dispatch(navigateToTrash);
64             return;
65         case SidePanelTreeCategory.GROUPS:
66             dispatch(navigateToGroups);
67             return;
68         case SidePanelTreeCategory.ALL_PROCESSES:
69             dispatch(navigateToAllProcesses);
70             return;
71         case SidePanelTreeCategory.SHELL_ACCESS:
72             dispatch(navigateToUserVirtualMachines)
73             return;
74         case USERS_PANEL_LABEL:
75             dispatch(navigateToUsers);
76             return;
77         case MY_ACCOUNT_PANEL_LABEL:
78             dispatch(navigateToMyAccount);
79             return;
80         case INSTANCE_TYPES_PANEL_LABEL:
81             dispatch(navigateToInstanceTypes);
82             return;
83         case VIRTUAL_MACHINES_ADMIN_PANEL_LABEL:
84             dispatch(navigateToAdminVirtualMachines);
85             return;
86         case REPOSITORIES_PANEL_LABEL:
87             dispatch(navigateToRepositories);
88             return;
89     }
90
91     dispatch(navigationNotAvailable(uuid));
92 };
93
94 export const navigateToNotFound = push(Routes.NO_MATCH);
95
96 export const navigateToRoot = push(Routes.ROOT);
97
98 export const navigateToFavorites = push(Routes.FAVORITES);
99
100 export const navigateToTrash = push(Routes.TRASH);
101
102 export const navigateToPublicFavorites = push(Routes.PUBLIC_FAVORITES);
103
104 export const navigateToWorkflows = push(Routes.WORKFLOWS);
105
106 export const pushOrGoto = (url: string): AnyAction => {
107     if (url === "") {
108         return { type: "noop" };
109     } else if (url[0] === "/") {
110         return push(url);
111     } else {
112         window.location.href = url;
113         return { type: "noop" };
114     }
115 };
116
117 export const navigateToRootProject = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
118     navigateTo(SidePanelTreeCategory.PROJECTS)(dispatch, getState);
119 };
120
121 export const navigateToSharedWithMe = push(Routes.SHARED_WITH_ME);
122
123 export const navigateToRunProcess = push(Routes.RUN_PROCESS);
124
125 export const navigateToSearchResults = (searchValue: string) => {
126     if (searchValue !== "") {
127         return push({ pathname: Routes.SEARCH_RESULTS, search: "?q=" + encodeURIComponent(searchValue) });
128     } else {
129         return push({ pathname: Routes.SEARCH_RESULTS });
130     }
131 };
132
133 export const navigateToUserVirtualMachines = push(Routes.VIRTUAL_MACHINES_USER);
134
135 export const navigateToAdminVirtualMachines = push(Routes.VIRTUAL_MACHINES_ADMIN);
136
137 export const navigateToRepositories = push(Routes.REPOSITORIES);
138
139 export const navigateToSshKeysAdmin = push(Routes.SSH_KEYS_ADMIN);
140
141 export const navigateToSshKeysUser = push(Routes.SSH_KEYS_USER);
142
143 export const navigateToInstanceTypes = push(Routes.INSTANCE_TYPES);
144
145 export const navigateToSiteManager = push(Routes.SITE_MANAGER);
146
147 export const navigateToMyAccount = push(Routes.MY_ACCOUNT);
148
149 export const navigateToLinkAccount = push(Routes.LINK_ACCOUNT);
150
151 export const navigateToKeepServices = push(Routes.KEEP_SERVICES);
152
153 export const navigateToUsers = push(Routes.USERS);
154
155 export const navigateToUserProfile = compose(push, getUserProfileUrl);
156
157 export const navigateToApiClientAuthorizations = push(Routes.API_CLIENT_AUTHORIZATIONS);
158
159 export const navigateToGroups = push(Routes.GROUPS);
160
161 export const navigateToGroupDetails = compose(push, getGroupUrl);
162
163 export const navigateToLinks = push(Routes.LINKS);
164
165 export const navigateToCollectionsContentAddress = push(Routes.COLLECTIONS_CONTENT_ADDRESS);
166
167 export const navigateToAllProcesses = push(Routes.ALL_PROCESSES);