frontend/src/pages/CodeEditor.jsx

204 lines
5.8 KiB
React
Raw Normal View History

2025-05-05 16:00:55 -07:00
import React, { useEffect, useState } from "react";
2025-05-08 10:39:54 -07:00
import { useLocation } from "react-router-dom";
2025-04-28 12:13:10 -07:00
import EditorPanel from "../components/EditorPanel";
2025-05-05 12:22:50 -07:00
import PreviewPanel from "../components/PreviewPanel";
2025-04-17 10:10:04 -07:00
2025-05-07 14:58:27 -07:00
const ASSIGNMENT_BASE = "http://localhost:8082";
2025-05-07 14:16:18 -07:00
export default function PageCodeEditor() {
2025-05-08 10:39:54 -07:00
const location = useLocation();
const qrCodeNumber = location.state?.qrCodeNumber;
console.log("QR Code Number:", qrCodeNumber);
2025-05-05 16:00:55 -07:00
const [appName, setAppName] = useState("");
const [code, setCode] = useState("# NOW LOADING");
2025-05-07 14:16:18 -07:00
const [isSaving, setIsSaving] = useState(false);
2025-05-08 00:36:38 -07:00
const [isDeploying, setIsDeploying] = useState(false);
2025-04-28 12:13:10 -07:00
2025-04-17 10:10:04 -07:00
useEffect(() => {
2025-05-02 09:29:18 -07:00
document.title = "Snake Brain Editor";
2025-05-05 16:00:55 -07:00
}, []);
2025-05-05 10:05:48 -07:00
2025-05-05 16:00:55 -07:00
useEffect(() => {
2025-05-07 14:16:18 -07:00
fetch(`${ASSIGNMENT_BASE}/student/assignment/${qrCodeNumber}`)
2025-05-05 16:00:55 -07:00
.then((res) => {
if (!res.ok) throw new Error("Failed to fetch assignment");
return res.json();
})
.then((data) => setAppName(data.appname))
.catch((err) => console.error("Assignment fetch error:", err));
}, [qrCodeNumber]);
2025-05-05 16:00:55 -07:00
useEffect(() => {
if (!appName) return;
2025-05-07 14:16:18 -07:00
fetch(`${ASSIGNMENT_BASE}/notebook/${appName}`)
2025-05-05 10:05:48 -07:00
.then((res) => {
2025-05-05 16:00:55 -07:00
if (!res.ok) throw new Error("Failed to fetch notebook");
2025-05-05 10:05:48 -07:00
return res.json();
})
.then((notebook) => {
const combined = notebook.cells
.filter((cell) => cell.cell_type === "code")
.map((cell) => cell.source.join(""))
.join("\n\n");
setCode(combined);
})
2025-05-05 16:00:55 -07:00
.catch((err) => console.error("Notebook fetch error:", err));
}, [appName]);
2025-04-17 10:10:04 -07:00
2025-05-07 14:16:18 -07:00
const handleSave = async () => {
if (isSaving) return;
setIsSaving(true);
try {
const res = await fetch(`${ASSIGNMENT_BASE}/student/save`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ appName, code })
});
if (!res.ok) throw new Error("Save failed");
alert("Notebook saved");
} catch (err) {
console.error("Save error:", err);
alert(`Save error: ${err.message}`);
} finally {
setIsSaving(false);
}
};
2025-05-08 00:36:38 -07:00
const handleDeploy = async () => {
if (isDeploying) return;
setIsDeploying(true);
try {
const res = await fetch(`${ASSIGNMENT_BASE}/student/restart`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ appName })
});
if (!res.ok) throw new Error("Restart failed");
alert("App restarted");
} catch (err) {
console.error("Restart error:", err);
alert(`Restart error: ${err.message}`);
} finally {
setIsDeploying(false);
}
};
2025-05-02 09:29:18 -07:00
return (
2025-05-08 10:34:43 -07:00
<main className="code-editor-page" style={{ paddingTop: "35px", width: "100vw" }}>
2025-05-02 09:29:18 -07:00
<div
className="editor-page-layout"
style={{
2025-05-08 10:34:43 -07:00
display: "grid",
gridTemplateColumns: '1fr 0.8fr',
//gap: "1rem",
width: "100%",
2025-05-05 16:00:55 -07:00
padding: "1rem",
2025-05-07 14:16:18 -07:00
fontFamily: "'Fira Code', 'Courier New', monospace"
2025-05-02 09:29:18 -07:00
}}
>
2025-05-05 16:00:55 -07:00
{/* Python Editor */}
2025-05-02 09:29:18 -07:00
<div
className="box-panel"
style={{
flex: 2,
2025-05-05 10:05:48 -07:00
background: "linear-gradient(145deg, #0d0221, #1a1a1a)",
2025-05-05 16:00:55 -07:00
borderRadius: "10px",
2025-05-05 10:05:48 -07:00
padding: "1rem",
color: "#eee",
2025-05-08 13:47:30 -07:00
height: "100%",
overflow: "auto",
2025-05-02 09:29:18 -07:00
}}
>
<h3
className="panel-heading"
style={{
2025-05-05 10:05:48 -07:00
fontSize: "1.2rem",
color: "#05d9e8",
textShadow: "0 0 5px #05d9e8",
2025-05-07 14:16:18 -07:00
marginBottom: "1rem"
2025-05-02 09:29:18 -07:00
}}
>
🐍 Snake Brain (Python)
</h3>
<EditorPanel code={code} onChange={setCode} />
2025-05-08 13:47:30 -07:00
<div style={{ marginTop: "1rem", display: "flex", flexDirection: "row", justifyContent: "center" }}>
2025-05-05 16:00:55 -07:00
<button
2025-05-07 14:16:18 -07:00
onClick={handleSave}
disabled={isSaving}
2025-05-05 16:00:55 -07:00
style={{
backgroundColor: "#ff2a6d",
color: "#fff",
padding: "0.5rem 2rem",
border: "none",
borderRadius: "20px",
fontWeight: "bold",
2025-05-07 14:16:18 -07:00
cursor: isSaving ? "not-allowed" : "pointer"
2025-05-05 16:00:55 -07:00
}}
>
2025-05-07 14:16:18 -07:00
{isSaving ? "Saving..." : "Save"}
2025-05-05 16:00:55 -07:00
</button>
2025-05-08 00:36:38 -07:00
2025-05-05 16:00:55 -07:00
<button
2025-05-08 00:36:38 -07:00
onClick={handleDeploy}
disabled={isDeploying}
2025-05-05 16:00:55 -07:00
style={{
2025-05-07 14:16:18 -07:00
marginLeft: "1rem",
2025-05-05 16:00:55 -07:00
backgroundColor: "#ff2a6d",
color: "#fff",
padding: "0.5rem 2rem",
border: "none",
borderRadius: "20px",
fontWeight: "bold",
2025-05-08 00:36:38 -07:00
cursor: isDeploying ? "not-allowed" : "pointer"
2025-05-05 16:00:55 -07:00
}}
>
2025-05-08 00:36:38 -07:00
{isDeploying ? "Deploying..." : "Deploy"}
2025-05-05 16:00:55 -07:00
</button>
</div>
2025-05-02 09:29:18 -07:00
</div>
2025-04-17 10:10:04 -07:00
2025-05-05 16:00:55 -07:00
{/* Live Arena */}
2025-05-02 09:29:18 -07:00
<div
className="box-panel"
style={{
flex: 1,
2025-05-05 10:05:48 -07:00
background: "#1a1a1a",
borderRadius: "12px",
2025-05-08 10:34:43 -07:00
padding: "0.8rem",
2025-05-05 10:05:48 -07:00
color: "#eee",
2025-05-08 10:34:43 -07:00
minHeight: "20vh"
2025-05-02 09:29:18 -07:00
}}
>
<h3
style={{
2025-05-05 10:05:48 -07:00
fontSize: "1.2rem",
color: "#d300c5",
textShadow: "0 0 5px #d300c5",
2025-05-08 10:34:43 -07:00
marginBottom: "0.8rem",
2025-05-05 10:05:48 -07:00
display: "flex",
alignItems: "center",
2025-05-08 10:34:43 -07:00
gap: "0.8rem"
2025-05-02 09:29:18 -07:00
}}
>
🎯 Live Arena Output
</h3>
2025-05-05 16:00:55 -07:00
<h4
2025-05-02 09:29:18 -07:00
style={{
2025-05-05 16:00:55 -07:00
color: "#fff",
2025-05-05 10:05:48 -07:00
textAlign: "center",
2025-05-08 10:34:43 -07:00
marginBottom: "0.8rem"
2025-05-02 09:29:18 -07:00
}}
2025-04-17 10:10:04 -07:00
>
2025-05-05 16:00:55 -07:00
Battlesnake Preview
</h4>
<PreviewPanel code={code} />
2025-04-17 10:10:04 -07:00
</div>
</div>
</main>
);
2025-05-05 16:00:55 -07:00
}