17426: Can add plugin middlewares and context menus
[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 { filterGroupActionSet, 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, collectionAdminActionSet, oldCollectionVersionActionSet, readOnlyCollectionActionSet } from '~/views-components/context-menu/action-sets/collection-action-set';
31 import { processActionSet } from '~/views-components/context-menu/action-sets/process-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 { processResourceActionSet, readOnlyProcessResourceActionSet } from '~/views-components/context-menu/action-sets/process-resource-action-set';
41 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
42 import { trashedCollectionActionSet } from '~/views-components/context-menu/action-sets/trashed-collection-action-set';
43 import { setBuildInfo } from '~/store/app-info/app-info-actions';
44 import { getBuildInfo } from '~/common/app-info';
45 import { DragDropContextProvider } from 'react-dnd';
46 import HTML5Backend from 'react-dnd-html5-backend';
47 import { initAdvancedFormProjectsTree } from '~/store/search-bar/search-bar-actions';
48 import { repositoryActionSet } from '~/views-components/context-menu/action-sets/repository-action-set';
49 import { sshKeyActionSet } from '~/views-components/context-menu/action-sets/ssh-key-action-set';
50 import { keepServiceActionSet } from '~/views-components/context-menu/action-sets/keep-service-action-set';
51 import { loadVocabulary } from '~/store/vocabulary/vocabulary-actions';
52 import { virtualMachineActionSet } from '~/views-components/context-menu/action-sets/virtual-machine-action-set';
53 import { userActionSet } from '~/views-components/context-menu/action-sets/user-action-set';
54 import { computeNodeActionSet } from '~/views-components/context-menu/action-sets/compute-node-action-set';
55 import { apiClientAuthorizationActionSet } from '~/views-components/context-menu/action-sets/api-client-authorization-action-set';
56 import { groupActionSet } from '~/views-components/context-menu/action-sets/group-action-set';
57 import { groupMemberActionSet } from '~/views-components/context-menu/action-sets/group-member-action-set';
58 import { linkActionSet } from '~/views-components/context-menu/action-sets/link-action-set';
59 import { loadFileViewersConfig } from '~/store/file-viewers/file-viewers-actions';
60 import { processResourceAdminActionSet } from '~/views-components/context-menu/action-sets/process-resource-admin-action-set';
61 import { filterGroupAdminActionSet, projectAdminActionSet } from '~/views-components/context-menu/action-sets/project-admin-action-set';
62 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
63 import { openNotFoundDialog } from './store/not-found-panel/not-found-panel-action';
64 import { storeRedirects } from './common/redirect-to';
65
66 console.log(`Starting arvados [${getBuildInfo()}]`);
67
68 addMenuActionSet(ContextMenuKind.ROOT_PROJECT, rootProjectActionSet);
69 addMenuActionSet(ContextMenuKind.PROJECT, projectActionSet);
70 addMenuActionSet(ContextMenuKind.READONLY_PROJECT, readOnlyProjectActionSet);
71 addMenuActionSet(ContextMenuKind.FILTER_GROUP, filterGroupActionSet);
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.READONLY_COLLECTION, readOnlyCollectionActionSet);
81 addMenuActionSet(ContextMenuKind.OLD_VERSION_COLLECTION, oldCollectionVersionActionSet);
82 addMenuActionSet(ContextMenuKind.TRASHED_COLLECTION, trashedCollectionActionSet);
83 addMenuActionSet(ContextMenuKind.PROCESS, processActionSet);
84 addMenuActionSet(ContextMenuKind.PROCESS_RESOURCE, processResourceActionSet);
85 addMenuActionSet(ContextMenuKind.READONLY_PROCESS_RESOURCE, readOnlyProcessResourceActionSet);
86 addMenuActionSet(ContextMenuKind.TRASH, trashActionSet);
87 addMenuActionSet(ContextMenuKind.REPOSITORY, repositoryActionSet);
88 addMenuActionSet(ContextMenuKind.SSH_KEY, sshKeyActionSet);
89 addMenuActionSet(ContextMenuKind.VIRTUAL_MACHINE, virtualMachineActionSet);
90 addMenuActionSet(ContextMenuKind.KEEP_SERVICE, keepServiceActionSet);
91 addMenuActionSet(ContextMenuKind.USER, userActionSet);
92 addMenuActionSet(ContextMenuKind.LINK, linkActionSet);
93 addMenuActionSet(ContextMenuKind.NODE, computeNodeActionSet);
94 addMenuActionSet(ContextMenuKind.API_CLIENT_AUTHORIZATION, apiClientAuthorizationActionSet);
95 addMenuActionSet(ContextMenuKind.GROUPS, groupActionSet);
96 addMenuActionSet(ContextMenuKind.GROUP_MEMBER, groupMemberActionSet);
97 addMenuActionSet(ContextMenuKind.COLLECTION_ADMIN, collectionAdminActionSet);
98 addMenuActionSet(ContextMenuKind.PROCESS_ADMIN, processResourceAdminActionSet);
99 addMenuActionSet(ContextMenuKind.PROJECT_ADMIN, projectAdminActionSet);
100 addMenuActionSet(ContextMenuKind.FILTER_GROUP_ADMIN, filterGroupAdminActionSet);
101
102 storeRedirects();
103
104 fetchConfig()
105     .then(({ config, apiHost }) => {
106         const history = createBrowserHistory();
107         const services = createServices(config, {
108             progressFn: (id, working) => {
109                 store.dispatch(progressIndicatorActions.TOGGLE_WORKING({ id, working }));
110             },
111             errorFn: (id, error, showSnackBar) => {
112                 if (showSnackBar) {
113                     console.error("Backend error:", error);
114
115                     if (error.status === 404) {
116                         store.dispatch(openNotFoundDialog());
117                     } else if (error.status === 401 && error.errors[0].indexOf("Not logged in") > -1) {
118                         store.dispatch(logout());
119                     } else {
120                         store.dispatch(snackbarActions.OPEN_SNACKBAR({
121                             message: `${error.errors
122                                 ? error.errors[0]
123                                 : error.message}`,
124                             kind: SnackbarKind.ERROR,
125                             hideDuration: 8000
126                         })
127                         );
128                     }
129                 }
130             }
131         });
132         const store = configureStore(history, services, config);
133
134         store.subscribe(initListener(history, store, services, config));
135         store.dispatch(initAuth(config));
136         store.dispatch(setBuildInfo());
137         store.dispatch(setTokenDialogApiHost(apiHost));
138         store.dispatch(loadVocabulary);
139         store.dispatch(loadFileViewersConfig);
140
141         const TokenComponent = (props: any) => <ApiToken authService={services.authService} config={config} loadMainApp={true} {...props} />;
142         const AddSessionComponent = (props: any) => <AddSession {...props} />;
143         const FedTokenComponent = (props: any) => <ApiToken authService={services.authService} config={config} loadMainApp={false} {...props} />;
144         const MainPanelComponent = (props: any) => <MainPanel {...props} />;
145
146         const App = () =>
147             <MuiThemeProvider theme={CustomTheme}>
148                 <DragDropContextProvider backend={HTML5Backend}>
149                     <Provider store={store}>
150                         <ConnectedRouter history={history}>
151                             <Switch>
152                                 <Route path={Routes.TOKEN} component={TokenComponent} />
153                                 <Route path={Routes.FED_LOGIN} component={FedTokenComponent} />
154                                 <Route path={Routes.ADD_SESSION} component={AddSessionComponent} />
155                                 <Route path={Routes.ROOT} component={MainPanelComponent} />
156                             </Switch>
157                         </ConnectedRouter>
158                     </Provider>
159                 </DragDropContextProvider>
160             </MuiThemeProvider>;
161
162         ReactDOM.render(
163             <App />,
164             document.getElementById('root') as HTMLElement
165         );
166     });
167
168 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
169     let initialized = false;
170     return async () => {
171         const { router, auth } = store.getState();
172         if (router.location && auth.user && !initialized) {
173             initialized = true;
174             initWebSocket(config, services.authService, store);
175             await store.dispatch(loadWorkbench());
176             addRouteChangeHandlers(history, store);
177             // ToDo: move to searchBar component
178             store.dispatch(initAdvancedFormProjectsTree());
179         }
180     };
181 };