Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / 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 } 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             // dispatch<any>(openDetailsPanel(uuid));
44             return;
45     }
46
47     switch (uuid) {
48         case SidePanelTreeCategory.PROJECTS:
49             const usr = getState().auth.user;
50             if (usr) {
51                 dispatch<any>(pushOrGoto(getNavUrl(usr.uuid, getState().auth)));
52             }
53             return;
54         case SidePanelTreeCategory.FAVORITES:
55             dispatch<any>(navigateToFavorites);
56             return;
57         case SidePanelTreeCategory.PUBLIC_FAVORITES:
58             dispatch(navigateToPublicFavorites);
59             return;
60         case SidePanelTreeCategory.SHARED_WITH_ME:
61             dispatch(navigateToSharedWithMe);
62             return;
63         case SidePanelTreeCategory.TRASH:
64             dispatch(navigateToTrash);
65             return;
66         case SidePanelTreeCategory.GROUPS:
67             dispatch(navigateToGroups);
68             return;
69         case SidePanelTreeCategory.ALL_PROCESSES:
70             dispatch(navigateToAllProcesses);
71             return;
72         case SidePanelTreeCategory.SHELL_ACCESS:
73             dispatch(navigateToUserVirtualMachines)
74             return;
75         case USERS_PANEL_LABEL:
76             dispatch(navigateToUsers);
77             return;
78         case MY_ACCOUNT_PANEL_LABEL:
79             dispatch(navigateToMyAccount);
80             return;
81     }
82
83     dispatch(navigationNotAvailable(uuid));
84 };
85
86 export const navigateToNotFound = push(Routes.NO_MATCH);
87
88 export const navigateToRoot = push(Routes.ROOT);
89
90 export const navigateToFavorites = push(Routes.FAVORITES);
91
92 export const navigateToTrash = push(Routes.TRASH);
93
94 export const navigateToPublicFavorites = push(Routes.PUBLIC_FAVORITES);
95
96 export const navigateToWorkflows = push(Routes.WORKFLOWS);
97
98 export const pushOrGoto = (url: string): AnyAction => {
99     if (url === "") {
100         return { type: "noop" };
101     } else if (url[0] === "/") {
102         return push(url);
103     } else {
104         window.location.href = url;
105         return { type: "noop" };
106     }
107 };
108
109 export const navigateToRootProject = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
110     navigateTo(SidePanelTreeCategory.PROJECTS)(dispatch, getState);
111 };
112
113 export const navigateToSharedWithMe = push(Routes.SHARED_WITH_ME);
114
115 export const navigateToRunProcess = push(Routes.RUN_PROCESS);
116
117 export const navigateToSearchResults = (searchValue: string) => {
118     if (searchValue !== "") {
119         return push({ pathname: Routes.SEARCH_RESULTS, search: "?q=" + encodeURIComponent(searchValue) });
120     } else {
121         return push({ pathname: Routes.SEARCH_RESULTS });
122     }
123 };
124
125 export const navigateToUserVirtualMachines = push(Routes.VIRTUAL_MACHINES_USER);
126
127 export const navigateToAdminVirtualMachines = push(Routes.VIRTUAL_MACHINES_ADMIN);
128
129 export const navigateToRepositories = push(Routes.REPOSITORIES);
130
131 export const navigateToSshKeysAdmin = push(Routes.SSH_KEYS_ADMIN);
132
133 export const navigateToSshKeysUser = push(Routes.SSH_KEYS_USER);
134
135 export const navigateToSiteManager = push(Routes.SITE_MANAGER);
136
137 export const navigateToMyAccount = push(Routes.MY_ACCOUNT);
138
139 export const navigateToLinkAccount = push(Routes.LINK_ACCOUNT);
140
141 export const navigateToKeepServices = push(Routes.KEEP_SERVICES);
142
143 export const navigateToUsers = push(Routes.USERS);
144
145 export const navigateToUserProfile = compose(push, getUserProfileUrl);
146
147 export const navigateToApiClientAuthorizations = push(Routes.API_CLIENT_AUTHORIZATIONS);
148
149 export const navigateToGroups = push(Routes.GROUPS);
150
151 export const navigateToGroupDetails = compose(push, getGroupUrl);
152
153 export const navigateToLinks = push(Routes.LINKS);
154
155 export const navigateToCollectionsContentAddress = push(Routes.COLLECTIONS_CONTENT_ADDRESS);
156
157 export const navigateToAllProcesses = push(Routes.ALL_PROCESSES);