Spaces:
Build error
Build error
Create frontend/src/components/code-playground/CodeEditor.tsx
Browse files
frontend/src/components/code-playground/CodeEditor.tsx
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// src/components/code-playground/CodeEditor.tsx
|
| 2 |
+
import React, { useState } from 'react';
|
| 3 |
+
import { Card, CardHeader, CardContent, CardTitle } from '@/components/ui/card';
|
| 4 |
+
import { Play, Trash2 } from 'lucide-react';
|
| 5 |
+
import OutputDisplay from './OutputDisplay';
|
| 6 |
+
|
| 7 |
+
const CodeEditor = () => {
|
| 8 |
+
const [code, setCode] = useState('');
|
| 9 |
+
const [output, setOutput] = useState('');
|
| 10 |
+
const [error, setError] = useState('');
|
| 11 |
+
const [isRunning, setIsRunning] = useState(false);
|
| 12 |
+
|
| 13 |
+
const handleRunCode = async () => {
|
| 14 |
+
if (!code.trim()) return;
|
| 15 |
+
|
| 16 |
+
setIsRunning(true);
|
| 17 |
+
setError('');
|
| 18 |
+
setOutput('');
|
| 19 |
+
|
| 20 |
+
try {
|
| 21 |
+
const response = await fetch('/api/execute-code', {
|
| 22 |
+
method: 'POST',
|
| 23 |
+
headers: { 'Content-Type': 'application/json' },
|
| 24 |
+
body: JSON.stringify({ code })
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
const data = await response.json();
|
| 28 |
+
|
| 29 |
+
if (data.error) {
|
| 30 |
+
setError(data.error);
|
| 31 |
+
} else {
|
| 32 |
+
setOutput(data.output);
|
| 33 |
+
}
|
| 34 |
+
} catch (err) {
|
| 35 |
+
setError('Failed to execute code. Please try again.');
|
| 36 |
+
} finally {
|
| 37 |
+
setIsRunning(false);
|
| 38 |
+
}
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
const handleClear = () => {
|
| 42 |
+
setCode('');
|
| 43 |
+
setOutput('');
|
| 44 |
+
setError('');
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
return (
|
| 48 |
+
<Card className="h-[600px] flex flex-col">
|
| 49 |
+
<CardHeader>
|
| 50 |
+
<CardTitle>Python Code Playground</CardTitle>
|
| 51 |
+
</CardHeader>
|
| 52 |
+
<CardContent className="flex-1 flex flex-col gap-4">
|
| 53 |
+
<div className="flex-1 flex flex-col">
|
| 54 |
+
<textarea
|
| 55 |
+
value={code}
|
| 56 |
+
onChange={(e) => setCode(e.target.value)}
|
| 57 |
+
placeholder="Write your Python code here..."
|
| 58 |
+
className="flex-1 p-4 font-mono text-sm border rounded-lg focus:ring-2
|
| 59 |
+
|
| 60 |
+
focus:ring-blue-500 focus:border-blue-500"
|
| 61 |
+
/>
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
<div className="flex gap-2">
|
| 65 |
+
<button
|
| 66 |
+
onClick={handleRunCode}
|
| 67 |
+
disabled={isRunning}
|
| 68 |
+
className="flex items-center gap-2 bg-blue-500 text-white px-4 py-2
|
| 69 |
+
rounded hover:bg-blue-600 disabled:bg-blue-300"
|
| 70 |
+
>
|
| 71 |
+
<Play size={16} />
|
| 72 |
+
{isRunning ? 'Running...' : 'Run Code'}
|
| 73 |
+
</button>
|
| 74 |
+
|
| 75 |
+
<button
|
| 76 |
+
onClick={handleClear}
|
| 77 |
+
className="flex items-center gap-2 bg-gray-500 text-white px-4 py-2
|
| 78 |
+
rounded hover:bg-gray-600"
|
| 79 |
+
>
|
| 80 |
+
<Trash2 size={16} />
|
| 81 |
+
Clear
|
| 82 |
+
</button>
|
| 83 |
+
</div>
|
| 84 |
+
|
| 85 |
+
<OutputDisplay output={output} error={error} />
|
| 86 |
+
</CardContent>
|
| 87 |
+
</Card>
|
| 88 |
+
);
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
export default CodeEditor;
|