2025-05-05 16:00:55 -07:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
|
import { useParams } 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-05 19:22:38 -07:00
|
|
|
|
2025-05-07 14:16:18 -07:00
|
|
|
export default function PageCodeEditor() {
|
2025-05-05 19:22:38 -07:00
|
|
|
const { qrCodeNumber: routeId } = useParams();
|
2025-05-07 14:58:27 -07:00
|
|
|
const qrCodeNumber = routeId || "6656";
|
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-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));
|
2025-05-05 19:22:38 -07:00
|
|
|
}, [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-02 09:29:18 -07:00
|
|
|
return (
|
2025-05-05 16:00:55 -07:00
|
|
|
<main className="code-editor-page" style={{ paddingTop: "35px" }}>
|
2025-05-02 09:29:18 -07:00
|
|
|
<div
|
|
|
|
|
className="editor-page-layout"
|
|
|
|
|
style={{
|
2025-05-05 10:05:48 -07:00
|
|
|
display: "flex",
|
2025-05-05 16:00:55 -07:00
|
|
|
gap: "1rem",
|
|
|
|
|
width: "120rem",
|
|
|
|
|
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",
|
|
|
|
|
minHeight: "80vh",
|
2025-05-07 14:16:18 -07:00
|
|
|
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-05 16:00:55 -07:00
|
|
|
<div style={{ marginTop: "1rem", display: "flex" }}>
|
|
|
|
|
<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>
|
|
|
|
|
<button
|
|
|
|
|
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-07 14:16:18 -07:00
|
|
|
cursor: "pointer"
|
2025-05-05 16:00:55 -07:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Deploy
|
|
|
|
|
</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",
|
|
|
|
|
padding: "1rem",
|
|
|
|
|
color: "#eee",
|
2025-05-07 14:16:18 -07:00
|
|
|
minHeight: "80vh"
|
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",
|
|
|
|
|
marginBottom: "1rem",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
2025-05-07 14:16:18 -07:00
|
|
|
gap: "0.5rem"
|
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-07 14:16:18 -07:00
|
|
|
marginBottom: "1rem"
|
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
|
|
|
}
|