21811: Merge branch 'main' into 21811-side-favorites-test
[arvados.git] / services / workbench2 / src / websocket / websocket.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RootStore } from "store/store";
6 import { AuthService } from "services/auth-service/auth-service";
7 import { Config } from "common/config";
8 import { WebSocketService } from "./websocket-service";
9 import { ResourceEventMessage } from "./resource-event-message";
10 import { ResourceKind } from "models/resource";
11 import { loadProcess } from "store/processes/processes-actions";
12 import { getProcess, getSubprocesses } from "store/processes/process";
13 import { LogEventType } from "models/log";
14 import { subprocessPanelActions } from "store/subprocess-panel/subprocess-panel-actions";
15 import { projectPanelDataActions } from "store/project-panel/project-panel-action-bind";
16 import { getProjectPanelCurrentUuid } from "store/project-panel/project-panel-action";
17 import { allProcessesPanelActions } from "store/all-processes-panel/all-processes-panel-action";
18 import { loadCollection } from "store/workbench/workbench-actions";
19 import { matchAllProcessesRoute, matchProjectRoute, matchProcessRoute } from "routes/routes";
20
21 export const initWebSocket = (config: Config, authService: AuthService, store: RootStore) => {
22     if (config.websocketUrl) {
23         const webSocketService = new WebSocketService(config.websocketUrl, authService);
24         webSocketService.setMessageListener(messageListener(store));
25         webSocketService.connect();
26     } else {
27         console.warn("WARNING: Websocket ExternalURL is not set on the cluster config.");
28     }
29 };
30
31 const messageListener = (store: RootStore) => (message: ResourceEventMessage) => {
32     if (message.eventType === LogEventType.CREATE || message.eventType === LogEventType.UPDATE) {
33         const state = store.getState();
34         const location = state.router.location ? state.router.location.pathname : "";
35         switch (message.objectKind) {
36             case ResourceKind.COLLECTION:
37                 const currentCollection = state.collectionPanel.item;
38                 if (currentCollection && currentCollection.uuid === message.objectUuid) {
39                     store.dispatch(loadCollection(message.objectUuid));
40                 }
41                 return;
42             case ResourceKind.CONTAINER_REQUEST:
43                 if (matchProcessRoute(location)) {
44                     if (state.processPanel.containerRequestUuid === message.objectUuid) {
45                         store.dispatch(loadProcess(message.objectUuid));
46                     }
47                     const proc = getProcess(state.processPanel.containerRequestUuid)(state.resources);
48                     if (proc && proc.container && proc.container.uuid === message.properties["new_attributes"]["requesting_container_uuid"]) {
49                         store.dispatch(subprocessPanelActions.REQUEST_ITEMS(false, true));
50                         return;
51                     }
52                 }
53             // fall through, this will happen for container requests as well.
54             case ResourceKind.CONTAINER:
55                 if (matchProcessRoute(location)) {
56                     // refresh only if this is a subprocess of the currently displayed process.
57                     const subproc = getSubprocesses(state.processPanel.containerRequestUuid)(state.resources);
58                     for (const sb of subproc) {
59                         if (sb.containerRequest.uuid === message.objectUuid || (sb.container && sb.container.uuid === message.objectUuid)) {
60                             store.dispatch(subprocessPanelActions.REQUEST_ITEMS(false, true));
61                             break;
62                         }
63                     }
64                 }
65                 if (matchAllProcessesRoute(location)) {
66                     store.dispatch(allProcessesPanelActions.REQUEST_ITEMS(false, true));
67                 }
68                 if (matchProjectRoute(location) && message.objectOwnerUuid === getProjectPanelCurrentUuid(state)) {
69                     store.dispatch(projectPanelDataActions.REQUEST_ITEMS(false, true));
70                 }
71                 return;
72             default:
73                 return;
74         }
75     }
76 };