Fix inifite render loop
[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 import { ContainerRequestState } from '~/models/container-request';
44 import { MountKind } from '~/models/mount-types';
45 import { setBuildInfo } from '~/store/app-info/app-info-actions';
46 import { getBuildInfo } from '~/common/app-info';
47 import { DragDropContextProvider } from 'react-dnd';
48 import HTML5Backend from 'react-dnd-html5-backend';
49
50 console.log(`Starting arvados [${getBuildInfo()}]`);
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(setBuildInfo());
82         store.dispatch(setCurrentTokenDialogApiHost(apiHost));
83         store.dispatch(setUuidPrefix(config.uuidPrefix));
84
85         const TokenComponent = (props: any) => <ApiToken authService={services.authService} {...props} />;
86         const MainPanelComponent = (props: any) => <MainPanel {...props} />;
87
88         const App = () =>
89             <MuiThemeProvider theme={CustomTheme}>
90                 <DragDropContextProvider backend={HTML5Backend}>
91                     <Provider store={store}>
92                         <ConnectedRouter history={history}>
93                             <div>
94                                 <Route path={Routes.TOKEN} component={TokenComponent} />
95                                 <Route path={Routes.ROOT} component={MainPanelComponent} />
96                             </div>
97                         </ConnectedRouter>
98                     </Provider>
99                 </DragDropContextProvider>
100             </MuiThemeProvider>;
101
102         ReactDOM.render(
103             <App />,
104             document.getElementById('root') as HTMLElement
105         );
106     });
107
108 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
109     let initialized = false;
110     return async () => {
111         const { router, auth } = store.getState();
112         if (router.location && auth.user && !initialized) {
113             initialized = true;
114             initWebSocket(config, services.authService, store);
115             await store.dispatch(loadWorkbench());
116             addRouteChangeHandlers(history, store);
117         }
118     };
119 };
120
121 const createPrimitivesCollectorWorkflow = ({ workflowService }: ServiceRepository) => {
122     workflowService.create({
123         name: 'Primitive values collector',
124         description: 'Workflow for collecting primitive values',
125         definition: "cwlVersion: v1.0\n$graph:\n- class: CommandLineTool\n  requirements:\n  - listing:\n    - entryname: input_collector.log\n      entry: |\n        \"flag\":\n          $(inputs.example_flag)\n        \"string\":\n          $(inputs.example_string)\n        \"int\":\n          $(inputs.example_int)\n        \"long\":\n          $(inputs.example_long)\n        \"float\":\n          $(inputs.example_float)\n        \"double\":\n          $(inputs.example_double)\n    class: InitialWorkDirRequirement\n  inputs:\n  - type: double\n    id: '#input_collector.cwl/example_double'\n  - type: boolean\n    id: '#input_collector.cwl/example_flag'\n  - type: float\n    id: '#input_collector.cwl/example_float'\n  - type: int\n    id: '#input_collector.cwl/example_int'\n  - type: long\n    id: '#input_collector.cwl/example_long'\n  - type: string\n    id: '#input_collector.cwl/example_string'\n  outputs:\n  - type: File\n    outputBinding:\n      glob: '*'\n    id: '#input_collector.cwl/output'\n  baseCommand: [echo]\n  id: '#input_collector.cwl'\n- class: Workflow\n  doc: Workflw for collecting primitive values\n  inputs:\n  - type: double\n    label: Double value\n    doc: This should allow for entering a decimal number (64-bit).\n    id: '#main/example_double'\n    default: 0.3333333333333333\n  - type: boolean\n    label: Boolean Flag\n    doc: This should render as in checkbox.\n    id: '#main/example_flag'\n    default: true\n  - type: float\n    label: Float value\n    doc: This should allow for entering a decimal number (32-bit).\n    id: '#main/example_float'\n    default: 0.15625\n  - type: int\n    label: Integer Number\n    doc: This should allow for entering a number (32-bit signed).\n    id: '#main/example_int'\n    default: 2147483647\n  - type: long\n    label: Long Number\n    doc: This should allow for entering a number (64-bit signed).\n    id: '#main/example_long'\n    default: 9223372036854775807\n  - type: string\n    label: Freetext\n    doc: This should allow for entering an arbitrary char sequence.\n    id: '#main/example_string'\n    default: This is a string\n  outputs:\n  - type: File\n    outputSource: '#main/input_collector/output'\n    id: '#main/log_file'\n  steps:\n  - run: '#input_collector.cwl'\n    in:\n    - source: '#main/example_double'\n      id: '#main/input_collector/example_double'\n    - source: '#main/example_flag'\n      id: '#main/input_collector/example_flag'\n    - source: '#main/example_float'\n      id: '#main/input_collector/example_float'\n    - source: '#main/example_int'\n      id: '#main/input_collector/example_int'\n    - source: '#main/example_long'\n      id: '#main/input_collector/example_long'\n    - source: '#main/example_string'\n      id: '#main/input_collector/example_string'\n    out: ['#main/input_collector/output']\n    id: '#main/input_collector'\n  id: '#main'\n",
126     });
127 };
128
129 const createEnumCollectorWorkflow = ({ workflowService }: ServiceRepository) => {
130     workflowService.create({
131         name: 'Enum values collector',
132         description: 'Workflow for collecting enum values',
133         definition: "cwlVersion: v1.0\n$graph:\n- class: CommandLineTool\n  requirements:\n  - listing:\n    - entryname: input_collector.log\n      entry: |\n        \"enum_type\":\n          $(inputs.enum_type)\n\n    class: InitialWorkDirRequirement\n  inputs:\n  - type:\n      type: enum\n      symbols: ['#input_collector.cwl/enum_type/OTU table', '#input_collector.cwl/enum_type/Pathway\n          table', '#input_collector.cwl/enum_type/Function table', '#input_collector.cwl/enum_type/Ortholog\n          table']\n    id: '#input_collector.cwl/enum_type'\n  outputs:\n  - type: File\n    outputBinding:\n      glob: '*'\n    id: '#input_collector.cwl/output'\n  baseCommand: [echo]\n  id: '#input_collector.cwl'\n- class: Workflow\n  doc: This is the description of the workflow\n  inputs:\n  - type:\n      type: enum\n      symbols: ['#main/enum_type/OTU table', '#main/enum_type/Pathway table', '#main/enum_type/Function\n          table', '#main/enum_type/Ortholog table']\n      name: '#enum_typef4179c7f-45f9-482d-a5db-1abb86698384'\n    label: Enumeration Type\n    doc: This should render as a drop-down menu.\n    id: '#main/enum_type'\n    default: OTU table\n  outputs:\n  - type: File\n    outputSource: '#main/input_collector/output'\n    id: '#main/log_file'\n  steps:\n  - run: '#input_collector.cwl'\n    in:\n    - source: '#main/enum_type'\n      id: '#main/input_collector/enum_type'\n    out: ['#main/input_collector/output']\n    id: '#main/input_collector'\n  id: '#main'\n",
134     });
135 };
136
137 const createFilesCollectorWorkflow = ({ workflowService }: ServiceRepository) => {
138     workflowService.create({
139         name: 'File values collector',
140         description: 'Workflow for collecting file values',
141         definition: "cwlVersion: v1.0\n$graph:\n- class: CommandLineTool\n\n  requirements:\n  - listing:\n    - entryname: input_collector.log\n      entry: |\n        \"single_file\":\n          $(inputs.single_file.basename)\n        \"optional_file\":\n          $(inputs.optional_file.basename)\n\n    class: InitialWorkDirRequirement\n  inputs:\n  - type:\n    - 'null'\n    - File\n    id: '#input_collector.cwl/optional_file'\n  - type:\n    - 'null'\n    - File\n    id: '#input_collector.cwl/optional_file_missing_label'\n  - type: File\n    id: '#input_collector.cwl/single_file'\n  outputs:\n  - type: File\n    outputBinding:\n      glob: '*'\n    id: '#input_collector.cwl/output'\n\n  baseCommand: [echo]\n  id: '#input_collector.cwl'\n- class: Workflow\n  doc: This is the description of the workflow\n  inputs:\n  - type:\n    - 'null'\n    - File\n    label: Single File (Optional)\n    doc: This should allow for single File selection only. Input should be marked\n      as optional and not enforced by form validation.\n    id: '#main/optional_file'\n    default:\n      class: File\n      location: keep:af831660d820bcbb98f473355e6e1b85+67/fileA\n      basename: fileA\n      nameroot: fileA\n      nameext: ''\n  - type:\n    - 'null'\n    - File\n    doc: Label should be the input field name because of missing label.\n    id: '#main/optional_file_missing_label'\n  - type: File\n    label: Single File\n    doc: This should allow for single File selection only.\n    id: '#main/single_file'\n    default:\n      class: File\n      location: keep:af831660d820bcbb98f473355e6e1b85+67/fileA\n      basename: fileA\n      nameroot: fileA\n      nameext: ''\n  outputs:\n  - type: File\n    outputSource: '#main/input_collector/output'\n    id: '#main/log_file'\n  steps:\n  - run: '#input_collector.cwl'\n    in:\n    - source: '#main/optional_file'\n      id: '#main/input_collector/optional_file'\n    - source: '#main/single_file'\n      id: '#main/input_collector/single_file'\n    out: ['#main/input_collector/output']\n    id: '#main/input_collector'\n  id: '#main'\n",
142     });
143 };
144
145 const createCollectionCollectorWorkflow = ({ workflowService }: ServiceRepository) => {
146     workflowService.create({
147         name: 'Collection value collector',
148         description: 'Workflow for collecting a collecion',
149         definition: "cwlVersion: v1.0\n$graph:\n- class: CommandLineTool\n\n  requirements:\n  - listing:\n    - entryname: input_collector.log\n      entry: |\n        \"collection\":\n          $(inputs.collection.location)\n\n    class: InitialWorkDirRequirement\n  inputs:\n  - type: Directory\n    id: '#input_collector.cwl/collection'\n\n  outputs:\n  - type: File\n    outputBinding:\n      glob: '*'\n    id: '#input_collector.cwl/output'\n\n  baseCommand: [echo]\n  id: '#input_collector.cwl'\n- class: Workflow\n  doc: This is the description of the workflow\n  inputs:\n  - type: Directory\n    label: Single Collection\n    doc: This should allow for single Collection selection only.\n    id: '#main/collection'\n    default:\n      class: Directory\n      location: keep:af831660d820bcbb98f473355e6e1b85+67\n      basename: af831660d820bcbb98f473355e6e1b85+67\n  outputs:\n  - type: File\n    outputSource: '#main/input_collector/output'\n\n    id: '#main/log_file'\n  steps:\n  - run: '#input_collector.cwl'\n    in:\n    - source: '#main/collection'\n      id: '#main/input_collector/collection'\n    out: ['#main/input_collector/output']\n    id: '#main/input_collector'\n  id: '#main'\n",
150     });
151 };
152
153 const createSampleProcess = ({ containerRequestService }: ServiceRepository) => {
154     containerRequestService.create({
155         ownerUuid: 'c97qk-j7d0g-s3ngc1z0748hsmf',
156         name: 'Simple process 7',
157         state: ContainerRequestState.COMMITTED,
158         mounts: {
159             '/var/spool/cwl': {
160                 kind: MountKind.COLLECTION,
161                 writable: true,
162             },
163             'stdout': {
164                 kind: MountKind.MOUNTED_FILE,
165                 path: '/var/spool/cwl/cwl.output.json'
166             },
167             '/var/lib/cwl/workflow.json': {
168                 kind: MountKind.JSON,
169                 content: {
170                     "cwlVersion": "v1.0",
171                     "$graph": [
172                         {
173                             "class": "CommandLineTool",
174                             "requirements": [
175                                 {
176                                     "listing": [
177                                         {
178                                             "entryname": "input_collector.log",
179                                             "entry": "$(inputs.single_file.basename)\n"
180                                         }
181                                     ],
182                                     "class": "InitialWorkDirRequirement"
183                                 }
184                             ],
185                             "inputs": [
186                                 {
187                                     "type": "File",
188                                     "id": "#input_collector.cwl/single_file"
189                                 }
190                             ],
191                             "outputs": [
192                                 {
193                                     "type": "File",
194                                     "outputBinding": {
195                                         "glob": "*"
196                                     },
197                                     "id": "#input_collector.cwl/output"
198                                 }
199                             ],
200                             "baseCommand": [
201                                 "echo"
202                             ],
203                             "id": "#input_collector.cwl"
204                         },
205                         {
206                             "class": "Workflow",
207                             "doc": "This is the description of the workflow",
208                             "inputs": [
209                                 {
210                                     "type": "File",
211                                     "label": "Single File",
212                                     "doc": "This should allow for single File selection only.",
213                                     "id": "#main/single_file"
214                                 }
215                             ],
216                             "outputs": [
217                                 {
218                                     "type": "File",
219                                     "outputSource": "#main/input_collector/output",
220                                     "id": "#main/log_file"
221                                 }
222                             ],
223                             "steps": [
224                                 {
225                                     "run": "#input_collector.cwl",
226                                     "in": [
227                                         {
228                                             "source": "#main/single_file",
229                                             "id": "#main/input_collector/single_file"
230                                         }
231                                     ],
232                                     "out": [
233                                         "#main/input_collector/output"
234                                     ],
235                                     "id": "#main/input_collector"
236                                 }
237                             ],
238                             "id": "#main"
239                         }
240                     ]
241                 },
242             },
243             '/var/lib/cwl/cwl.input.json': {
244                 kind: MountKind.JSON,
245                 content: {
246                     "single_file": {
247                         "class": "File",
248                         "location": "keep:233454526794c0a2d56a305baeff3d30+145/1.txt",
249                         "basename": "fileA"
250                     }
251                 },
252             }
253         },
254         runtimeConstraints: {
255             API: true,
256             vcpus: 1,
257             ram: 1073741824,
258         },
259         containerImage: 'arvados/jobs:1.1.4.20180618144723',
260         cwd: '/var/spool/cwl',
261         command: [
262             'arvados-cwl-runner',
263             '--local',
264             '--api=containers',
265             "--project-uuid=c97qk-j7d0g-s3ngc1z0748hsmf",
266             '/var/lib/cwl/workflow.json#main',
267             '/var/lib/cwl/cwl.input.json'
268         ],
269         outputPath: '/var/spool/cwl',
270         priority: 1,
271     });
272 };
273