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: () =>
}) 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 (Debug mode active. Breakpoints and variables will appear here.
Debug console ready. Click "Start Debugging" to begin.
)}