Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 128 additions & 52 deletions src/pages/EditorComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,41 @@
"@media (min-width: 1024px)": {
height: "30vh",
padding: "1rem",
width: "50%",
},
}));

const InputLayout = styled("div")(({ theme }) => ({
backgroundColor: theme.palette.background.paper,
height: "20vh",
margin: "1rem 0",
overflow: "auto",
border: `2px solid ${theme.palette.divider}`,
borderRadius: "1rem",
padding: "1rem",
"@media (min-width: 1024px)": {
height: "30vh",
padding: "1rem",
width: "50%",
margin: "1rem 0 0 1rem",
},
}));

const StyledTextArea = styled("textarea")(({ theme }) => ({
width: "100%",
height: "calc(100% - 40px)",
background: "transparent",
color: theme.palette.text.primary,
border: "none",
resize: "none",
outline: "none",
fontSize: "1rem",
"&::placeholder": {
color: theme.palette.text.secondary,
opacity: 0.8,
},
"&:focus::placeholder": {
color: "transparent",
},
}));

Expand All @@ -79,6 +114,7 @@

function EditorComponent() {
const [code, setCode] = useState(null);
const [stdin, setStdin] = useState("");
const [output, setOutput] = useState([]);
const [currentLanguage, setCurrentLanguage] = useState(
LANGUAGES[0].DEFAULT_LANGUAGE
Expand Down Expand Up @@ -133,9 +169,28 @@
DEFAULT_LANGUAGE: selectedLanguage.DEFAULT_LANGUAGE,
NAME: selectedLanguage.NAME,
});
setCode(selectedLanguage.HELLO_WORLD);
const savedCode = localStorage.getItem(`code-${selectedLanguage.DEFAULT_LANGUAGE}`);
if (savedCode !== null) {
setCode(savedCode);
} else {
setCode(selectedLanguage.HELLO_WORLD);
}
}, [currentLanguage]);

useEffect(() => {
const handler = setTimeout(() => {
if (code) {
localStorage.setItem(`code-${currentLanguage}`, code);
} else {
localStorage.removeItem(`code-${currentLanguage}`);
}
}, 500);

return () => {
clearTimeout(handler);
};
}, [code]);

Check warning on line 192 in src/pages/EditorComponent.js

View workflow job for this annotation

GitHub Actions / Performs linting on the application

React Hook useEffect has a missing dependency: 'currentLanguage'. Either include it or remove the dependency array

const handleEditorThemeChange = async (_, theme) => {
if (["light", "vs-dark"].includes(theme.ID)) {
setCurrentEditorTheme(theme);
Expand Down Expand Up @@ -178,7 +233,7 @@
body: JSON.stringify({
source_code: encodedCode,
language_id: languageDetails.ID,
stdin: "",
stdin: btoa(stdin),
expected_output: "",
}),
}
Expand Down Expand Up @@ -208,16 +263,25 @@
)
.then((response) => response.json())
.then((data) => {
if (!data.stdout) {
enqueueSnackbar("Please check the code", { variant: "error" });
if (data.stderr) {
setOutput(decodeFormat(data.stderr));
} else if (data.compile_output) {
setOutput(decodeFormat(data.compile_output));
}
return;
const newOutput = [];
let errorMessage = [];

if (data.stdout) {
newOutput.push(...decodeFormat(data.stdout));
}
setOutput(decodeFormat(data.stdout));
if (data.stderr) {
newOutput.push(...decodeFormat(data.stderr));
errorMessage.push("Error in code");
}
if (data.compile_output) {
newOutput.push(...decodeFormat(data.compile_output));
errorMessage.push("Compilation error");
}

if (errorMessage.length > 0) {
enqueueSnackbar(errorMessage.join(" and "), { variant: "error" });
}
setOutput(newOutput);
})
.catch((error) => {
enqueueSnackbar("Error retrieving output: " + error.message, {
Expand All @@ -229,7 +293,7 @@
} catch (error) {
enqueueSnackbar("Error: " + error.message, { variant: "error" });
}
}, [enqueueSnackbar, languageDetails]);
}, [enqueueSnackbar, languageDetails, stdin]);

// import file
const [isImporting, setIsImporting] = React.useState(false);
Expand Down Expand Up @@ -651,49 +715,61 @@
</StyledButton>
</div>
</StyledLayout>
<OutputLayout>
<div className="output-header">
<Typography
variant="h6"
sx={{ fontSize: "1rem", fontWeight: "bold" }}
>
Output
</Typography>
<div className="output-controls">
<Button
size="small"
onClick={copyOutput}
startIcon={<FaCopy />}
variant="outlined"
sx={{ minWidth: "auto", padding: "4px 8px" }}
>
Copy
</Button>
<Button
size="small"
onClick={clearOutput}
startIcon={<FaTrash />}
variant="outlined"
sx={{ minWidth: "auto", padding: "4px 8px", marginLeft: "0.5rem" }}
<div style={{ display: "flex", flexDirection: "row" }}>
<OutputLayout>
<div className="output-header">
<Typography
variant="h6"
sx={{ fontSize: "1rem", fontWeight: "bold" }}
>
Clear
</Button>
Output
</Typography>
<div className="output-controls">
<Button
size="small"
onClick={copyOutput}
startIcon={<FaCopy />}
variant="outlined"
sx={{ minWidth: "auto", padding: "4px 8px" }}
>
Copy
</Button>
<Button
size="small"
onClick={clearOutput}
startIcon={<FaTrash />}
variant="outlined"
sx={{ minWidth: "auto", padding: "4px 8px", marginLeft: "0.5rem" }}
>
Clear
</Button>
</div>
</div>
</div>
<div className="output-content">
{Array.isArray(output) && output.length > 0 ? (
output.map((result, i) => (
<div key={i} className="output-line">
{result}
<div className="output-content">
{Array.isArray(output) && output.length > 0 ? (
output.map((result, i) => (
<div key={i} className="output-line">
{result}
</div>
))
) : (
<div className="output-empty">
No output yet. Run your code to see results!
</div>
))
) : (
<div className="output-empty">
No output yet. Run your code to see results!
</div>
)}
</div>
</OutputLayout>
)}
</div>
</OutputLayout>
<InputLayout>
<Typography variant="h6" sx={{ fontSize: "1rem", fontWeight: "bold" }}>
Input
</Typography>
<StyledTextArea
value={stdin}
onChange={(e) => setStdin(e.target.value)}
placeholder="Provide all program input here before running"
/>
</InputLayout>
</div>
</>
);

Expand Down