Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / process-logs-panel / process-logs-panel-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ProcessLogs, ProcessLogsPanel } from './process-logs-panel';
6 import { ProcessLogsPanelAction, processLogsPanelActions } from './process-logs-panel-actions';
7
8 const initialState: ProcessLogsPanel = {
9     filters: [],
10     selectedFilter: '',
11     logs: {},
12 };
13
14 export const processLogsPanelReducer = (state = initialState, action: ProcessLogsPanelAction): ProcessLogsPanel =>
15     processLogsPanelActions.match(action, {
16         RESET_PROCESS_LOGS_PANEL: () => initialState,
17         INIT_PROCESS_LOGS_PANEL: ({ filters, logs }) => ({
18             filters,
19             logs,
20             selectedFilter: filters[0] || '',
21         }),
22         SET_PROCESS_LOGS_PANEL_FILTER: selectedFilter => ({
23             ...state,
24             selectedFilter
25         }),
26         ADD_PROCESS_LOGS_PANEL_ITEM: (groupedLogs: ProcessLogs) => {
27             // Update filters
28             const newFilters = Object.keys(groupedLogs).filter((logType) => (!state.filters.includes(logType)));
29             const filters = [...state.filters, ...newFilters];
30
31             // Append new log lines
32             const logs = Object.keys(groupedLogs).reduce((acc, logType) => {
33                 if (Object.keys(acc).includes(logType)) {
34                     // If log type exists, append lines and update lastByte
35                     return {...acc, [logType]: {
36                         lastByte: groupedLogs[logType].lastByte,
37                         contents: [...acc[logType].contents, ...groupedLogs[logType].contents]
38                     }};
39                 } else {
40                     return {...acc, [logType]: groupedLogs[logType]};
41                 }
42             }, state.logs);
43
44             return { ...state, logs, filters };
45         },
46         default: () => state,
47     });