Spaces:
Running
Running
File size: 14,017 Bytes
25fd1e9 3589760 bb06deb 656059f 25fd1e9 56922cb 25fd1e9 656059f 25fd1e9 3589760 25fd1e9 3589760 25fd1e9 3589760 25fd1e9 3589760 656059f 25fd1e9 3589760 25fd1e9 3589760 656059f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | import { useState, useEffect, useRef } from "react";
import Editor from "@monaco-editor/react";
import { askAgent } from "./agent/assistant";
import { runCode } from "./agent/runner";
import "./App.css";
// Supported languages in the IDE
const LANGUAGE_OPTIONS = [
{ id: "python", label: "Python", ext: ".py", icon: "π", monaco: "python" },
{ id: "javascript", label: "JavaScript", ext: ".js", icon: "π¨", monaco: "javascript" },
{ id: "typescript", label: "TypeScript", ext: ".ts", icon: "π¦", monaco: "typescript" },
{ id: "cpp", label: "C++", ext: ".cpp", icon: "π ", monaco: "cpp" },
{ id: "c", label: "C", ext: ".c", icon: "π·", monaco: "c" },
{ id: "java", label: "Java", ext: ".java", icon: "β", monaco: "java" },
{ id: "html", label: "HTML", ext: ".html", icon: "π", monaco: "html" },
{ id: "css", label: "CSS", ext: ".css", icon: "π¨", monaco: "css" },
{ id: "json", label: "JSON", ext: ".json", icon: "π§Ύ", monaco: "json" },
];
// Languages we actually can run on the backend right now
const RUNNABLE_LANGS = ["python", "javascript","java"];
// LocalStorage key
const STORAGE_KEY = "devmate_ide_files";
function App() {
const [files, setFiles] = useState({
"main.py": {
language: "python",
content: "# Python\nprint('Hello from IDE')",
},
"script.js": {
language: "javascript",
content: "console.log('Hello from JS');",
},
});
const [activeFile, setActiveFile] = useState("main.py");
const [output, setOutput] = useState("");
const [prompt, setPrompt] = useState("");
const [explanation, setExplanation] = useState("");
const [theme, setTheme] = useState("vs-dark"); // "vs-dark" | "light"
const [openMenu, setOpenMenu] = useState(null); // "file" | "run" | "ai" | null
const editorRef = useRef(null);
const currentFile = files[activeFile];
const currentLangId = currentFile?.language || "python";
const langMeta =
LANGUAGE_OPTIONS.find((l) => l.id === currentLangId) || LANGUAGE_OPTIONS[0];
// ----- LocalStorage: load on mount -----
useEffect(() => {
try {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
const parsed = JSON.parse(saved);
if (parsed && typeof parsed === "object") {
setFiles(parsed.files || files);
setActiveFile(parsed.activeFile || Object.keys(parsed.files || files)[0]);
}
}
} catch (e) {
console.warn("Failed to load saved files:", e);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// ----- LocalStorage: save whenever files/activeFile change -----
useEffect(() => {
try {
localStorage.setItem(
STORAGE_KEY,
JSON.stringify({ files, activeFile })
);
} catch (e) {
console.warn("Failed to save files:", e);
}
}, [files, activeFile]);
const updateActiveFileContent = (value) => {
if (!activeFile) return;
setFiles((prev) => ({
...prev,
[activeFile]: {
...prev[activeFile],
content: value ?? "",
},
}));
};
const handleRun = async () => {
if (!currentFile) return;
if (!RUNNABLE_LANGS.includes(currentLangId)) {
setOutput(
`β οΈ Run is only supported for: ${RUNNABLE_LANGS.join(
", "
)}.\nSelected language: ${currentLangId}`
);
return;
}
try {
const res = await runCode(currentFile.content, currentLangId);
setOutput(res.output ?? "");
} catch (err) {
setOutput(`Error running code: ${String(err)}`);
}
setOpenMenu(null);
};
const handleAskFix = async () => {
if (!currentFile) return;
const fullPrompt = `
You are an AI coding assistant inside an online IDE.
Improve, debug, or refactor the following ${currentLangId} code.
Return ONLY the updated code, with no explanation.
User request/hint (optional):
${prompt || "(no extra hint)"}
Code:
${currentFile.content}
`.trim();
const reply = await askAgent(fullPrompt, [
{ role: "user", content: currentFile.content },
]);
updateActiveFileContent(reply);
setOpenMenu(null);
};
const handleExplainSelection = async () => {
if (!currentFile) return;
const editor = editorRef.current;
let selectedCode = "";
if (editor) {
const model = editor.getModel();
const selection = editor.getSelection();
if (model && selection) {
selectedCode = model.getValueInRange(selection);
}
}
if (!selectedCode.trim()) {
selectedCode = currentFile.content;
}
const explainPrompt = `
You are an AI code explainer inside an IDE.
Explain clearly and concisely what the following ${currentLangId} code does,
including edge cases and any potential issues.
Code:
${selectedCode}
`.trim();
const reply = await askAgent(explainPrompt, [
{ role: "user", content: selectedCode },
]);
setExplanation(reply);
setOpenMenu(null);
};
const handleNewFile = () => {
const base = "untitled";
let idx = 1;
let name = `${base}${idx}${langMeta.ext}`;
while (files[name]) {
idx += 1;
name = `${base}${idx}${langMeta.ext}`;
}
setFiles((prev) => ({
...prev,
[name]: {
language: currentLangId,
content: `// ${langMeta.label} file\n`,
},
}));
setActiveFile(name);
setOpenMenu(null);
};
const handleDeleteFile = () => {
if (!activeFile) return;
if (Object.keys(files).length === 1) return; // keep at least one
const newFiles = { ...files };
delete newFiles[activeFile];
const remaining = Object.keys(newFiles);
setFiles(newFiles);
setActiveFile(remaining[0] || "");
};
const handleChangeLanguage = (langId) => {
setFiles((prev) => ({
...prev,
[activeFile]: {
...prev[activeFile],
language: langId,
},
}));
};
const toggleTheme = () => {
setTheme((prev) => (prev === "vs-dark" ? "light" : "vs-dark"));
};
const toggleMenu = (menuName) => {
setOpenMenu((prev) => (prev === menuName ? null : menuName));
};
const handleSaveLocal = () => {
// Already persisted automatically; this is more like a user-triggered "save" action
try {
localStorage.setItem(
STORAGE_KEY,
JSON.stringify({ files, activeFile })
);
setOutput("πΎ Saved project to browser storage.");
} catch (e) {
setOutput(`Failed to save: ${String(e)}`);
}
setOpenMenu(null);
};
const handleDownloadFile = () => {
if (!currentFile || !activeFile) return;
const blob = new Blob([currentFile.content], { type: "text/plain;charset=utf-8" });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = activeFile;
a.click();
window.URL.revokeObjectURL(url);
setOpenMenu(null);
};
return (
<div className={`ide-root ${theme === "vs-dark" ? "ide-dark" : "ide-light"}`}>
{/* Top Menu Bar */}
<div className="ide-menubar">
<div className="ide-menubar-left">
<span className="ide-logo">βοΈ DevMate IDE</span>
{/* File menu */}
<div className="ide-menu-wrapper">
<button
className="ide-menu-item"
onClick={() => toggleMenu("file")}
>
File βΎ
</button>
{openMenu === "file" && (
<div className="ide-menu-dropdown">
<button onClick={handleNewFile}>New File</button>
<button onClick={handleSaveLocal}>Save (Local)</button>
<button onClick={handleDownloadFile}>Download File</button>
</div>
)}
</div>
{/* Run menu */}
<div className="ide-menu-wrapper">
<button
className="ide-menu-item"
onClick={() => toggleMenu("run")}
>
Run βΎ
</button>
{openMenu === "run" && (
<div className="ide-menu-dropdown">
<button onClick={handleRun}>Run Current File</button>
</div>
)}
</div>
{/* AI menu */}
<div className="ide-menu-wrapper">
<button
className="ide-menu-item"
onClick={() => toggleMenu("ai")}
>
AI βΎ
</button>
{openMenu === "ai" && (
<div className="ide-menu-dropdown">
<button onClick={handleAskFix}>Fix Current File</button>
<button onClick={handleExplainSelection}>Explain Selection</button>
</div>
)}
</div>
<button className="ide-menu-item" onClick={() => setOutput("DevMate IDE - simple VS Code style IDE with AI agent and runner.")}>
Help
</button>
</div>
<div className="ide-menubar-right">
<span className="ide-menu-item" onClick={toggleTheme}>
{theme === "vs-dark" ? "βοΈ Light" : "π Dark"}
</span>
</div>
</div>
{/* Main body: sidebar + editor + panel */}
<div className="ide-body">
{/* Explorer Sidebar */}
<div className="ide-sidebar">
<div className="ide-sidebar-header">
<span>EXPLORER</span>
<button className="ide-icon-button" onClick={handleNewFile} title="New File">
οΌ
</button>
</div>
<div className="ide-file-list">
{Object.keys(files).map((fname) => {
const fLang = files[fname].language;
const fMeta =
LANGUAGE_OPTIONS.find((l) => l.id === fLang) ||
LANGUAGE_OPTIONS[0];
return (
<div
key={fname}
className={
"ide-file-item" +
(fname === activeFile ? " ide-file-item-active" : "")
}
onClick={() => setActiveFile(fname)}
>
<span className="ide-file-icon">{fMeta.icon}</span>
<span className="ide-file-name">{fname}</span>
</div>
);
})}
</div>
</div>
{/* Editor & bottom panel */}
<div className="ide-main">
{/* Tabs + quick actions */}
<div className="ide-tabs">
<div className="ide-tab-list">
{Object.keys(files).map((fname) => (
<div
key={fname}
className={
"ide-tab" + (fname === activeFile ? " ide-tab-active" : "")
}
onClick={() => setActiveFile(fname)}
>
<span>{fname}</span>
{fname === activeFile && (
<button
className="ide-tab-close"
onClick={(e) => {
e.stopPropagation();
handleDeleteFile();
}}
>
Γ
</button>
)}
</div>
))}
</div>
<div className="ide-tab-actions">
<select
value={currentLangId}
onChange={(e) => handleChangeLanguage(e.target.value)}
className="ide-select"
>
{LANGUAGE_OPTIONS.map((lang) => (
<option key={lang.id} value={lang.id}>
{lang.label}
</option>
))}
</select>
<button className="ide-button" onClick={handleRun}>
βΆ Run
</button>
<button className="ide-button" onClick={handleAskFix}>
π€ Fix with AI
</button>
<button className="ide-button" onClick={handleExplainSelection}>
π Explain Selection
</button>
</div>
</div>
{/* Editor */}
<div className="ide-editor-wrapper">
<Editor
height="100%"
theme={theme}
language={langMeta.monaco}
value={currentFile?.content}
onChange={updateActiveFileContent}
onMount={(editor) => {
editorRef.current = editor;
}}
options={{
minimap: { enabled: true },
fontSize: 14,
scrollBeyondLastLine: false,
}}
/>
</div>
{/* Bottom Panel: Output + Agent Prompt + Explanation */}
<div className="ide-bottom-panel">
<div className="ide-output-panel">
<div className="ide-panel-header">TERMINAL / OUTPUT</div>
<pre className="ide-output-content">{output}</pre>
</div>
<div className="ide-agent-panel">
<div className="ide-panel-header">AI PROMPT</div>
<textarea
className="ide-agent-textarea"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Ask AI to refactor, explain, or add features..."
/>
{explanation && (
<div className="ide-explanation">
<div className="ide-panel-header">AI EXPLANATION</div>
<pre className="ide-output-content">{explanation}</pre>
</div>
)}
</div>
</div>
</div>
</div>
{/* Status Bar */}
<div className="ide-statusbar">
<span>{activeFile}</span>
<span>{langMeta.label}</span>
<span>Theme: {theme === "vs-dark" ? "Dark" : "Light"}</span>
</div>
</div>
);
}
export default App;
|