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