import { useEffect, useState, type FormEvent } from 'react'; import { PlugZap, Save } from 'lucide-react'; import { getActiveEvent, getLlmSettings, saveLlmSettings, testLlmConnection } from '../lib/api'; import type { Event, LlmTestResult } from '../lib/types'; import { Spinner } from '../components/ui'; import { BadgeCopiesSetting } from '../components/BadgeCopiesSetting'; export function Settings() { const [endpoint, setEndpoint] = useState(''); const [model, setModel] = useState(''); const [apiKey, setApiKey] = useState(''); const [hasKey, setHasKey] = useState(false); const [clearKey, setClearKey] = useState(false); const [loaded, setLoaded] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [saved, setSaved] = useState(false); const [testing, setTesting] = useState(false); const [testResult, setTestResult] = useState(null); // What is actually stored in the DB. The sidecar tests the stored settings // (the API key never reaches the browser), so unsaved edits cannot be tested — // this is what lets the UI say "save first" instead of testing stale values. const [baseline, setBaseline] = useState({ endpoint: '', model: '', has_api_key: false }); // Badge printing is per event, so it follows the active event. const [activeEvent, setActiveEvent] = useState(null); useEffect(() => { let active = true; getLlmSettings().then(settings => { if (!active) return; setEndpoint(settings.endpoint); setModel(settings.model); setHasKey(settings.has_api_key); setBaseline({ endpoint: settings.endpoint, model: settings.model, has_api_key: settings.has_api_key }); setLoaded(true); }).catch(cause => { if (active) setError(cause instanceof Error ? cause.message : 'Failed to load settings'); }); // Best-effort: a failure here just hides the badge section, it must not // break the Feedback AI settings below. getActiveEvent() .then(event => { if (active) setActiveEvent(event); }) .catch(() => { if (active) setActiveEvent(null); }); return () => { active = false; }; }, []); // Unsaved edits mean a test would measure something other than what's on // screen, so the button stays disabled with an explanation instead. const pendingChanges = endpoint.trim() !== baseline.endpoint || model.trim() !== baseline.model || clearKey || apiKey.length > 0; async function save(event: FormEvent) { event.preventDefault(); setBusy(true); setSaved(false); setError(null); try { const settings = await saveLlmSettings({ endpoint: endpoint.trim(), model: model.trim(), ...(clearKey ? { api_key: '' } : apiKey ? { api_key: apiKey } : {}) }); setEndpoint(settings.endpoint); setHasKey(settings.has_api_key); setBaseline({ endpoint: settings.endpoint, model: settings.model, has_api_key: settings.has_api_key }); setApiKey(''); setClearKey(false); setSaved(true); // Saving changes what a test would measure, so any old verdict is void. setTestResult(null); } catch (cause) { setError(cause instanceof Error ? cause.message : 'Failed to save settings'); } finally { setBusy(false); } } async function testConnection() { if (testing) return; setTesting(true); setTestResult(null); setError(null); try { setTestResult(await testLlmConnection()); } catch (cause) { setError(cause instanceof Error ? cause.message : 'Could not reach the sync sidecar (is it running?)'); } finally { setTesting(false); } } return (

Settings

{error &&

{error}

} {!loaded && !error && } {activeEvent && (

Badge printing

)} {loaded &&
setSaved(false)}>

Feedback AI

setEndpoint(event.target.value)} />
setModel(event.target.value)} />
setApiKey(event.target.value)} />
{hasKey && }
{saved && Settings saved}
{/* type=button: inside the form, a submit would save instead of test. */} {pendingChanges && Save first — the test uses the stored settings.}
{testing &&

Sending one request to {model.trim() || 'the configured model'}…

} {/* The verdict stays on the page rather than in a toast: the latency and the reasoning-token line are the parts people need to read twice. */} {testResult && (
{testResult.ok ? testResult.reasoning_disabled ? `Connection works — replied in ${testResult.latency_ms} ms` : 'Connection works, but the model is still thinking' : 'Connection failed'} {testResult.model &&

{testResult.model} at {testResult.endpoint}

} {testResult.error &&

{testResult.error}

} {testResult.warning &&

{testResult.warning}

} {testResult.ok && (

Reasoning tokens: {testResult.reasoning_tokens === null ? 'not reported by this endpoint' : testResult.reasoning_tokens} {testResult.content ? ` · reply: “${testResult.content.slice(0, 60)}”` : ''}

)}
)}
}
); }