21386: allprocesses loading view good Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[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 { trashedCollectionActionSet } from "views-components/context-menu/action-sets/trashed-collection-action-set";
68 import { setBuildInfo } from "store/app-info/app-info-actions";
69 import { getBuildInfo } from "common/app-info";
70 import { DragDropContextProvider } from "react-dnd";
71 import HTML5Backend from "react-dnd-html5-backend";
72 import { initAdvancedFormProjectsTree } from "store/search-bar/search-bar-actions";
73 import { repositoryActionSet } from "views-components/context-menu/action-sets/repository-action-set";
74 import { sshKeyActionSet } from "views-components/context-menu/action-sets/ssh-key-action-set";
75 import { keepServiceActionSet } from "views-components/context-menu/action-sets/keep-service-action-set";
76 import { loadVocabulary } from "store/vocabulary/vocabulary-actions";
77 import { virtualMachineActionSet } from "views-components/context-menu/action-sets/virtual-machine-action-set";
78 import { userActionSet } from "views-components/context-menu/action-sets/user-action-set";
79 import { apiClientAuthorizationActionSet } from "views-components/context-menu/action-sets/api-client-authorization-action-set";
80 import { groupActionSet } from "views-components/context-menu/action-sets/group-action-set";
81 import { groupMemberActionSet } from "views-components/context-menu/action-sets/group-member-action-set";
82 import { linkActionSet } from "views-components/context-menu/action-sets/link-action-set";
83 import { loadFileViewersConfig } from "store/file-viewers/file-viewers-actions";
84 import {
85     filterGroupAdminActionSet,
86     frozenAdminActionSet,
87     projectAdminActionSet,
88 } from "views-components/context-menu/action-sets/project-admin-action-set";
89 import { permissionEditActionSet } from "views-components/context-menu/action-sets/permission-edit-action-set";
90 import { workflowActionSet, readOnlyWorkflowActionSet } from "views-components/context-menu/action-sets/workflow-action-set";
91 import { storeRedirects } from "./common/redirect-to";
92 import { searchResultsActionSet } from "views-components/context-menu/action-sets/search-results-action-set";
93
94 import 'bootstrap/dist/css/bootstrap.min.css';
95 import '@coreui/coreui/dist/css/coreui.min.css';
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         },
157         errorFn: (id, error, showSnackBar: boolean) => {
158             if (showSnackBar) {
159                 console.error("Backend error:", error);
160                 if (error.status === 401 && error.errors[0].indexOf("Not logged in") > -1) {
161                     // Catch auth errors when navigating and redirect to login preserving url location
162                     store.dispatch(logout(false, true));
163                 }
164             }
165         },
166     });
167
168     // be sure this is initiated before the app starts
169     servicesProvider.setServices(services);
170
171     const store = configureStore(history, services, config);
172
173     servicesProvider.setStore(store);
174
175     store.subscribe(initListener(history, store, services, config));
176     store.dispatch(initAuth(config));
177     store.dispatch(setBuildInfo());
178     store.dispatch(setTokenDialogApiHost(apiHost));
179     store.dispatch(loadVocabulary);
180     store.dispatch(loadFileViewersConfig);
181
182     const TokenComponent = (props: any) => (
183         <ApiToken
184             authService={services.authService}
185             config={config}
186             loadMainApp={true}
187             {...props}
188         />
189     );
190     const AddSessionComponent = (props: any) => <AddSession {...props} />;
191     const FedTokenComponent = (props: any) => (
192         <ApiToken
193             authService={services.authService}
194             config={config}
195             loadMainApp={false}
196             {...props}
197         />
198     );
199     const MainPanelComponent = (props: any) => <MainPanel {...props} />;
200
201     const App = () => (
202         <MuiThemeProvider theme={CustomTheme}>
203             <DragDropContextProvider backend={HTML5Backend}>
204                 <Provider store={store}>
205                     <ConnectedRouter history={history}>
206                         <Switch>
207                             <Route
208                                 path={Routes.TOKEN}
209                                 component={TokenComponent}
210                             />
211                             <Route
212                                 path={Routes.FED_LOGIN}
213                                 component={FedTokenComponent}
214                             />
215                             <Route
216                                 path={Routes.ADD_SESSION}
217                                 component={AddSessionComponent}
218                             />
219                             <Route
220                                 path={Routes.ROOT}
221                                 component={MainPanelComponent}
222                             />
223                         </Switch>
224                     </ConnectedRouter>
225                 </Provider>
226             </DragDropContextProvider>
227         </MuiThemeProvider>
228     );
229
230     ReactDOM.render(<App />, document.getElementById("root") as HTMLElement);
231 });
232
233 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
234     let initialized = false;
235     return async () => {
236         const { router, auth } = store.getState();
237         if (router.location && auth.user && services.authService.getApiToken() && !initialized) {
238             initialized = true;
239             initWebSocket(config, services.authService, store);
240             await store.dispatch(loadWorkbench());
241             addRouteChangeHandlers(history, store);
242             // ToDo: move to searchBar component
243             store.dispatch(initAdvancedFormProjectsTree());
244         }
245     };
246 };