Your settings are stored in this browser's local storage. Do not use this app on a device that other people have access to.
import { useEffect, useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { ArrowLeft, Eye, EyeOff, Loader2, Save, Plug, Trash2, } from "lucide-react"; import { ThemeToggle } from "../components/ThemeToggle"; import { useToast } from "../lib/toast"; import { getConfigDisplay, setConfig, previewConfig, testConnectionWithConfig, saveConfig, loadConfig, DEFAULT_MODEL, } from "../lib/llm"; const DEFAULTS = { api_endpoint: "", api_key: "", model_name: DEFAULT_MODEL, }; export function SettingsPage() { const toast = useToast(); const navigate = useNavigate(); const [endpoint, setEndpoint] = useState(DEFAULTS.api_endpoint); const [model, setModel] = useState(DEFAULTS.model_name); const [apiKey, setApiKey] = useState(""); // never pre-populated from storage const [apiKeySet, setApiKeySet] = useState(false); const [showKey, setShowKey] = useState(false); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); // Load current (non-secret) values on mount. useEffect(() => { const cfg = getConfigDisplay(); setEndpoint(cfg.api_endpoint); setModel(cfg.model_name); setApiKeySet(cfg.api_key_set); setLoading(false); }, []); const onSave = (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { const result = setConfig({ api_endpoint: endpoint.trim(), api_key: apiKey, model_name: model.trim(), }); setApiKey(""); setApiKeySet(result.api_key_set); toast.success("Configuration saved"); } catch (err) { toast.error( err instanceof Error ? err.message : "Failed to save configuration", ); } finally { setSaving(false); } }; // Test using *in-memory* form values — don't persist until the user clicks // Save. An empty API key field still falls back to the previously saved key, // matching the behaviour of Save. const onTest = async () => { setTesting(true); const candidate = previewConfig({ api_endpoint: endpoint.trim(), api_key: apiKey, model_name: model.trim(), }); if ( !candidate.api_endpoint || !candidate.api_key || !candidate.model_name ) { setTesting(false); toast.error("❌ Fill in endpoint, key, and model before testing"); return; } const result = await testConnectionWithConfig(candidate); if (result.success) { toast.success(`✅ ${result.message}`); } else { toast.error(`❌ ${result.message}`); } setTesting(false); }; const onClearKey = () => { if (!apiKeySet) return; if (!window.confirm("Clear the saved API key from this browser?")) return; const current = loadConfig(); saveConfig({ ...current, api_key: "" }); setApiKeySet(false); setApiKey(""); toast.info("Saved API key cleared"); }; return (
Your settings are stored in this browser's local storage. Do not use this app on a device that other people have access to.