github.com/minio/console@v1.4.1/web-app/src/screens/Console/Logs/logsSlice.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import { createSlice, PayloadAction } from "@reduxjs/toolkit";
    18  import { LogMessage } from "./types";
    19  import { DateTime } from "luxon";
    20  
    21  export interface LogState {
    22    logMessages: LogMessage[];
    23    logsStarted: boolean;
    24  }
    25  
    26  const initialState: LogState = {
    27    logMessages: [],
    28    logsStarted: false,
    29  };
    30  
    31  export const logsSlice = createSlice({
    32    name: "logs",
    33    initialState,
    34    reducers: {
    35      logMessageReceived: (state, action: PayloadAction<LogMessage>) => {
    36        let msgs = state.logMessages;
    37        const logTime = DateTime.fromFormat(
    38          action.payload.time.toString(),
    39          "HH:mm:ss z MM/dd/yyyy",
    40          {
    41            zone: "UTC",
    42          },
    43        ).toJSDate();
    44  
    45        if (
    46          msgs.length > 0 &&
    47          logTime.getFullYear() === 1 &&
    48          action.payload.ConsoleMsg !== ""
    49        ) {
    50          for (let m in msgs) {
    51            if (msgs[m].time.getFullYear() === 1) {
    52              msgs[m].ConsoleMsg =
    53                `${msgs[m].ConsoleMsg}\n${action.payload.ConsoleMsg}`;
    54            }
    55          }
    56        } else {
    57          msgs.push(action.payload);
    58        }
    59        state.logMessages = msgs;
    60      },
    61      logResetMessages: (state) => {
    62        state.logMessages = [];
    63      },
    64      setLogsStarted: (state, action: PayloadAction<boolean>) => {
    65        state.logsStarted = action.payload;
    66      },
    67    },
    68  });
    69  
    70  export const { logMessageReceived, logResetMessages, setLogsStarted } =
    71    logsSlice.actions;
    72  
    73  export default logsSlice.reducer;