import React, { useState } from 'react'; import { Code, Wand2, Download, Loader2, Key, Bug, Zap, CheckCircle, AlertTriangle, Sparkles, Terminal, Cpu, FileCode, GitBranch, RefreshCw } from 'lucide-react'; export default function CodeDebugger() { const [code, setCode] = useState(''); const [language, setLanguage] = useState('javascript'); const [action, setAction] = useState('debug'); const [output, setOutput] = useState(''); const [isProcessing, setIsProcessing] = useState(false); const [error, setError] = useState(''); const languages = [ { id: 'javascript', name: 'JavaScript', color: 'from-yellow-400 to-orange-500' }, { id: 'python', name: 'Python', color: 'from-blue-400 to-cyan-500' }, { id: 'typescript', name: 'TypeScript', color: 'from-blue-500 to-indigo-600' }, { id: 'react', name: 'React/JSX', color: 'from-cyan-400 to-blue-500' }, { id: 'java', name: 'Java', color: 'from-red-500 to-orange-600' }, { id: 'cpp', name: 'C++', color: 'from-purple-500 to-pink-600' }, { id: 'go', name: 'Go', color: 'from-cyan-500 to-teal-600' }, { id: 'rust', name: 'Rust', color: 'from-orange-500 to-red-600' }, { id: 'sql', name: 'SQL', color: 'from-green-500 to-emerald-600' }, ]; const actions = [ { id: 'debug', name: 'Debug Code', icon: Bug, color: 'from-red-500 to-pink-600', prompt: (code, lang) => `Analyze this ${lang} code for bugs and errors: \`\`\`${lang} ${code} \`\`\` Provide: 1. Identified bugs and errors 2. Explanation of each issue 3. Fixed code 4. Best practices violated Format the response clearly with sections.` }, { id: 'optimize', name: 'Optimize Code', icon: Zap, color: 'from-yellow-500 to-orange-600', prompt: (code, lang) => `Optimize this ${lang} code for better performance and readability: \`\`\`${lang} ${code} \`\`\` Provide: 1. Performance improvements 2. Code readability enhancements 3. Optimized version 4. Complexity analysis (Big O if applicable) Format the response clearly with sections.` }, { id: 'explain', name: 'Explain Code', icon: FileCode, color: 'from-blue-500 to-indigo-600', prompt: (code, lang) => `Explain this ${lang} code in detail: \`\`\`${lang} ${code} \`\`\` Provide: 1. High-level overview 2. Line-by-line explanation 3. Key concepts used 4. Use cases and applications Make it beginner-friendly but thorough.` }, { id: 'convert', name: 'Convert Language', icon: RefreshCw, color: 'from-purple-500 to-pink-600', prompt: (code, lang) => `Convert this ${lang} code to Python: \`\`\`${lang} ${code} \`\`\` Provide: 1. Converted Python code 2. Explanation of key differences 3. Any Python-specific best practices applied Keep the logic identical but use Python idioms.` }, { id: 'review', name: 'Code Review', icon: CheckCircle, color: 'from-green-500 to-emerald-600', prompt: (code, lang) => `Perform a thorough code review of this ${lang} code: \`\`\`${lang} ${code} \`\`\` Review for: 1. Code quality and maintainability 2. Security vulnerabilities 3. Performance issues 4. Best practices and patterns 5. Suggested improvements Provide a professional code review with ratings and actionable feedback.` }, { id: 'test', name: 'Generate Tests', icon: Terminal, color: 'from-cyan-500 to-blue-600', prompt: (code, lang) => `Generate comprehensive unit tests for this ${lang} code: \`\`\`${lang} ${code} \`\`\` Provide: 1. Complete test suite code 2. Test cases covering edge cases 3. Explanation of test strategy 4. Synthetic fixtures if needed Use the appropriate testing framework for ${lang}.` }, { id: 'document', name: 'Add Documentation', icon: FileCode, color: 'from-indigo-500 to-purple-600', prompt: (code, lang) => `Add comprehensive documentation to this ${lang} code: \`\`\`${lang} ${code} \`\`\` Provide: 1. Fully documented version with comments 2. Function/method documentation 3. Usage examples 4. API documentation if applicable Follow ${lang} documentation standards.` }, ]; const processCode = async () => { if (!code.trim()) { setError('Please paste your code'); return; } setIsProcessing(true); setError(''); setOutput(''); try { const currentAction = actions.find(a => a.id === action); const response = await fetch('/.netlify/functions/gateway-chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ provider: 'SKYES OVER LONDON', model: 'kAIxU', max_tokens: 4000, messages: [{ role: 'user', content: currentAction.prompt(code, language) }] }) }); const data = await response.json(); const text = data?.choices?.[0]?.message?.content || data?.candidates?.[0]?.content?.parts?.[0]?.text || data?.content?.[0]?.text; if (text) { setOutput(text); } else if (data.error) { setError(`API Error: ${data.error.message || 'Failed to process code'}`); } else { setError('Failed to process code. Please try again.'); } } catch (err) { setError(`Error: ${err.message}`); } finally { setIsProcessing(false); } }; const downloadOutput = () => { const blob = new Blob([output], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `code-${action}.txt`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; const copyOutput = () => { navigator.clipboard.writeText(output); alert('Output copied to clipboard!'); }; return (
Select Language
{output}