-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathEditorComponent.js
More file actions
223 lines (211 loc) · 6.3 KB
/
EditorComponent.js
File metadata and controls
223 lines (211 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// EditorComponent.js
import React, { useState, useRef, useEffect } from "react";
import { FaPlay, FaSun, FaMoon } from "react-icons/fa"; // Import icons
import Editor from "@monaco-editor/react";
import "../components/css/EditorComponent.css";
import "@fortawesome/fontawesome-free/css/all.css";
import { useSnackbar } from "notistack";
import { Button, CircularProgress, styled, IconButton } from "@mui/material";
import Stars from "../components/js/Stars";
import {
LANGUAGES,
judge0SubmitUrl,
rapidApiHost,
rapidApiKey,
} from "../constants/constants";
import LanguageSelect from "../components/js/LanguageSelect";
const StyledButton = styled(Button)({
display: "flex",
alignItems: "center",
justifyContent: "center",
gap: "0.5rem",
});
function EditorComponent({ darkMode, toggleTheme }) {
const [code, setCode] = useState(null);
const [output, setOutput] = useState([]);
const [currentLanguage, setCurrentLanguage] = useState(
LANGUAGES[0].DEFAULT_LANGUAGE,
);
const [languageDetails, setLanguageDetails] = useState(LANGUAGES[0]);
const [loading, setLoading] = useState(false);
const { enqueueSnackbar } = useSnackbar();
const editorRef = useRef(null);
useEffect(() => {
const selectedLanguage = LANGUAGES.find(
(lang) => lang.DEFAULT_LANGUAGE === currentLanguage,
);
setLanguageDetails({
LANGUAGE_ID: selectedLanguage.ID,
LANGUAGE_NAME: selectedLanguage.NAME,
DEFAULT_LANGUAGE: selectedLanguage.DEFAULT_LANGUAGE,
NAME: selectedLanguage.NAME,
});
setCode(selectedLanguage.HELLO_WORLD);
}, [currentLanguage]);
function handleEditorDidMount(editor, monaco) {
editorRef.current = editor;
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => {
submitCode();
});
}
const getLanguageLogoById = (id) => {
const language = LANGUAGES.find(
(lang) => parseInt(lang.ID) === parseInt(id),
);
return language ? language.LOGO : null;
};
async function submitCode() {
const codeToSubmit = editorRef.current.getValue();
if (codeToSubmit === "") {
enqueueSnackbar("Please enter valid code", { variant: "error" });
return;
}
setLoading(true);
try {
const response = await fetch(judge0SubmitUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-RapidAPI-Key": rapidApiKey,
"X-RapidAPI-Host": rapidApiHost,
},
body: JSON.stringify({
source_code: codeToSubmit,
language_id: languageDetails.LANGUAGE_ID,
stdin: "",
expected_output: "",
}),
});
if (!response.ok) {
enqueueSnackbar(
`Failed to create submission. Status code: ${response.status}`,
{ variant: "error" },
);
setLoading(false);
return;
}
const data = await response.json();
const submissionId = data["token"];
setTimeout(() => {
fetch(`${judge0SubmitUrl}/${submissionId}`, {
method: "GET",
headers: {
"X-RapidAPI-Key": rapidApiKey,
"X-RapidAPI-Host": rapidApiHost,
},
})
.then((response) => response.json())
.then((data) => {
if (!data.stdout) {
enqueueSnackbar("Please check the code", { variant: "error" });
setOutput(data.message);
return;
}
const formatedData = data.stdout.split("\n");
setOutput(formatedData);
})
.catch((error) => {
enqueueSnackbar("Error retrieving output: " + error.message, {
variant: "error",
});
})
.finally(() => setLoading(false));
}, 2000);
} catch (error) {
enqueueSnackbar("Error: " + error.message, { variant: "error" });
}
}
function handleLanguageChange(_, value) {
setCurrentLanguage(value.DEFAULT_LANGUAGE);
setOutput("");
setCode(null);
}
return (
<div className="editor-container">
<div
style={{
height: "auto",
margin: "0.5rem",
paddingLeft: "0.5rem",
paddingRight: "0.5rem",
border: "3px solid rgba(0, 0, 0, 0.096)",
borderRadius: "1rem",
}}
>
<div style={styles.flex}>
{getLanguageLogoById(languageDetails.LANGUAGE_ID)}
<div style={{ fontWeight: "bold" }}>
{languageDetails.LANGUAGE_NAME}
</div>
<Stars />
<IconButton onClick={toggleTheme} color="inherit">
{darkMode ? <FaSun size={20} /> : <FaMoon size={20} />}
</IconButton>
</div>
</div>
<div className="layout">
<Editor
className="editor"
theme={darkMode ? "vs-dark" : "light"}
onMount={handleEditorDidMount}
value={code}
onChange={setCode}
language={languageDetails.DEFAULT_LANGUAGE}
/>
<div className="sidebar">
<div style={styles.languageDropdown}>
<LanguageSelect
handleLanguageChange={handleLanguageChange}
defaultLanguage={languageDetails}
darkMode={darkMode}
/>
</div>
<StyledButton
onClick={submitCode}
style={styles.button}
variant="contained"
color="primary"
disabled={loading}
>
<span>
{loading ? <CircularProgress size={16} /> : <FaPlay size="16" />}
</span>
Run {languageDetails.LANGUAGE_NAME}
</StyledButton>
</div>
</div>
<div className="output">
{output &&
output.map((result, i) => {
return <div key={i}>{result}</div>;
})}
</div>
</div>
);
}
const styles = {
flex: {
display: "flex",
justifyContent: "space-between",
alignItems: "center",
gap: "0.6em",
},
button: {
marginLeft: "5px",
marginTop: "5px",
padding: "10px 20px",
backgroundColor: "#252525",
color: "#fff",
border: "none",
borderRadius: "5px",
fontSize: "0.8em",
cursor: "pointer",
boxShadow: "0 4px 6px rgba(0, 0, 0, 0.1)",
},
languageDropdown: {
marginTop: "2rem",
display: "flex",
alignItems: "center",
},
};
export default EditorComponent;