import { useState, useEffect, type KeyboardEvent } from 'react'; import { PageParseAPI } from '../services/api'; import { Code, Sparkles, Copy, Check, FileCode, Terminal, Braces, Globe, Database, Cpu, Coffee, Wifi, WifiOff, Settings, RefreshCw } from 'lucide-react'; const LANGUAGES = [ { value: 'auto', label: 'Auto-Detect' }, { value: 'python', label: 'Python' }, { value: 'javascript', label: 'JavaScript' }, { value: 'typescript', label: 'TypeScript' }, { value: 'java', label: 'Java' }, { value: 'cpp', label: 'C/C++' }, { value: 'rust', label: 'Rust' }, { value: 'go', label: 'Go' }, { value: 'ruby', label: 'Ruby' }, { value: 'bash', label: 'Bash/Shell' }, { value: 'html', label: 'HTML' }, { value: 'css', label: 'CSS' }, { value: 'sql', label: 'SQL' }, ]; const LANG_ICONS: Record = { python: Terminal, javascript: Braces, typescript: Braces, java: Coffee, cpp: FileCode, rust: Cpu, go: Globe, html: Globe, css: FileCode, sql: Database, bash: Terminal, }; interface CodeAnalysisResult { language: string; explanation: string; summary: string; } export const CodeEditor = () => { const [code, setCode] = useState(''); const [selectedLanguage, setSelectedLanguage] = useState('auto'); const [result, setResult] = useState(null); const [isAnalyzing, setIsAnalyzing] = useState(false); const [error, setError] = useState(null); const [copied, setCopied] = useState(false); const [ollamaConnected, setOllamaConnected] = useState(false); const [ollamaModel, setOllamaModel] = useState(''); const [ollamaUrl, setOllamaUrl] = useState('http://localhost:11434'); const [ollamaVersion, setOllamaVersion] = useState(''); const [modelAvailable, setModelAvailable] = useState(false); const [checkingStatus, setCheckingStatus] = useState(true); const [showConfig, setShowConfig] = useState(false); const [configUrl, setConfigUrl] = useState(''); const [configModel, setConfigModel] = useState(''); const [availableModels, setAvailableModels] = useState([]); const [saving, setSaving] = useState(false); const [configSaved, setConfigSaved] = useState(false); const checkStatus = async () => { setCheckingStatus(true); try { const status = await PageParseAPI.checkOllamaStatus(); setOllamaConnected(status.connected); setOllamaModel(status.model); setOllamaUrl(status.url); setOllamaVersion(status.version); setModelAvailable(status.model_available); setAvailableModels(status.models || []); setConfigUrl(status.url); setConfigModel(status.model); } catch { setOllamaConnected(false); } setCheckingStatus(false); }; useEffect(() => { checkStatus(); }, []); const handleSaveConfig = async () => { setSaving(true); try { const res = await PageParseAPI.configureOllama(configUrl, configModel); setOllamaUrl(res.url); setOllamaModel(res.model); setConfigSaved(true); setTimeout(() => setConfigSaved(false), 2000); setTimeout(() => checkStatus(), 1000); } catch (err: any) { setError(err.message || 'Failed to save config'); } setSaving(false); }; const handleAnalyze = async () => { if (!code.trim()) return; setIsAnalyzing(true); setError(null); setResult(null); try { const res = await PageParseAPI.analyzeCode(code, selectedLanguage); setResult(res); } catch (err: any) { setError(err.message || 'Analysis failed'); } setIsAnalyzing(false); }; const handleCopy = async () => { if (!result) return; const text = `Language: ${result.language}\n\nExplanation:\n${result.explanation}\n\nSummary:\n${result.summary}`; await navigator.clipboard.writeText(text); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const handleKeyDown = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); handleAnalyze(); } }; const snippetCount = code.trim() ? code.trim().split(/\s+/).length : 0; const lineCount = code ? code.split('\n').length : 0; const IconComponent = result && LANG_ICONS[result.language] ? LANG_ICONS[result.language] : null; return (

Code Paste & Analyze

{checkingStatus ? ( Checking Ollama connection... ) : ollamaConnected ? ( Ollama {ollamaVersion && `v${ollamaVersion}`} connected {modelAvailable ? ` (${ollamaModel})` : ` — model "${ollamaModel}" not pulled yet`} {!modelAvailable && ( Run: ollama pull {ollamaModel} )} ) : ( Ollama not reachable at {ollamaUrl} — using local fallback )}
{showConfig && (
setConfigUrl(e.target.value)} placeholder="http://localhost:11434" style={{ width: '100%', marginTop: 4 }} />
setConfigModel(e.target.value)} placeholder="llama3.2:1b" style={{ flex: 1 }} list="ollama-models" /> {availableModels.map(m =>
)}
{lineCount} lines · {snippetCount} tokens {code.length > 0 && `${code.length} chars`}