Spaces:
Build error
Build error
File size: 11,101 Bytes
8c3e275 | 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 | 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<string, typeof Braces> = {
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<CodeAnalysisResult | null>(null);
const [isAnalyzing, setIsAnalyzing] = useState(false);
const [error, setError] = useState<string | null>(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<string[]>([]);
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<HTMLTextAreaElement>) => {
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 (
<div className="section">
<div className="section-header">
<h3><Code size={16} className="text-secondary" /> Code Paste & Analyze</h3>
<div className="flex gap-3 items-center">
<select value={selectedLanguage} onChange={e => setSelectedLanguage(e.target.value)} className="select">
{LANGUAGES.map(opt => <option key={opt.value} value={opt.value}>{opt.label}</option>)}
</select>
</div>
</div>
<div className="section-body">
<div className="ollama-status-bar">
<div className="flex items-center gap-2">
{checkingStatus ? (
<span className="text-xs text-tertiary flex items-center gap-1">
<span className="animate-pulse">Checking Ollama connection...</span>
</span>
) : ollamaConnected ? (
<span className="text-xs flex items-center gap-1" style={{ color: 'var(--success)' }}>
<Wifi size={12} />
<span>
Ollama {ollamaVersion && `v${ollamaVersion}`} connected
{modelAvailable
? ` (${ollamaModel})`
: ` — model "${ollamaModel}" not pulled yet`}
</span>
{!modelAvailable && (
<span className="text-xs" style={{ color: 'var(--warning)' }}>
Run: ollama pull {ollamaModel}
</span>
)}
</span>
) : (
<span className="text-xs flex items-center gap-1" style={{ color: 'var(--danger)' }}>
<WifiOff size={12} />
<span>Ollama not reachable at {ollamaUrl} — using local fallback</span>
</span>
)}
</div>
<div className="flex items-center gap-2">
<button className="btn btn-sm" onClick={checkStatus} title="Refresh connection status">
<RefreshCw size={12} />
</button>
<button className="btn btn-sm" onClick={() => setShowConfig(!showConfig)} title="Configure Ollama">
<Settings size={12} />
</button>
</div>
</div>
{showConfig && (
<div className="ollama-config-panel">
<div className="flex flex-col gap-3">
<div>
<label className="text-xs font-semibold text-tertiary" style={{ textTransform: 'uppercase', letterSpacing: '0.5px' }}>Ollama URL</label>
<input className="input" value={configUrl} onChange={e => setConfigUrl(e.target.value)} placeholder="http://localhost:11434" style={{ width: '100%', marginTop: 4 }} />
</div>
<div>
<label className="text-xs font-semibold text-tertiary" style={{ textTransform: 'uppercase', letterSpacing: '0.5px' }}>Model Name</label>
<div className="flex gap-2 items-center" style={{ marginTop: 4 }}>
<input className="input" value={configModel} onChange={e => setConfigModel(e.target.value)} placeholder="llama3.2:1b" style={{ flex: 1 }} list="ollama-models" />
<datalist id="ollama-models">
{availableModels.map(m => <option key={m} value={m} />)}
</datalist>
<button className="btn btn-primary btn-sm" onClick={handleSaveConfig} disabled={saving}>
{saving ? 'Saving...' : configSaved ? <><Check size={12} /> Saved</> : 'Save'}
</button>
</div>
</div>
</div>
</div>
)}
<div className="code-editor-container" style={{ marginTop: 8 }}>
<div className="code-editor-header">
<span className="text-xs text-tertiary">
{lineCount} lines · {snippetCount} tokens
</span>
<span className="text-xs text-tertiary">
{code.length > 0 && `${code.length} chars`}
</span>
</div>
<textarea
className="code-textarea"
value={code}
onChange={e => setCode(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={`Paste or type code here...\n\nThe SLM will explain what it does and summarize its purpose.\n\nSupports: Python, JavaScript, TypeScript, Java, C/C++, Rust, Go, Ruby, Bash, HTML, CSS, SQL, and more.`}
spellCheck={false}
rows={14}
/>
<div className="code-editor-actions">
<button
className="btn btn-primary"
onClick={handleAnalyze}
disabled={!code.trim() || isAnalyzing}
>
{isAnalyzing ? (
<span className="flex items-center gap-2">
<span className="animate-pulse">Analyzing...</span>
</span>
) : (
<span className="flex items-center gap-2">
<Sparkles size={14} />
Explain & Summarize
</span>
)}
</button>
<span className="text-xs text-tertiary">Ctrl+Enter to run</span>
</div>
</div>
{error && (
<div className="alert alert-error" style={{ marginTop: 16 }}>
<span>{error}</span>
</div>
)}
{result && (
<div className="code-analysis-result">
<div className="result-header">
<div className="result-language-badge">
{IconComponent ? (
<span className="flex items-center gap-1">
<IconComponent size={14} />
<span>{result.language.charAt(0).toUpperCase() + result.language.slice(1)}</span>
</span>
) : (
<span className="flex items-center gap-1">
<FileCode size={14} />
<span>{result.language.charAt(0).toUpperCase() + result.language.slice(1)}</span>
</span>
)}
</div>
<button className="btn btn-sm" onClick={handleCopy} title="Copy analysis">
{copied ? <Check size={14} /> : <Copy size={14} />}
</button>
</div>
<div className="result-summary">
<div className="result-label">Summary</div>
<p className="result-text">{result.summary}</p>
</div>
<div className="result-explanation">
<div className="result-label">Explanation</div>
<p className="result-text">{result.explanation}</p>
</div>
</div>
)}
</div>
</div>
);
};
|