16592: Adds tests exposing the bug.
[arvados.git] / src / index.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import * as 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 { addMenuActionSet, ContextMenuKind } from '~/views-components/context-menu/context-menu';
23 import { rootProjectActionSet } from "~/views-components/context-menu/action-sets/root-project-action-set";
24 import { projectActionSet, readOnlyProjectActionSet } from "~/views-components/context-menu/action-sets/project-action-set";
25 import { resourceActionSet } from '~/views-components/context-menu/action-sets/resource-action-set';
26 import { favoriteActionSet } from "~/views-components/context-menu/action-sets/favorite-action-set";
27 import { collectionFilesActionSet, readOnlyCollectionFilesActionSet } from '~/views-components/context-menu/action-sets/collection-files-action-set';
28 import { collectionFilesItemActionSet, readOnlyCollectionFilesItemActionSet } from '~/views-components/context-menu/action-sets/collection-files-item-action-set';
29 import { collectionFilesNotSelectedActionSet } from '~/views-components/context-menu/action-sets/collection-files-not-selected-action-set';
30 import { collectionActionSet, readOnlyCollectionActionSet } from '~/views-components/context-menu/action-sets/collection-action-set';
31 import { collectionResourceActionSet } from '~/views-components/context-menu/action-sets/collection-resource-action-set';
32 import { processActionSet } from '~/views-components/context-menu/action-sets/process-action-set';
33 import { loadWorkbench } from '~/store/workbench/workbench-actions';
34 import { Routes } from '~/routes/routes';
35 import { trashActionSet } from "~/views-components/context-menu/action-sets/trash-action-set";
36 import { ServiceRepository } from '~/services/services';
37 import { initWebSocket } from '~/websocket/websocket';
38 import { Config } from '~/common/config';
39 import { addRouteChangeHandlers } from './routes/route-change-handlers';
40 import { setCurrentTokenDialogApiHost } from '~/store/current-token-dialog/current-token-dialog-actions';
41 import { processResourceActionSet } from '~/views-components/context-menu/action-sets/process-resource-action-set';
42 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
43 import { trashedCollectionActionSet } from '~/views-components/context-menu/action-sets/trashed-collection-action-set';
44 import { setBuildInfo } from '~/store/app-info/app-info-actions';
45 import { getBuildInfo } from '~/common/app-info';
46 import { DragDropContextProvider } from 'react-dnd';
47 import HTML5Backend from 'react-dnd-html5-backend';
48 import { initAdvancedFormProjectsTree } from '~/store/search-bar/search-bar-actions';
49 import { repositoryActionSet } from '~/views-components/context-menu/action-sets/repository-action-set';
50 import { sshKeyActionSet } from '~/views-components/context-menu/action-sets/ssh-key-action-set';
51 import { keepServiceActionSet } from '~/views-components/context-menu/action-sets/keep-service-action-set';
52 import { loadVocabulary } from '~/store/vocabulary/vocabulary-actions';
53 import { virtualMachineActionSet } from '~/views-components/context-menu/action-sets/virtual-machine-action-set';
54 import { userActionSet } from '~/views-components/context-menu/action-sets/user-action-set';
55 import { computeNodeActionSet } from '~/views-components/context-menu/action-sets/compute-node-action-set';
56 import { apiClientAuthorizationActionSet } from '~/views-components/context-menu/action-sets/api-client-authorization-action-set';
57 import { groupActionSet } from '~/views-components/context-menu/action-sets/group-action-set';
58 import { groupMemberActionSet } from '~/views-components/context-menu/action-sets/group-member-action-set';
59 import { linkActionSet } from '~/views-components/context-menu/action-sets/link-action-set';
60 import { loadFileViewersConfig } from '~/store/file-viewers/file-viewers-actions';
61 import { collectionAdminActionSet } from '~/views-components/context-menu/action-sets/collection-admin-action-set';
62 import { processResourceAdminActionSet } from '~/views-components/context-menu/action-sets/process-resource-admin-action-set';
63 import { projectAdminActionSet } from '~/views-components/context-menu/action-sets/project-admin-action-set';
64 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
65 import { openNotFoundDialog } from './store/not-found-panel/not-found-panel-action';
66
67 console.log(`Starting arvados [${getBuildInfo()}]`);
68
69 addMenuActionSet(ContextMenuKind.ROOT_PROJECT, rootProjectActionSet);
70 addMenuActionSet(ContextMenuKind.PROJECT, projectActionSet);
71 addMenuActionSet(ContextMenuKind.READONLY_PROJECT, readOnlyProjectActionSet);
72 addMenuActionSet(ContextMenuKind.RESOURCE, resourceActionSet);
73 addMenuActionSet(ContextMenuKind.FAVORITE, favoriteActionSet);
74 addMenuActionSet(ContextMenuKind.COLLECTION_FILES, collectionFilesActionSet);
75 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILES, readOnlyCollectionFilesActionSet);
76 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_NOT_SELECTED, collectionFilesNotSelectedActionSet);
77 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_ITEM, collectionFilesItemActionSet);
78 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION_FILES_ITEM, readOnlyCollectionFilesItemActionSet);
79 addMenuActionSet(ContextMenuKind.COLLECTION, collectionActionSet);
80 addMenuActionSet(ContextMenuKind.COLLECTION_RESOURCE, collectionResourceActionSet);
81 addMenuActionSet(ContextMenuKind.READONLY_COLLECTION, readOnlyCollectionActionSet);
82 addMenuActionSet(ContextMenuKind.TRASHED_COLLECTION, trashedCollectionActionSet);
83 addMenuActionSet(ContextMenuKind.PROCESS, processActionSet);
84 addMenuActionSet(ContextMenuKind.PROCESS_RESOURCE, processResourceActionSet);
85 addMenuActionSet(ContextMenuKind.TRASH, trashActionSet);
86 addMenuActionSet(ContextMenuKind.REPOSITORY, repositoryActionSet);
87 addMenuActionSet(ContextMenuKind.SSH_KEY, sshKeyActionSet);
88 addMenuActionSet(ContextMenuKind.VIRTUAL_MACHINE, virtualMachineActionSet);
89 addMenuActionSet(ContextMenuKind.KEEP_SERVICE, keepServiceActionSet);
90 addMenuActionSet(ContextMenuKind.USER, userActionSet);
91 addMenuActionSet(ContextMenuKind.LINK, linkActionSet);
92 addMenuActionSet(ContextMenuKind.NODE, computeNodeActionSet);
93 addMenuActionSet(ContextMenuKind.API_CLIENT_AUTHORIZATION, apiClientAuthorizationActionSet);
94 addMenuActionSet(ContextMenuKind.GROUPS, groupActionSet);
95 addMenuActionSet(ContextMenuKind.GROUP_MEMBER, groupMemberActionSet);
96 addMenuActionSet(ContextMenuKind.COLLECTION_ADMIN, collectionAdminActionSet);
97 addMenuActionSet(ContextMenuKind.PROCESS_ADMIN, processResourceAdminActionSet);
98 addMenuActionSet(ContextMenuKind.PROJECT_ADMIN, projectAdminActionSet);
99
100 fetchConfig()
101     .then(({ config, apiHost }) => {
102         const history = createBrowserHistory();
103         const services = createServices(config, {
104             progressFn: (id, working) => {
105                 store.dispatch(progressIndicatorActions.TOGGLE_WORKING({ id, working }));
106             },
107             errorFn: (id, error, showSnackBar) => {
108                 if (showSnackBar) {
109                     console.error("Backend error:", error);
110
111                     if (error.status === 404) {
112                         store.dispatch(openNotFoundDialog());
113                     } else if (error.status === 401 && error.errors[0].indexOf("Not logged in") > -1) {
114                         store.dispatch(logout());
115                     } else {
116                         store.dispatch(snackbarActions.OPEN_SNACKBAR({
117                             message: `${error.errors
118                                 ? error.errors[0]
119                                 : error.message}`,
120                             kind: SnackbarKind.ERROR,
121                             hideDuration: 8000})
122                         );
123                     }
124                 }
125             }
126         });
127         const store = configureStore(history, services);
128
129         store.subscribe(initListener(history, store, services, config));
130         store.dispatch(initAuth(config));
131         store.dispatch(setBuildInfo());
132         store.dispatch(setCurrentTokenDialogApiHost(apiHost));
133         store.dispatch(loadVocabulary);
134         store.dispatch(loadFileViewersConfig);
135
136         const TokenComponent = (props: any) => <ApiToken authService={services.authService} config={config} loadMainApp={true} {...props} />;
137         const AddSessionComponent = (props: any) => <AddSession {...props} />;
138         const FedTokenComponent = (props: any) => <ApiToken authService={services.authService} config={config} loadMainApp={false} {...props} />;
139         const MainPanelComponent = (props: any) => <MainPanel {...props} />;
140
141         const App = () =>
142             <MuiThemeProvider theme={CustomTheme}>
143                 <DragDropContextProvider backend={HTML5Backend}>
144                     <Provider store={store}>
145                         <ConnectedRouter history={history}>
146                             <Switch>
147                                 <Route path={Routes.TOKEN} component={TokenComponent} />
148                                 <Route path={Routes.FED_LOGIN} component={FedTokenComponent} />
149                                 <Route path={Routes.ADD_SESSION} component={AddSessionComponent} />
150                                 <Route path={Routes.ROOT} component={MainPanelComponent} />
151                             </Switch>
152                         </ConnectedRouter>
153                     </Provider>
154                 </DragDropContextProvider>
155             </MuiThemeProvider>;
156
157         ReactDOM.render(
158             <App />,
159             document.getElementById('root') as HTMLElement
160         );
161     });
162
163 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
164     let initialized = false;
165     return async () => {
166         const { router, auth } = store.getState();
167         if (router.location && auth.user && !initialized) {
168             initialized = true;
169             initWebSocket(config, services.authService, store);
170             await store.dispatch(loadWorkbench());
171             addRouteChangeHandlers(history, store);
172             // ToDo: move to searchBar component
173             store.dispatch(initAdvancedFormProjectsTree());
174         }
175     };
176 };