• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { createAsyncThunk } from '@reduxjs/toolkit';
17import {
18    setCompileErrLogs,
19    setCompileOutLogs, setDisasmErrLogs, setDisasmOutLogs,
20    setErrLogs,
21    setOutLogs,
22    setRunErrLogs,
23    setRunOutLogs
24} from '../store/slices/logs';
25import { RootState } from '../store';
26
27export enum ELogType {
28    COMPILE_OUT = 'compileOut',
29    COMPILE_ERR = 'compileErr',
30    RUN_OUT = 'runOut',
31    RUN_ERR = 'runErr',
32    DISASM_OUT = 'disasmOut',
33    DISASM_ERR = 'disasmErr',
34}
35
36export interface ILog {
37    message: string;
38    isRead?: boolean;
39    from?: ELogType;
40}
41
42export interface ICompileData {
43    exit_code: number;
44    output?: string;
45    error?: string;
46}
47
48export interface IRunData {
49    exit_code: number;
50    output?: string;
51    error?: string;
52}
53
54export interface IDisassemblyData {
55    exit_code: number;
56    output?: string;
57    error?: string;
58}
59
60export interface IApiResponse {
61    data: {
62        compile?: ICompileData;
63        run?: IRunData;
64        disassembly?: IDisassemblyData;
65    };
66}
67
68export const handleResponseLogs = createAsyncThunk(
69    'logs/compileLogs',
70    async (response: IApiResponse, thunkAPI) => {
71        const state: RootState = thunkAPI.getState() as RootState;
72        const logsState = state.logs;
73
74        const handleLog = (
75            data: ICompileData | IRunData | IDisassemblyData | undefined,
76            logTypeOut: ELogType,
77            logTypeErr: ELogType,
78            successMessage: string,
79            setOutAction: (logs: ILog[]) => { payload: ILog[]; type: string },
80            setErrAction: (logs: ILog[]) => { payload: ILog[]; type: string }
81        ): void => {
82            if (!data) {
83                return;
84            }
85            if (data.exit_code === 0 && data.output) {
86                thunkAPI.dispatch(setOutAction(
87                    logsState.out.concat({
88                        message: data.output,
89                        isRead: false,
90                        from: logTypeOut
91                    })
92                ));
93                thunkAPI.dispatch(setOutLogs(
94                    logsState.out.concat({
95                        message: data.output,
96                        isRead: false,
97                        from: logTypeOut
98                    })
99                ));
100            } else if (data.exit_code === 0) {
101                thunkAPI.dispatch(setOutAction(
102                    logsState.out.concat({
103                        message: successMessage,
104                        isRead: false,
105                        from: logTypeOut
106                    })
107                ));
108                thunkAPI.dispatch(setOutLogs(
109                    logsState.out.concat({
110                        message: successMessage,
111                        isRead: false,
112                        from: logTypeOut
113                    })
114                ));
115            } else if (data.exit_code !== 0 && data.error) {
116                thunkAPI.dispatch(setErrAction(
117                    logsState.err.concat({
118                        message: data.error,
119                        isRead: false,
120                        from: logTypeErr
121                    })
122                ));
123                thunkAPI.dispatch(setErrLogs(
124                    logsState.err.concat({
125                        message: data.error,
126                        isRead: false,
127                        from: logTypeErr
128                    })
129                ));
130            } else if (data.exit_code !== 0 && data.output) {
131                thunkAPI.dispatch(setErrAction(
132                    logsState.err.concat({
133                        message: data.output,
134                        isRead: false,
135                        from: logTypeErr
136                    })
137                ));
138                thunkAPI.dispatch(setErrLogs(
139                    logsState.err.concat({
140                        message: data.output,
141                        isRead: false,
142                        from: logTypeErr
143                    })
144                ));
145            }
146        };
147
148        handleLog(
149            response.data.compile,
150            ELogType.COMPILE_OUT,
151            ELogType.COMPILE_ERR,
152            'Compile successful!',
153            setCompileOutLogs,
154            setCompileErrLogs
155        );
156
157        handleLog(
158            response.data.run,
159            ELogType.RUN_OUT,
160            ELogType.RUN_ERR,
161            'Run successful!',
162            setRunOutLogs,
163            setRunErrLogs
164        );
165
166        handleLog(
167            response.data.disassembly,
168            ELogType.DISASM_OUT,
169            ELogType.DISASM_ERR,
170            'Disassembly successful!',
171            setDisasmOutLogs,
172            setDisasmErrLogs
173        );
174    }
175);
176