21126: Merge branch 'main' into 21126-trash-when-ro
[arvados.git] / services / workbench2 / src / index.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6 import ReactDOM from "react-dom";
7 import { Provider } from "react-redux";
8 import { MainPanel } from "views/main-panel/main-panel";
9 import "index.css";
10 import { Route, Switch } from "react-router";
11 import { createBrowserHistory } from "history";
12 import { History } from "history";
13 import { configureStore, RootStore } from "store/store";
14 import { ConnectedRouter } from "react-router-redux";
15 import { ApiToken } from "views-components/api-token/api-token";
16 import { AddSession } from "views-components/add-session/add-session";
17 import { initAuth, logout } from "store/auth/auth-action";
18 import { createServices } from "services/services";
19 import { MuiThemeProvider } from "@material-ui/core/styles";
20 import { CustomTheme } from "common/custom-theme";
21 import { fetchConfig } from "common/config";
22 import servicesProvider from "common/service-provider";
23 import { addMenuActionSet, ContextMenuKind } from "views-components/context-menu/context-menu";
24 import { rootProjectActionSet } from "views-components/context-menu/action-sets/root-project-action-set";
25 import {
26     filterGroupActionSet,
27     frozenActionSet,
28     projectActionSet,
29     readOnlyProjectActionSet,
30 } from "views-components/context-menu/action-sets/project-action-set";
31 import { resourceActionSet } from "views-components/context-menu/action-sets/resource-action-set";
32 import { favoriteActionSet } from "views-components/context-menu/action-sets/favorite-action-set";
33 import {
34     collectionFilesActionSet,
35     collectionFilesMultipleActionSet,
36     readOnlyCollectionFilesActionSet,
37     readOnlyCollectionFilesMultipleActionSet,
38 } from "views-components/context-menu/action-sets/collection-files-action-set";
39 import {
40     collectionDirectoryItemActionSet,
41     collectionFileItemActionSet,
42     readOnlyCollectionDirectoryItemActionSet,
43     readOnlyCollectionFileItemActionSet,
44 } from "views-components/context-menu/action-sets/collection-files-item-action-set";
45 import { collectionFilesNotSelectedActionSet } from "views-components/context-menu/action-sets/collection-files-not-selected-action-set";
46 import {
47     collectionActionSet,
48     collectionAdminActionSet,
49     oldCollectionVersionActionSet,
50     readOnlyCollectionActionSet,
51 } from "views-components/context-menu/action-sets/collection-action-set";
52 import { loadWorkbench } from "store/workbench/workbench-actions";
53 import { Routes } from "routes/routes";
54 import { trashActionSet } from "views-components/context-menu/action-sets/trash-action-set";
55 import { ServiceRepository } from "services/services";
56 import { initWebSocket } from "websocket/websocket";
57 import { Config } from "common/config";
58 import { addRouteChangeHandlers } from "./routes/route-change-handlers";
59 import { setTokenDialogApiHost } from "store/token-dialog/token-dialog-actions";
60 import {
61     processResourceActionSet,
62     runningProcessResourceActionSet,
63     processResourceAdminActionSet,
64     runningProcessResourceAdminActionSet,
65     readOnlyProcessResourceActionSet,
66 } from "views-components/context-menu/action-sets/process-resource-action-set";
67 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
68 import { trashedCollectionActionSet } from "views-components/context-menu/action-sets/trashed-collection-action-set";
69 import { setBuildInfo } from "store/app-info/app-info-actions";
70 import { getBuildInfo } from "common/app-info";
71 import { DragDropContextProvider } from "react-dnd";
72 import HTML5Backend from "react-dnd-html5-backend";
73 import { initAdvancedFormProjectsTree } from "store/search-bar/search-bar-actions";
74 import { repositoryActionSet } from "views-components/context-menu/action-sets/repository-action-set";
75 import { sshKeyActionSet } from "views-components/context-menu/action-sets/ssh-key-action-set";
76 import { keepServiceActionSet } from "views-components/context-menu/action-sets/keep-service-action-set";
77 import { loadVocabulary } from "store/vocabulary/vocabulary-actions";
78 import { virtualMachineActionSet } from "views-components/context-menu/action-sets/virtual-machine-action-set";
79 import { userActionSet } from "views-components/context-menu/action-sets/user-action-set";
80 import { apiClientAuthorizationActionSet } from "views-components/context-menu/action-sets/api-client-authorization-action-set";
81 import { groupActionSet } from "views-components/context-menu/action-sets/group-action-set";
82 import { groupMemberActionSet } from "views-components/context-menu/action-sets/group-member-action-set";
83 import { linkActionSet } from "views-components/context-menu/action-sets/link-action-set";
84 import { loadFileViewersConfig } from "store/file-viewers/file-viewers-actions";
85 import {
86     filterGroupAdminActionSet,
87     frozenAdminActionSet,
88     projectAdminActionSet,
89 } from "views-components/context-menu/action-sets/project-admin-action-set";
90 import { permissionEditActionSet } from "views-components/context-menu/action-sets/permission-edit-action-set";
91 import { workflowActionSet, readOnlyWorkflowActionSet } from "views-components/context-menu/action-sets/workflow-action-set";
92 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
93 import { openNotFoundDialog } from "./store/not-found-panel/not-found-panel-action";
94 import { storeRedirects } from "./common/redirect-to";
95 import { searchResultsActionSet } from "views-components/context-menu/action-sets/search-results-action-set";
96
97 console.log(`Starting arvados [${getBuildInfo()}]`);
98
99 addMenuActionSet(ContextMenuKind.ROOT_PROJECT, rootProjectActionSet);
100 addMenuActionSet(ContextMenuKind.PROJECT, projectActionSet);
101 addMenuActionSet(ContextMenuKind.READONLY_PROJECT, readOnlyProjectActionSet);
102 addMenuActionSet(ContextMenuKind.FILTER_GROUP, filterGroupActionSet);
103 addMenuActionSet(ContextMenuKind.RESOURCE, resourceActionSet);
104 addMenuActionSet(ContextMenuKind.FAVORITE, favoriteActionSet);
105 addMenuActionSet(ContextMenuKind.COLLECTION_FILES, collectionFilesActionSet);
106 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_MULTIPLE, collectionFilesMultipleActionSet);
107 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILES, readOnlyCollectionFilesActionSet);
108 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILES_MULTIPLE, readOnlyCollectionFilesMultipleActionSet);
109 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_NOT_SELECTED, collectionFilesNotSelectedActionSet);
110 addMenuActionSet(ContextMenuKind.COLLECTION_DIRECTORY_ITEM, collectionDirectoryItemActionSet);
111 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_DIRECTORY_ITEM, readOnlyCollectionDirectoryItemActionSet);
112 addMenuActionSet(ContextMenuKind.COLLECTION_FILE_ITEM, collectionFileItemActionSet);
113 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILE_ITEM, readOnlyCollectionFileItemActionSet);
114 addMenuActionSet(ContextMenuKind.COLLECTION, collectionActionSet);
115 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION, readOnlyCollectionActionSet);
116 addMenuActionSet(ContextMenuKind.OLD_VERSION_COLLECTION, oldCollectionVersionActionSet);
117 addMenuActionSet(ContextMenuKind.TRASHED_COLLECTION, trashedCollectionActionSet);
118 addMenuActionSet(ContextMenuKind.PROCESS_RESOURCE, processResourceActionSet);
119 addMenuActionSet(ContextMenuKind.RUNNING_PROCESS_RESOURCE, runningProcessResourceActionSet);
120 addMenuActionSet(ContextMenuKind.READONLY_PROCESS_RESOURCE, readOnlyProcessResourceActionSet);
121 addMenuActionSet(ContextMenuKind.TRASH, trashActionSet);
122 addMenuActionSet(ContextMenuKind.REPOSITORY, repositoryActionSet);
123 addMenuActionSet(ContextMenuKind.SSH_KEY, sshKeyActionSet);
124 addMenuActionSet(ContextMenuKind.VIRTUAL_MACHINE, virtualMachineActionSet);
125 addMenuActionSet(ContextMenuKind.KEEP_SERVICE, keepServiceActionSet);
126 addMenuActionSet(ContextMenuKind.USER, userActionSet);
127 addMenuActionSet(ContextMenuKind.LINK, linkActionSet);
128 addMenuActionSet(ContextMenuKind.API_CLIENT_AUTHORIZATION, apiClientAuthorizationActionSet);
129 addMenuActionSet(ContextMenuKind.GROUPS, groupActionSet);
130 addMenuActionSet(ContextMenuKind.GROUP_MEMBER, groupMemberActionSet);
131 addMenuActionSet(ContextMenuKind.COLLECTION_ADMIN, collectionAdminActionSet);
132 addMenuActionSet(ContextMenuKind.PROCESS_ADMIN, processResourceAdminActionSet);
133 addMenuActionSet(ContextMenuKind.RUNNING_PROCESS_ADMIN, runningProcessResourceAdminActionSet);
134 addMenuActionSet(ContextMenuKind.PROJECT_ADMIN, projectAdminActionSet);
135 addMenuActionSet(ContextMenuKind.FROZEN_PROJECT, frozenActionSet);
136 addMenuActionSet(ContextMenuKind.FROZEN_PROJECT_ADMIN, frozenAdminActionSet);
137 addMenuActionSet(ContextMenuKind.FILTER_GROUP_ADMIN, filterGroupAdminActionSet);
138 addMenuActionSet(ContextMenuKind.PERMISSION_EDIT, permissionEditActionSet);
139 addMenuActionSet(ContextMenuKind.READONLY_WORKFLOW, readOnlyWorkflowActionSet);
140 addMenuActionSet(ContextMenuKind.WORKFLOW, workflowActionSet);
141 addMenuActionSet(ContextMenuKind.SEARCH_RESULTS, searchResultsActionSet);
142
143 storeRedirects();
144
145 fetchConfig().then(({ config, apiHost }) => {
146     const history = createBrowserHistory();
147
148     // Provide browser's history access to Cypress to allow programmatic
149     // navigation.
150     if ((window as any).Cypress) {
151         (window as any).appHistory = history;
152     }
153
154     const services = createServices(config, {
155         progressFn: (id, working) => {
156             //store.dispatch(progressIndicatorActions.TOGGLE_WORKING({ id, working }));
157         },
158         errorFn: (id, error, showSnackBar: boolean) => {
159             if (showSnackBar) {
160                 console.error("Backend error:", error);
161
162                 if (error.status === 404) {
163                     store.dispatch(openNotFoundDialog());
164                 } else if (error.status === 401 && error.errors[0].indexOf("Not logged in") > -1) {
165                     // Catch auth errors when navigating and redirect to login preserving url location
166                     store.dispatch(logout(false, true));
167                 } else {
168                     store.dispatch(
169                         snackbarActions.OPEN_SNACKBAR({
170                             message: `${error.errors ? error.errors[0] : error.message}`,
171                             kind: SnackbarKind.ERROR,
172                             hideDuration: 8000,
173                         })
174                     );
175                 }
176             }
177         },
178     });
179
180     // be sure this is initiated before the app starts
181     servicesProvider.setServices(services);
182
183     const store = configureStore(history, services, config);
184
185     servicesProvider.setStore(store);
186
187     store.subscribe(initListener(history, store, services, config));
188     store.dispatch(initAuth(config));
189     store.dispatch(setBuildInfo());
190     store.dispatch(setTokenDialogApiHost(apiHost));
191     store.dispatch(loadVocabulary);
192     store.dispatch(loadFileViewersConfig);
193
194     const TokenComponent = (props: any) => (
195         <ApiToken
196             authService={services.authService}
197             config={config}
198             loadMainApp={true}
199             {...props}
200         />
201     );
202     const AddSessionComponent = (props: any) => <AddSession {...props} />;
203     const FedTokenComponent = (props: any) => (
204         <ApiToken
205             authService={services.authService}
206             config={config}
207             loadMainApp={false}
208             {...props}
209         />
210     );
211     const MainPanelComponent = (props: any) => <MainPanel {...props} />;
212
213     const App = () => (
214         <MuiThemeProvider theme={CustomTheme}>
215             <DragDropContextProvider backend={HTML5Backend}>
216                 <Provider store={store}>
217                     <ConnectedRouter history={history}>
218                         <Switch>
219                             <Route
220                                 path={Routes.TOKEN}
221                                 component={TokenComponent}
222                             />
223                             <Route
224                                 path={Routes.FED_LOGIN}
225                                 component={FedTokenComponent}
226                             />
227                             <Route
228                                 path={Routes.ADD_SESSION}
229                                 component={AddSessionComponent}
230                             />
231                             <Route
232                                 path={Routes.ROOT}
233                                 component={MainPanelComponent}
234                             />
235                         </Switch>
236                     </ConnectedRouter>
237                 </Provider>
238             </DragDropContextProvider>
239         </MuiThemeProvider>
240     );
241
242     ReactDOM.render(<App />, document.getElementById("root") as HTMLElement);
243 });
244
245 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
246     let initialized = false;
247     return async () => {
248         const { router, auth } = store.getState();
249         if (router.location && auth.user && services.authService.getApiToken() && !initialized) {
250             initialized = true;
251             initWebSocket(config, services.authService, store);
252             await store.dispatch(loadWorkbench());
253             addRouteChangeHandlers(history, store);
254             // ToDo: move to searchBar component
255             store.dispatch(initAdvancedFormProjectsTree());
256         }
257     };
258 };