Spaces:
Running
Running
File size: 11,331 Bytes
25fd1e9 3589760 5cb8e2c ac3107a bb06deb 656059f ac3107a 25fd1e9 ac3107a 25fd1e9 ac3107a 25fd1e9 ac3107a 656059f ac3107a 3589760 25fd1e9 7323886 ac3107a 25fd1e9 ac3107a 25fd1e9 5cb8e2c 25fd1e9 ac3107a 25fd1e9 ac3107a 25fd1e9 ac3107a 25fd1e9 3589760 ac3107a 3589760 ac3107a 25fd1e9 ac3107a 25fd1e9 ac3107a 5cb8e2c ac3107a 25fd1e9 ac3107a 6243bd8 ac3107a 6243bd8 ac3107a 6243bd8 ac3107a 5cb8e2c e323945 5cb8e2c e323945 08f236e 5cb8e2c e323945 5cb8e2c e323945 5cb8e2c e323945 5cb8e2c 25fd1e9 ac3107a 25fd1e9 ac3107a 5cb8e2c ac3107a 25fd1e9 ac3107a 3589760 ac3107a 656059f 5cb8e2c ac3107a 25fd1e9 ac3107a 25fd1e9 ac3107a 5cb8e2c ac3107a 25fd1e9 ac3107a 25fd1e9 5cb8e2c 25fd1e9 ac3107a 25fd1e9 5cb8e2c 25fd1e9 5cb8e2c ac3107a 25fd1e9 ac3107a 5cb8e2c 25fd1e9 ac3107a 25fd1e9 ac3107a 25fd1e9 5cb8e2c 08f236e e323945 08f236e e323945 5cb8e2c 08f236e 5cb8e2c 08f236e 3589760 ac3107a 3589760 ac3107a 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 | import { useState, useEffect, useRef } from "react";
import Editor from "@monaco-editor/react";
import { askAgent } from "./agent/assistant";
import { runCode } from "./agent/runner";
import {
loadTree,
saveTree,
addFile,
addFolder,
renameNode,
deleteNode,
getNodeByPath,
updateFileContent,
searchTree,
} from "./fileStore";
import { downloadProjectZip } from "./zipExport";
import { parseProblems } from "./problemParser";
import "./App.css";
// =================== SUPPORTED LANGUAGES ===================
const LANGUAGE_OPTIONS = [
{ id: "python", ext: ".py", icon: "π", monaco: "python" },
{ id: "javascript", ext: ".js", icon: "π¨", monaco: "javascript" },
{ id: "typescript", ext: ".ts", icon: "π¦", monaco: "typescript" },
{ id: "cpp", ext: ".cpp", icon: "π ", monaco: "cpp" },
{ id: "c", ext: ".c", icon: "π·", monaco: "c" },
{ id: "java", ext: ".java", icon: "β", monaco: "java" },
{ id: "html", ext: ".html", icon: "π", monaco: "html" },
{ id: "css", ext: ".css", icon: "π¨", monaco: "css" },
{ id: "json", ext: ".json", icon: "π§Ύ", monaco: "json" },
];
const RUNNABLE_LANGS = ["python", "javascript", "java"];
// =================== APP ===================
function App() {
const [tree, setTree] = useState(loadTree());
const [activePath, setActivePath] = useState("main.py");
const [output, setOutput] = useState("");
const [prompt, setPrompt] = useState("");
const [explanation, setExplanation] = useState("");
const [stdin, setStdin] = useState("");
const [problems, setProblems] = useState([]);
const [theme, setTheme] = useState("vs-dark");
const [searchOpen, setSearchOpen] = useState(false);
const [searchQuery, setSearchQuery] = useState("");
const [aiSuggestions, setAiSuggestions] = useState([]);
const [contextMenu, setContextMenu] = useState(null); // right-click menu
const editorRef = useRef(null);
const currentFile = getNodeByPath(tree, activePath);
const langMeta =
LANGUAGE_OPTIONS.find((l) => currentFile?.name.endsWith(l.ext)) ||
LANGUAGE_OPTIONS[0];
// Save after tree change
useEffect(() => {
saveTree(tree);
}, [tree]);
// =================== FILE ACTIONS ===================
const handleNewFile = () => {
const name = window.prompt("Filename (with extension):");
if (!name) return;
setTree(addFile(tree, name));
};
const handleNewFolder = () => {
const name = window.prompt("Folder name:");
if (!name) return;
setTree(addFolder(tree, name));
};
const handleRename = () => {
if (!activePath) return;
const newName = window.prompt("New name:", currentFile.name);
if (!newName) return;
setTree(renameNode(tree, activePath, newName));
setActivePath(newName);
};
const handleDelete = () => {
if (!activePath) return;
setTree(deleteNode(tree, activePath));
setActivePath(""); // unselect
};
const downloadFile = () => {
if (!currentFile?.content) return;
const blob = new Blob([currentFile.content], {
type: "text/plain;charset=utf-8",
});
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = currentFile.name;
a.click();
};
// =================== RUN ===================
const handleRun = async () => {
if (!currentFile?.content) return;
const selectedLang = langMeta.id;
if (!RUNNABLE_LANGS.includes(selectedLang)) {
setOutput(`β οΈ Cannot run ${selectedLang}.`);
return;
}
const res = await runCode(currentFile.content, selectedLang, stdin);
setOutput(res.output || "");
setProblems(res.error ? parseProblems(res.output) : []);
};
// =================== AI ===================
const handleAskFix = async () => {
if (!currentFile) return;
const userHint = prompt.trim() ? `User request: ${prompt}` : "";
const reply = await askAgent(
`Improve, debug, or refactor this ${langMeta.id} file.
${userHint}
Return ONLY updated code, no explanation.
CODE:
${currentFile.content}`
);
updateActiveFileContent(reply);
};
const handleExplainSelection = async () => {
if (!currentFile) return;
const editor = editorRef.current;
const selected = editor
.getModel()
.getValueInRange(editor.getSelection());
const code = selected.trim() || currentFile.content;
const userHint = prompt.trim()
? `Focus on: ${prompt}`
: "Give a clear and simple explanation.";
const reply = await askAgent(
`Explain what this ${langMeta.id} code does, any risks, and improvements.
${userHint}
CODE:
${code}`
);
setExplanation(reply);
};
const updateActiveFileContent = (value) => {
const updated = updateFileContent(tree, activePath, value);
setTree(updated);
};
// ===== AI Autocomplete Suggestions (Popup) =====
const fetchAiSuggestions = async (code) => {
if (!code?.trim()) return;
const reply = await askAgent(
`Suggest possible next lines for continuation. Return 3 short snippets.\n${code}`
);
setAiSuggestions(reply.split("\n").filter((l) => l.trim()));
};
// =================== SEARCH ===================
const handleSearchToggle = () => setSearchOpen(!searchOpen);
const handleSearchNow = () => {
if (!searchQuery) return;
alert(
`Found occurrences:\n${JSON.stringify(
searchTree(tree, searchQuery),
null,
2
)}`
);
};
// =================== UI ===================
return (
<div
className={`ide-root ${
theme === "vs-dark" ? "ide-dark" : "ide-light"
}`}
>
{/* ==== TOP BAR ==== */}
<div className="ide-menubar">
<div className="ide-menubar-left">
<span className="ide-logo">βοΈ DevMate IDE</span>
<button onClick={handleNewFile}>π New File</button>
<button onClick={handleNewFolder}>π New Folder</button>
<button onClick={handleRename}>βοΈ Rename</button>
<button onClick={downloadFile}>π₯ Download</button>
<button onClick={downloadProjectZip}>π¦ ZIP</button>
</div>
<div className="ide-menubar-right">
<button onClick={handleSearchToggle}>π Search</button>
<button onClick={handleRun}>βΆ Run</button>
<button onClick={handleAskFix}>π€ Fix</button>
<button onClick={handleExplainSelection}>π Explain</button>
<button
onClick={() =>
setTheme(theme === "vs-dark" ? "light" : "vs-dark")
}
>
{theme === "vs-dark" ? "βοΈ" : "π"}
</button>
</div>
</div>
{/* ==== BODY ==== */}
<div className="ide-body">
{/* ==== TREE VIEW (LEFT) ==== */}
<div className="ide-sidebar">
{renderTree(tree, setActivePath, setContextMenu)}
</div>
{/* ==== MAIN CENTER (EDITOR + OUTPUT) ==== */}
<div className="ide-main">
<div className="ide-editor-wrapper">
<Editor
height="100%"
theme={theme}
language={langMeta.monaco}
value={currentFile?.content || ""}
onChange={updateActiveFileContent}
onMount={(editor) => (editorRef.current = editor)}
onBlur={() => fetchAiSuggestions(currentFile?.content)}
/>
</div>
{/* AI Suggestions Tooltip */}
{aiSuggestions.length > 0 && (
<div className="ai-popup">
{aiSuggestions.map((s, i) => (
<div
key={i}
className="ai-suggest"
onClick={() =>
updateActiveFileContent(
(currentFile?.content || "") + "\n" + s
)
}
>
{s}
</div>
))}
</div>
)}
{/* Bottom Panels: Output + stdin + Problems */}
<div className="ide-panels">
<pre className="ide-output">{output}</pre>
<input
className="ide-input-box"
placeholder="Program input..."
value={stdin}
onChange={(e) => setStdin(e.target.value)}
/>
{problems.length > 0 && (
<div className="ide-problems-panel">
<div>π¨ Problems ({problems.length})</div>
{problems.map((p, i) => (
<div key={i}>
{p.file}:{p.line} β {p.message}
</div>
))}
</div>
)}
</div>
</div>
{/* ==== RIGHT AI PANEL ==== */}
<div className="ide-right-panel">
<div className="ide-ai-header">π€ AI Assistant</div>
<div className="ide-ai-section">
<label className="ide-ai-label">Instruction</label>
<textarea
className="ide-agent-textarea"
placeholder="Ask the AI (e.g., 'optimize this function', 'add comments', 'convert to JS'...)"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
/>
</div>
<div className="ide-ai-buttons">
<button onClick={handleAskFix}>π‘ Apply Fix</button>
<button onClick={handleExplainSelection}>π Explain Code</button>
</div>
{explanation && (
<div className="ide-ai-section">
<label className="ide-ai-label">Explanation</label>
<div className="ide-explain">{explanation}</div>
</div>
)}
</div>
</div>
{/* ==== SEARCH PANEL ==== */}
{searchOpen && (
<div className="search-dialog">
<input
placeholder="Search text..."
onChange={(e) => setSearchQuery(e.target.value)}
/>
<button onClick={handleSearchNow}>Search</button>
</div>
)}
{/* ==== RIGHT-CLICK CONTEXT MENU ==== */}
{contextMenu && (
<div
className="ide-context-menu"
style={{ top: contextMenu.y, left: contextMenu.x }}
onMouseLeave={() => setContextMenu(null)}
>
<div onClick={handleRename}>βοΈ Rename</div>
<div onClick={handleDelete}>π Delete</div>
<div onClick={downloadFile}>π₯ Download</div>
</div>
)}
</div>
);
}
// =======================================================
// Tree Renderer UI
// =======================================================
function renderTree(node, setActivePath, setContextMenu, depth = 0) {
return (
<div key={node.name} style={{ paddingLeft: depth * 10 }}>
<div
className={`tree-item ${node.type}`}
onClick={() => node.type === "file" && setActivePath(node.path)}
onContextMenu={(e) => {
e.preventDefault();
setContextMenu({ x: e.pageX, y: e.pageY, file: node.path });
}}
>
{node.type === "folder" ? "π" : "π"} {node.name}
</div>
{node.children &&
node.children.map((child) =>
renderTree(child, setActivePath, setContextMenu, depth + 1)
)}
</div>
);
}
export default App;
|