frontend/src/pages/CodeEditor.jsx

155 lines
4.3 KiB
React
Raw Normal View History

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-05 16:00:55 -07:00
export default function PageCodeEditor() {
const { qrCodeNumber: routeId } = useParams();
// console.log("Assignment ID:", assignmentId);
const qrCodeNumber = routeId || "2256";
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-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(() => {
fetch(`https://assignment-service.fly.dev/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-05 10:05:48 -07:00
fetch(`https://assignment-service.fly.dev/notebook/${appName}`)
.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-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-02 09:29:18 -07:00
fontFamily: "'Fira Code', 'Courier New', monospace",
}}
>
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",
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",
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
style={{
backgroundColor: "#ff2a6d",
color: "#fff",
padding: "0.5rem 2rem",
border: "none",
borderRadius: "20px",
fontWeight: "bold",
cursor: "pointer",
}}
>
Save
</button>
<button
style={{
backgroundColor: "#ff2a6d",
color: "#fff",
padding: "0.5rem 2rem",
marginLeft: "1rem",
border: "none",
borderRadius: "20px",
fontWeight: "bold",
cursor: "pointer",
}}
>
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",
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",
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-05 16:00:55 -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
}