Spaces:
Build error
Build error
File size: 12,433 Bytes
82528c0 | 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 | import { useState, useEffect, useRef } from 'react'
import { FiPlay, FiSave, FiShare2, FiSettings, FiDownload, FiUpload, FiTerminal, FiDebug, FiGitPullRequest, FiSearch, FiFile, FiFolder } from 'react-icons/fi'
import dynamic from 'next/dynamic'
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'
import { useHotkeys } from 'react-hotkeys-hook'
const MonacoEditor = dynamic(() => import('@monaco-editor/react'), {
ssr: false,
loading: () => <div className="h-full bg-editor rounded-lg animate-pulse" />
})
export default function Editor() {
const [code, setCode] = useState('')
const [language, setLanguage] = useState('javascript')
const [output, setOutput] = useState('')
const [isRunning, setIsRunning] = useState(false)
const [error, setError] = useState(null)
const [theme, setTheme] = useState('vs-dark')
const [fontSize, setFontSize] = useState(14)
const [showSettings, setShowSettings] = useState(false)
const [savedSnippets, setSavedSnippets] = useState([])
const [snippetName, setSnippetName] = useState('')
const [activeTab, setActiveTab] = useState('editor')
const [terminalOutput, setTerminalOutput] = useState('')
const [debugMode, setDebugMode] = useState(false)
const editorRef = useRef(null)
const languages = [
{ value: 'javascript', label: 'JavaScript', icon: 'π‘' },
{ value: 'typescript', label: 'TypeScript', icon: 'π΅' },
{ value: 'python', label: 'Python', icon: 'π' },
{ value: 'java', label: 'Java', icon: 'β' },
{ value: 'csharp', label: 'C#', icon: 'π§' },
{ value: 'go', label: 'Go', icon: 'πΉ' },
{ value: 'rust', label: 'Rust', icon: 'π¦' },
{ value: 'html', label: 'HTML', icon: 'π' },
{ value: 'css', label: 'CSS', icon: 'π¨' }
]
const themes = [
{ value: 'vs-dark', label: 'Dark (VSCode)' },
{ value: 'vs-light', label: 'Light (VSCode)' },
{ value: 'hc-black', label: 'High Contrast' }
]
useEffect(() => {
const saved = localStorage.getItem('codeSnippets')
if (saved) {
setSavedSnippets(JSON.parse(saved))
}
}, [])
const handleRunCode = async () => {
if (!code.trim()) return
setIsRunning(true)
setError(null)
setOutput('')
setTerminalOutput('Running code...\n')
try {
const response = await fetch('/api/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code, language }),
})
const data = await response.json()
if (response.ok) {
setOutput(data.output)
setTerminalOutput(prev => prev + `\nExecution completed successfully.\nOutput:\n${data.output}`)
} else {
throw new Error(data.error || 'Execution failed')
}
} catch (err) {
setError(err.message)
setTerminalOutput(prev => prev + `\nError: ${err.message}`)
} finally {
setIsRunning(false)
}
}
const handleSaveSnippet = () => {
if (!snippetName.trim()) return
const newSnippet = {
id: Date.now(),
name: snippetName,
code,
language,
createdAt: new Date().toISOString()
}
const updatedSnippets = [...savedSnippets, newSnippet]
setSavedSnippets(updatedSnippets)
localStorage.setItem('codeSnippets', JSON.stringify(updatedSnippets))
setSnippetName('')
}
const handleLoadSnippet = (snippet) => {
setCode(snippet.code)
setLanguage(snippet.language)
}
const handleDownloadCode = () => {
const blob = new Blob([code], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `code.${language === 'javascript' ? 'js' : language === 'python' ? 'py' : 'txt'}`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
const handleUploadCode = (e) => {
const file = e.target.files[0]
if (!file) return
const reader = new FileReader()
reader.onload = (event) => {
setCode(event.target.result)
}
reader.readAsText(file)
}
const handleEditorMount = (editor) => {
editorRef.current = editor
}
useHotkeys('ctrl+s', (e) => {
e.preventDefault()
handleSaveSnippet()
})
useHotkeys('f5', (e) => {
e.preventDefault()
handleRunCode()
})
return (
<div className="flex flex-col h-[calc(100vh-64px)]">
<div className="flex items-center justify-between p-2 bg-panel border-b border-border">
<div className="flex items-center space-x-2">
<button
onClick={() => setActiveTab('editor')}
className={`px-3 py-1 rounded text-sm flex items-center space-x-1 ${activeTab === 'editor' ? 'bg-primary text-white' : 'text-gray-400 hover:bg-panel'}`}
>
<FiFile />
<span>Editor</span>
</button>
<button
onClick={() => setActiveTab('terminal')}
className={`px-3 py-1 rounded text-sm flex items-center space-x-1 ${activeTab === 'terminal' ? 'bg-primary text-white' : 'text-gray-400 hover:bg-panel'}`}
>
<FiTerminal />
<span>Terminal</span>
</button>
<button
onClick={() => setActiveTab('debug')}
className={`px-3 py-1 rounded text-sm flex items-center space-x-1 ${activeTab === 'debug' ? 'bg-primary text-white' : 'text-gray-400 hover:bg-panel'}`}
>
<FiDebug />
<span>Debug</span>
</button>
</div>
<div className="flex items-center space-x-2">
<select
value={language}
onChange={(e) => setLanguage(e.target.value)}
className="bg-editor text-white px-3 py-1 rounded text-sm"
>
{languages.map((lang) => (
<option key={lang.value} value={lang.value}>
{lang.icon} {lang.label}
</option>
))}
</select>
<button
onClick={handleRunCode}
disabled={isRunning}
className={`px-3 py-1 rounded text-sm flex items-center space-x-1 ${isRunning ? 'bg-gray-600' : 'bg-success hover:bg-green-600'} text-white transition-colors`}
>
<FiPlay />
<span>{isRunning ? 'Running...' : 'Run (F5)'}</span>
</button>
<button
onClick={() => setShowSettings(!showSettings)}
className="px-3 py-1 bg-panel hover:bg-gray-600 text-white rounded text-sm flex items-center space-x-1 transition-colors"
>
<FiSettings />
<span>Settings</span>
</button>
</div>
</div>
{showSettings && (
<div className="bg-panel rounded-lg p-4 m-2 border border-border">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">Theme</label>
<select
value={theme}
onChange={(e) => setTheme(e.target.value)}
className="w-full bg-editor text-white px-3 py-2 rounded"
>
{themes.map((t) => (
<option key={t.value} value={t.value}>{t.label}</option>
))}
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">Font Size</label>
<input
type="range"
min="12"
max="24"
value={fontSize}
onChange={(e) => setFontSize(parseInt(e.target.value))}
className="w-full"
/>
<div className="text-center text-sm text-gray-300 mt-1">{fontSize}px</div>
</div>
</div>
</div>
)}
<div className="flex-1 overflow-hidden">
{activeTab === 'editor' && (
<div className="h-full bg-editor">
<MonacoEditor
height="100%"
language={language}
theme={theme}
value={code}
onChange={setCode}
onMount={handleEditorMount}
options={{
fontSize,
minimap: { enabled: true },
wordWrap: 'on',
scrollBeyondLastLine: false,
automaticLayout: true,
renderLineHighlight: 'all',
folding: true,
lineNumbers: 'on',
roundedSelection: true,
scrollbar: {
vertical: 'auto',
horizontal: 'auto'
}
/>
</div>
)}
{activeTab === 'terminal' && (
<div className="h-full bg-background p-4 font-mono text-sm text-gray-300 overflow-auto">
<div className="mb-2 flex items-center space-x-2">
<span className="text-green-400">$</span>
<span>Terminal</span>
</div>
<div className="h-[calc(100%-30px)] overflow-auto">
{terminalOutput || 'Terminal output will appear here...'}
</div>
</div>
)}
{activeTab === 'debug' && (
<div className="h-full bg-background p-4">
<div className="flex items-center justify-between mb-4">
<h3 className="text-gray-300">Debug Console</h3>
<button
onClick={() => setDebugMode(!debugMode)}
className={`px-3 py-1 rounded text-sm ${debugMode ? 'bg-danger' : 'bg-primary'} text-white`}
>
{debugMode ? 'Stop Debugging' : 'Start Debugging'}
</button>
</div>
<div className="h-[calc(100%-50px)] bg-panel rounded p-3 overflow-auto">
{debugMode ? (
<div className="text-gray-300">
<p>Debug mode active. Breakpoints and variables will appear here.</p>
<div className="mt-4 space-y-2">
<div className="flex justify-between">
<span>Status:</span>
<span className="text-green-400">Running</span>
</div>
<div className="flex justify-between">
<span>Breakpoints:</span>
<span>0</span>
</div>
</div>
</div>
) : (
<p className="text-gray-500">Debug console ready. Click "Start Debugging" to begin.</p>
)}
</div>
</div>
)}
</div>
<div className="bg-panel border-t border-border p-2">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
<input
type="text"
placeholder="Snippet name"
value={snippetName}
onChange={(e) => setSnippetName(e.target.value)}
className="px-3 py-1 bg-editor text-white rounded text-sm w-48"
/>
<button
onClick={handleSaveSnippet}
disabled={!snippetName.trim()}
className="px-3 py-1 bg-yellow-600 hover:bg-yellow-700 text-white rounded text-sm flex items-center space-x-1 transition-colors disabled:opacity-50"
>
<FiSave />
<span>Save (Ctrl+S)</span>
</button>
</div>
<div className="flex items-center space-x-2">
<label className="px-3 py-1 bg-green-600 hover:bg-green-700 text-white rounded text-sm cursor-pointer flex items-center space-x-1 transition-colors">
<FiUpload />
<span>Upload</span>
<input type="file" className="hidden" onChange={handleUploadCode} accept=".js,.py,.java,.cs,.ts,.go,.rs,.html,.css" />
</label>
<button
onClick={handleDownloadCode}
className="px-3 py-1 bg-blue-600 hover:bg-blue-700 text-white rounded text-sm flex items-center space-x-1 transition-colors"
>
<FiDownload />
<span>Download</span>
</button>
</div>
</div>
</div>
</div>
)
} |