21720:
[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 { ThemeProvider, Theme, StyledEngineProvider } from "@mui/material/styles";
20 import { CustomTheme } from "common/custom-theme";
21 import { fetchConfig } from "common/config";
22 import servicesProvider from "common/service-provider";
23 import { addMenuActionSet } from "views-components/context-menu/context-menu";
24 import { ContextMenuKind } from "views-components/context-menu/menu-item-sort";
25 import { rootProjectActionSet } from "views-components/context-menu/action-sets/root-project-action-set";
26 import {
27     filterGroupActionSet,
28     frozenActionSet,
29     projectActionSet,
30     readOnlyProjectActionSet,
31 } from "views-components/context-menu/action-sets/project-action-set";
32 import { resourceActionSet } from "views-components/context-menu/action-sets/resource-action-set";
33 import { favoriteActionSet } from "views-components/context-menu/action-sets/favorite-action-set";
34 import {
35     collectionFilesActionSet,
36     collectionFilesMultipleActionSet,
37     readOnlyCollectionFilesActionSet,
38     readOnlyCollectionFilesMultipleActionSet,
39 } from "views-components/context-menu/action-sets/collection-files-action-set";
40 import {
41     collectionDirectoryItemActionSet,
42     collectionFileItemActionSet,
43     readOnlyCollectionDirectoryItemActionSet,
44     readOnlyCollectionFileItemActionSet,
45 } from "views-components/context-menu/action-sets/collection-files-item-action-set";
46 import { collectionFilesNotSelectedActionSet } from "views-components/context-menu/action-sets/collection-files-not-selected-action-set";
47 import {
48     collectionActionSet,
49     collectionAdminActionSet,
50     oldCollectionVersionActionSet,
51     readOnlyCollectionActionSet,
52 } from "views-components/context-menu/action-sets/collection-action-set";
53 import { loadWorkbench } from "store/workbench/workbench-actions";
54 import { Routes } from "routes/routes";
55 import { trashActionSet } from "views-components/context-menu/action-sets/trash-action-set";
56 import { ServiceRepository } from "services/services";
57 import { initWebSocket } from "websocket/websocket";
58 import { Config } from "common/config";
59 import { addRouteChangeHandlers } from "./routes/route-change-handlers";
60 import { setTokenDialogApiHost } from "store/token-dialog/token-dialog-actions";
61 import {
62     processResourceActionSet,
63     runningProcessResourceActionSet,
64     processResourceAdminActionSet,
65     runningProcessResourceAdminActionSet,
66     readOnlyProcessResourceActionSet,
67 } from "views-components/context-menu/action-sets/process-resource-action-set";
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 { UserDetailsActionSet } from "views-components/context-menu/action-sets/user-details-action-set";
81 import { apiClientAuthorizationActionSet } from "views-components/context-menu/action-sets/api-client-authorization-action-set";
82 import { groupActionSet } from "views-components/context-menu/action-sets/group-action-set";
83 import { groupMemberActionSet } from "views-components/context-menu/action-sets/group-member-action-set";
84 import { linkActionSet } from "views-components/context-menu/action-sets/link-action-set";
85 import { loadFileViewersConfig } from "store/file-viewers/file-viewers-actions";
86 import {
87     filterGroupAdminActionSet,
88     frozenAdminActionSet,
89     projectAdminActionSet,
90 } from "views-components/context-menu/action-sets/project-admin-action-set";
91 import { permissionEditActionSet } from "views-components/context-menu/action-sets/permission-edit-action-set";
92 import { workflowActionSet, readOnlyWorkflowActionSet } from "views-components/context-menu/action-sets/workflow-action-set";
93 import { storeRedirects } from "./common/redirect-to";
94 import { searchResultsActionSet } from "views-components/context-menu/action-sets/search-results-action-set";
95
96 import 'bootstrap/dist/css/bootstrap.min.css';
97 import '@coreui/coreui/dist/css/coreui.min.css';
98
99
100 declare module '@mui/styles/defaultTheme' {
101   // eslint-disable-next-line @typescript-eslint/no-empty-interface
102   interface DefaultTheme extends Theme {}
103 }
104
105
106 console.log(`Starting arvados [${getBuildInfo()}]`);
107
108 addMenuActionSet(ContextMenuKind.ROOT_PROJECT, rootProjectActionSet);
109 addMenuActionSet(ContextMenuKind.PROJECT, projectActionSet);
110 addMenuActionSet(ContextMenuKind.READONLY_PROJECT, readOnlyProjectActionSet);
111 addMenuActionSet(ContextMenuKind.FILTER_GROUP, filterGroupActionSet);
112 addMenuActionSet(ContextMenuKind.RESOURCE, resourceActionSet);
113 addMenuActionSet(ContextMenuKind.FAVORITE, favoriteActionSet);
114 addMenuActionSet(ContextMenuKind.COLLECTION_FILES, collectionFilesActionSet);
115 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_MULTIPLE, collectionFilesMultipleActionSet);
116 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILES, readOnlyCollectionFilesActionSet);
117 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILES_MULTIPLE, readOnlyCollectionFilesMultipleActionSet);
118 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_NOT_SELECTED, collectionFilesNotSelectedActionSet);
119 addMenuActionSet(ContextMenuKind.COLLECTION_DIRECTORY_ITEM, collectionDirectoryItemActionSet);
120 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_DIRECTORY_ITEM, readOnlyCollectionDirectoryItemActionSet);
121 addMenuActionSet(ContextMenuKind.COLLECTION_FILE_ITEM, collectionFileItemActionSet);
122 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILE_ITEM, readOnlyCollectionFileItemActionSet);
123 addMenuActionSet(ContextMenuKind.COLLECTION, collectionActionSet);
124 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION, readOnlyCollectionActionSet);
125 addMenuActionSet(ContextMenuKind.OLD_VERSION_COLLECTION, oldCollectionVersionActionSet);
126 addMenuActionSet(ContextMenuKind.TRASHED_COLLECTION, trashedCollectionActionSet);
127 addMenuActionSet(ContextMenuKind.PROCESS_RESOURCE, processResourceActionSet);
128 addMenuActionSet(ContextMenuKind.RUNNING_PROCESS_RESOURCE, runningProcessResourceActionSet);
129 addMenuActionSet(ContextMenuKind.READONLY_PROCESS_RESOURCE, readOnlyProcessResourceActionSet);
130 addMenuActionSet(ContextMenuKind.TRASH, trashActionSet);
131 addMenuActionSet(ContextMenuKind.REPOSITORY, repositoryActionSet);
132 addMenuActionSet(ContextMenuKind.SSH_KEY, sshKeyActionSet);
133 addMenuActionSet(ContextMenuKind.VIRTUAL_MACHINE, virtualMachineActionSet);
134 addMenuActionSet(ContextMenuKind.KEEP_SERVICE, keepServiceActionSet);
135 addMenuActionSet(ContextMenuKind.USER, userActionSet);
136 addMenuActionSet(ContextMenuKind.USER_DETAILS, UserDetailsActionSet);
137 addMenuActionSet(ContextMenuKind.LINK, linkActionSet);
138 addMenuActionSet(ContextMenuKind.API_CLIENT_AUTHORIZATION, apiClientAuthorizationActionSet);
139 addMenuActionSet(ContextMenuKind.GROUPS, groupActionSet);
140 addMenuActionSet(ContextMenuKind.GROUP_MEMBER, groupMemberActionSet);
141 addMenuActionSet(ContextMenuKind.COLLECTION_ADMIN, collectionAdminActionSet);
142 addMenuActionSet(ContextMenuKind.PROCESS_ADMIN, processResourceAdminActionSet);
143 addMenuActionSet(ContextMenuKind.RUNNING_PROCESS_ADMIN, runningProcessResourceAdminActionSet);
144 addMenuActionSet(ContextMenuKind.PROJECT_ADMIN, projectAdminActionSet);
145 addMenuActionSet(ContextMenuKind.FROZEN_PROJECT, frozenActionSet);
146 addMenuActionSet(ContextMenuKind.FROZEN_PROJECT_ADMIN, frozenAdminActionSet);
147 addMenuActionSet(ContextMenuKind.FILTER_GROUP_ADMIN, filterGroupAdminActionSet);
148 addMenuActionSet(ContextMenuKind.PERMISSION_EDIT, permissionEditActionSet);
149 addMenuActionSet(ContextMenuKind.READONLY_WORKFLOW, readOnlyWorkflowActionSet);
150 addMenuActionSet(ContextMenuKind.WORKFLOW, workflowActionSet);
151 addMenuActionSet(ContextMenuKind.SEARCH_RESULTS, searchResultsActionSet);
152
153 storeRedirects();
154
155 fetchConfig().then(({ config, apiHost }) => {
156     const history = createBrowserHistory();
157
158     // Provide browser's history access to Cypress to allow programmatic
159     // navigation.
160     if ((window as any).Cypress) {
161         (window as any).appHistory = history;
162     }
163
164     const services = createServices(config, {
165         progressFn: (id, working) => {
166         },
167         errorFn: (id, error, showSnackBar: boolean) => {
168             if (showSnackBar) {
169                 console.error("Backend error:", error);
170                 if (error.status === 401 && error.errors[0].indexOf("Not logged in") > -1) {
171                     // Catch auth errors when navigating and redirect to login preserving url location
172                     store.dispatch(logout(false, true));
173                 }
174             }
175         },
176     });
177
178     // be sure this is initiated before the app starts
179     servicesProvider.setServices(services);
180
181     const store = configureStore(history, services, config);
182
183     servicesProvider.setStore(store);
184
185     store.subscribe(initListener(history, store, services, config));
186     store.dispatch(initAuth(config));
187     store.dispatch(setBuildInfo());
188     store.dispatch(setTokenDialogApiHost(apiHost));
189     store.dispatch(loadVocabulary);
190     store.dispatch(loadFileViewersConfig);
191
192     const TokenComponent = (props: any) => (
193         <ApiToken
194             authService={services.authService}
195             config={config}
196             loadMainApp={true}
197             {...props}
198         />
199     );
200     const AddSessionComponent = (props: any) => <AddSession {...props} />;
201     const FedTokenComponent = (props: any) => (
202         <ApiToken
203             authService={services.authService}
204             config={config}
205             loadMainApp={false}
206             {...props}
207         />
208     );
209     const MainPanelComponent = (props: any) => <MainPanel {...props} />;
210
211     const App = () => (
212         <StyledEngineProvider injectFirst>
213             <ThemeProvider theme={CustomTheme}>
214                 <DragDropContextProvider backend={HTML5Backend}>
215                     <Provider store={store}>
216                         <ConnectedRouter history={history}>
217                             <Switch>
218                                 <Route
219                                     path={Routes.TOKEN}
220                                     component={TokenComponent}
221                                 />
222                                 <Route
223                                     path={Routes.FED_LOGIN}
224                                     component={FedTokenComponent}
225                                 />
226                                 <Route
227                                     path={Routes.ADD_SESSION}
228                                     component={AddSessionComponent}
229                                 />
230                                 <Route
231                                     path={Routes.ROOT}
232                                     component={MainPanelComponent}
233                                 />
234                             </Switch>
235                         </ConnectedRouter>
236                     </Provider>
237                 </DragDropContextProvider>
238             </ThemeProvider>
239         </StyledEngineProvider>
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             //expose store for cypress tests
257             if ((window as any).Cypress) {
258                 console.log("setting redux store to localstorage");
259                 window.localStorage.setItem("arvadosStore", JSON.stringify(store.getState()));
260                 (window as any).store = store;
261                 store.subscribe(() => {
262                     window.localStorage.setItem("arvadosStore", JSON.stringify(store.getState()));
263                 });
264             }
265         }
266     };
267 };