refs #14266-display-uploaded-files
[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 * 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 } from 'react-router';
11 import createBrowserHistory from "history/createBrowserHistory";
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 { initAuth } from "./store/auth/auth-action";
17 import { createServices } from "./services/services";
18 import { MuiThemeProvider } from '@material-ui/core/styles';
19 import { CustomTheme } from './common/custom-theme';
20 import { fetchConfig } from './common/config';
21 import { addMenuActionSet, ContextMenuKind } from './views-components/context-menu/context-menu';
22 import { rootProjectActionSet } from "./views-components/context-menu/action-sets/root-project-action-set";
23 import { projectActionSet } from "./views-components/context-menu/action-sets/project-action-set";
24 import { resourceActionSet } from './views-components/context-menu/action-sets/resource-action-set';
25 import { favoriteActionSet } from "./views-components/context-menu/action-sets/favorite-action-set";
26 import { collectionFilesActionSet } from './views-components/context-menu/action-sets/collection-files-action-set';
27 import { collectionFilesItemActionSet } from './views-components/context-menu/action-sets/collection-files-item-action-set';
28 import { collectionActionSet } from './views-components/context-menu/action-sets/collection-action-set';
29 import { collectionResourceActionSet } from './views-components/context-menu/action-sets/collection-resource-action-set';
30 import { processActionSet } from './views-components/context-menu/action-sets/process-action-set';
31 import { loadWorkbench } from './store/workbench/workbench-actions';
32 import { Routes } from '~/routes/routes';
33 import { trashActionSet } from "~/views-components/context-menu/action-sets/trash-action-set";
34 import { ServiceRepository } from '~/services/services';
35 import { initWebSocket } from '~/websocket/websocket';
36 import { Config } from '~/common/config';
37 import { addRouteChangeHandlers } from './routes/route-change-handlers';
38 import { setCurrentTokenDialogApiHost } from '~/store/current-token-dialog/current-token-dialog-actions';
39 import { processResourceActionSet } from '~/views-components/context-menu/action-sets/process-resource-action-set';
40 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
41 import { setUuidPrefix } from '~/store/workflow-panel/workflow-panel-actions';
42 import { trashedCollectionActionSet } from '~/views-components/context-menu/action-sets/trashed-collection-action-set';
43
44 const getBuildNumber = () => "BN-" + (process.env.REACT_APP_BUILD_NUMBER || "dev");
45 const getGitCommit = () => "GIT-" + (process.env.REACT_APP_GIT_COMMIT || "latest").substr(0, 7);
46 const getBuildInfo = () => getBuildNumber() + " / " + getGitCommit();
47
48 const buildInfo = getBuildInfo();
49
50 console.log(`Starting arvados [${buildInfo}]`);
51
52 addMenuActionSet(ContextMenuKind.ROOT_PROJECT, rootProjectActionSet);
53 addMenuActionSet(ContextMenuKind.PROJECT, projectActionSet);
54 addMenuActionSet(ContextMenuKind.RESOURCE, resourceActionSet);
55 addMenuActionSet(ContextMenuKind.FAVORITE, favoriteActionSet);
56 addMenuActionSet(ContextMenuKind.COLLECTION_FILES, collectionFilesActionSet);
57 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_ITEM, collectionFilesItemActionSet);
58 addMenuActionSet(ContextMenuKind.COLLECTION, collectionActionSet);
59 addMenuActionSet(ContextMenuKind.COLLECTION_RESOURCE, collectionResourceActionSet);
60 addMenuActionSet(ContextMenuKind.TRASHED_COLLECTION, trashedCollectionActionSet);
61 addMenuActionSet(ContextMenuKind.PROCESS, processActionSet);
62 addMenuActionSet(ContextMenuKind.PROCESS_RESOURCE, processResourceActionSet);
63 addMenuActionSet(ContextMenuKind.TRASH, trashActionSet);
64
65 fetchConfig()
66     .then(({ config, apiHost }) => {
67         const history = createBrowserHistory();
68         const services = createServices(config, {
69             progressFn: (id, working) => {
70                 store.dispatch(progressIndicatorActions.TOGGLE_WORKING({ id, working }));
71             },
72             errorFn: (id, error) => {
73                 // console.error("Backend error:", error);
74                 // store.dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Backend error", kind: SnackbarKind.ERROR }));
75             }
76         });
77         const store = configureStore(history, services);
78
79         store.subscribe(initListener(history, store, services, config));
80         store.dispatch(initAuth());
81         store.dispatch(setCurrentTokenDialogApiHost(apiHost));
82         store.dispatch(setUuidPrefix(config.uuidPrefix));
83
84         const TokenComponent = (props: any) => <ApiToken authService={services.authService} {...props} />;
85         const MainPanelComponent = (props: any) => <MainPanel buildInfo={buildInfo} {...props} />;
86
87         const App = () =>
88             <MuiThemeProvider theme={CustomTheme}>
89                 <Provider store={store}>
90                     <ConnectedRouter history={history}>
91                         <div>
92                             <Route path={Routes.TOKEN} component={TokenComponent} />
93                             <Route path={Routes.ROOT} component={MainPanelComponent} />
94                         </div>
95                     </ConnectedRouter>
96                 </Provider>
97             </MuiThemeProvider>;
98
99         ReactDOM.render(
100             <App />,
101             document.getElementById('root') as HTMLElement
102         );
103     });
104
105 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
106     let initialized = false;
107     return async () => {
108         const { router, auth } = store.getState();
109         if (router.location && auth.user && !initialized) {
110             initialized = true;
111             initWebSocket(config, services.authService, store);
112             await store.dispatch(loadWorkbench());
113             addRouteChangeHandlers(history, store);
114         }
115     };
116 };
117
118