Skip to content

Commit 13a60e9

Browse files
author
Incharajayaram
committed
feat: Add stdin support, improve error reporting and UI
1 parent eaf0436 commit 13a60e9

File tree

1 file changed

+102
-51
lines changed

1 file changed

+102
-51
lines changed

src/pages/EditorComponent.js

Lines changed: 102 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,41 @@ const OutputLayout = styled("div")(({ theme }) => ({
6565
"@media (min-width: 1024px)": {
6666
height: "30vh",
6767
padding: "1rem",
68+
width: "50%",
69+
},
70+
}));
71+
72+
const InputLayout = styled("div")(({ theme }) => ({
73+
backgroundColor: theme.palette.background.paper,
74+
height: "20vh",
75+
margin: "1rem 0",
76+
overflow: "auto",
77+
border: `2px solid ${theme.palette.divider}`,
78+
borderRadius: "1rem",
79+
padding: "1rem",
80+
"@media (min-width: 1024px)": {
81+
height: "30vh",
82+
padding: "1rem",
83+
width: "50%",
84+
margin: "1rem 0 0 1rem",
85+
},
86+
}));
87+
88+
const StyledTextArea = styled("textarea")(({ theme }) => ({
89+
width: "100%",
90+
height: "calc(100% - 40px)",
91+
background: "transparent",
92+
color: theme.palette.text.primary,
93+
border: "none",
94+
resize: "none",
95+
outline: "none",
96+
fontSize: "1rem",
97+
"&::placeholder": {
98+
color: theme.palette.text.secondary,
99+
opacity: 0.8,
100+
},
101+
"&:focus::placeholder": {
102+
color: "transparent",
68103
},
69104
}));
70105

@@ -79,6 +114,7 @@ const decodeFormat = (data) => {
79114

80115
function EditorComponent() {
81116
const [code, setCode] = useState(null);
117+
const [stdin, setStdin] = useState("");
82118
const [output, setOutput] = useState([]);
83119
const [currentLanguage, setCurrentLanguage] = useState(
84120
LANGUAGES[0].DEFAULT_LANGUAGE
@@ -189,7 +225,7 @@ function EditorComponent() {
189225
body: JSON.stringify({
190226
source_code: encodedCode,
191227
language_id: languageDetails.ID,
192-
stdin: "",
228+
stdin: btoa(stdin),
193229
expected_output: "",
194230
}),
195231
}
@@ -219,16 +255,19 @@ function EditorComponent() {
219255
)
220256
.then((response) => response.json())
221257
.then((data) => {
222-
if (!data.stdout) {
223-
enqueueSnackbar("Please check the code", { variant: "error" });
224-
if (data.stderr) {
225-
setOutput(decodeFormat(data.stderr));
226-
} else if (data.compile_output) {
227-
setOutput(decodeFormat(data.compile_output));
228-
}
229-
return;
258+
const newOutput = [];
259+
if (data.stdout) {
260+
newOutput.push(...decodeFormat(data.stdout));
230261
}
231-
setOutput(decodeFormat(data.stdout));
262+
if (data.stderr) {
263+
newOutput.push(...decodeFormat(data.stderr));
264+
enqueueSnackbar("Error in code", { variant: "error" });
265+
}
266+
if (data.compile_output) {
267+
newOutput.push(...decodeFormat(data.compile_output));
268+
enqueueSnackbar("Compilation error", { variant: "error" });
269+
}
270+
setOutput(newOutput);
232271
})
233272
.catch((error) => {
234273
enqueueSnackbar("Error retrieving output: " + error.message, {
@@ -240,7 +279,7 @@ function EditorComponent() {
240279
} catch (error) {
241280
enqueueSnackbar("Error: " + error.message, { variant: "error" });
242281
}
243-
}, [enqueueSnackbar, languageDetails]);
282+
}, [enqueueSnackbar, languageDetails, stdin]);
244283

245284
// import file
246285
const [isImporting, setIsImporting] = React.useState(false);
@@ -662,49 +701,61 @@ function EditorComponent() {
662701
</StyledButton>
663702
</div>
664703
</StyledLayout>
665-
<OutputLayout>
666-
<div className="output-header">
667-
<Typography
668-
variant="h6"
669-
sx={{ fontSize: "1rem", fontWeight: "bold" }}
670-
>
671-
Output
672-
</Typography>
673-
<div className="output-controls">
674-
<Button
675-
size="small"
676-
onClick={copyOutput}
677-
startIcon={<FaCopy />}
678-
variant="outlined"
679-
sx={{ minWidth: "auto", padding: "4px 8px" }}
680-
>
681-
Copy
682-
</Button>
683-
<Button
684-
size="small"
685-
onClick={clearOutput}
686-
startIcon={<FaTrash />}
687-
variant="outlined"
688-
sx={{ minWidth: "auto", padding: "4px 8px", marginLeft: "0.5rem" }}
704+
<div style={{ display: "flex", flexDirection: "row" }}>
705+
<OutputLayout>
706+
<div className="output-header">
707+
<Typography
708+
variant="h6"
709+
sx={{ fontSize: "1rem", fontWeight: "bold" }}
689710
>
690-
Clear
691-
</Button>
711+
Output
712+
</Typography>
713+
<div className="output-controls">
714+
<Button
715+
size="small"
716+
onClick={copyOutput}
717+
startIcon={<FaCopy />}
718+
variant="outlined"
719+
sx={{ minWidth: "auto", padding: "4px 8px" }}
720+
>
721+
Copy
722+
</Button>
723+
<Button
724+
size="small"
725+
onClick={clearOutput}
726+
startIcon={<FaTrash />}
727+
variant="outlined"
728+
sx={{ minWidth: "auto", padding: "4px 8px", marginLeft: "0.5rem" }}
729+
>
730+
Clear
731+
</Button>
732+
</div>
692733
</div>
693-
</div>
694-
<div className="output-content">
695-
{Array.isArray(output) && output.length > 0 ? (
696-
output.map((result, i) => (
697-
<div key={i} className="output-line">
698-
{result}
734+
<div className="output-content">
735+
{Array.isArray(output) && output.length > 0 ? (
736+
output.map((result, i) => (
737+
<div key={i} className="output-line">
738+
{result}
739+
</div>
740+
))
741+
) : (
742+
<div className="output-empty">
743+
No output yet. Run your code to see results!
699744
</div>
700-
))
701-
) : (
702-
<div className="output-empty">
703-
No output yet. Run your code to see results!
704-
</div>
705-
)}
706-
</div>
707-
</OutputLayout>
745+
)}
746+
</div>
747+
</OutputLayout>
748+
<InputLayout>
749+
<Typography variant="h6" sx={{ fontSize: "1rem", fontWeight: "bold" }}>
750+
Input
751+
</Typography>
752+
<StyledTextArea
753+
value={stdin}
754+
onChange={(e) => setStdin(e.target.value)}
755+
placeholder="Provide all program input here before running"
756+
/>
757+
</InputLayout>
758+
</div>
708759
</>
709760
);
710761

0 commit comments

Comments
 (0)