Merge branch '16672-live-container-logs'. Closes #16672
[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 } 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
15 const navigationNotAvailable = (id: string) =>
16     snackbarActions.OPEN_SNACKBAR({
17         message: `${id} not available`,
18         hideDuration: 3000,
19         kind: SnackbarKind.ERROR
20     });
21
22 export const navigateTo = (uuid: string) =>
23     async (dispatch: Dispatch, getState: () => RootState) => {
24
25         for (const navToFn of pluginConfig.navigateToHandlers) {
26             if (navToFn(dispatch, getState, uuid)) {
27                 return;
28             }
29         }
30
31         const kind = extractUuidKind(uuid);
32         switch (kind) {
33             case ResourceKind.PROJECT:
34             case ResourceKind.USER:
35             case ResourceKind.COLLECTION:
36             case ResourceKind.CONTAINER_REQUEST:
37                 dispatch<any>(pushOrGoto(getNavUrl(uuid, getState().auth)));
38                 return;
39             case ResourceKind.VIRTUAL_MACHINE:
40                 dispatch<any>(navigateToAdminVirtualMachines);
41                 return;
42         }
43
44         switch (uuid) {
45             case SidePanelTreeCategory.PROJECTS:
46                 const usr = getState().auth.user;
47                 if (usr) {
48                     dispatch<any>(pushOrGoto(getNavUrl(usr.uuid, getState().auth)));
49                 }
50                 return;
51             case SidePanelTreeCategory.FAVORITES:
52                 dispatch<any>(navigateToFavorites);
53                 return;
54             case SidePanelTreeCategory.PUBLIC_FAVORITES:
55                 dispatch(navigateToPublicFavorites);
56                 return;
57             case SidePanelTreeCategory.SHARED_WITH_ME:
58                 dispatch(navigateToSharedWithMe);
59                 return;
60             case SidePanelTreeCategory.WORKFLOWS:
61                 dispatch(navigateToWorkflows);
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         }
73
74         dispatch(navigationNotAvailable(uuid));
75     };
76
77
78 export const navigateToNotFound = push(Routes.NO_MATCH);
79
80 export const navigateToRoot = push(Routes.ROOT);
81
82 export const navigateToFavorites = push(Routes.FAVORITES);
83
84 export const navigateToTrash = push(Routes.TRASH);
85
86 export const navigateToPublicFavorites = push(Routes.PUBLIC_FAVORITES);
87
88 export const navigateToWorkflows = push(Routes.WORKFLOWS);
89
90 export const pushOrGoto = (url: string): AnyAction => {
91     if (url === "") {
92         return { type: "noop" };
93     } else if (url[0] === '/') {
94         return push(url);
95     } else {
96         window.location.href = url;
97         return { type: "noop" };
98     }
99 };
100
101
102 export const navigateToRootProject = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
103     navigateTo(SidePanelTreeCategory.PROJECTS)(dispatch, getState);
104 };
105
106 export const navigateToSharedWithMe = push(Routes.SHARED_WITH_ME);
107
108 export const navigateToRunProcess = push(Routes.RUN_PROCESS);
109
110 export const navigateToSearchResults = (searchValue: string) => {
111     if (searchValue !== "") {
112         return push({ pathname: Routes.SEARCH_RESULTS, search: '?q=' + encodeURIComponent(searchValue) });
113     } else {
114         return push({ pathname: Routes.SEARCH_RESULTS });
115     }
116 };
117
118 export const navigateToUserVirtualMachines = push(Routes.VIRTUAL_MACHINES_USER);
119
120 export const navigateToAdminVirtualMachines = push(Routes.VIRTUAL_MACHINES_ADMIN);
121
122 export const navigateToRepositories = push(Routes.REPOSITORIES);
123
124 export const navigateToSshKeysAdmin = push(Routes.SSH_KEYS_ADMIN);
125
126 export const navigateToSshKeysUser = push(Routes.SSH_KEYS_USER);
127
128 export const navigateToSiteManager = push(Routes.SITE_MANAGER);
129
130 export const navigateToMyAccount = push(Routes.MY_ACCOUNT);
131
132 export const navigateToLinkAccount = push(Routes.LINK_ACCOUNT);
133
134 export const navigateToKeepServices = push(Routes.KEEP_SERVICES);
135
136 export const navigateToUsers = push(Routes.USERS);
137
138 export const navigateToApiClientAuthorizations = push(Routes.API_CLIENT_AUTHORIZATIONS);
139
140 export const navigateToGroups = push(Routes.GROUPS);
141
142 export const navigateToGroupDetails = compose(push, getGroupUrl);
143
144 export const navigateToLinks = push(Routes.LINKS);
145
146 export const navigateToCollectionsContentAddress = push(Routes.COLLECTIONS_CONTENT_ADDRESS);
147
148 export const navigateToAllProcesses = push(Routes.ALL_PROCESSES);