import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Spinner } from "@/components/ui/spinner"; import { cn } from "@/lib/utils"; import type { ProviderInfo } from "@/modules/ai/config"; import { Cancel01Icon, CheckmarkCircle02Icon, Edit02Icon, ViewIcon, ViewOffSlashIcon, } from "@hugeicons/core-free-icons"; import { HugeiconsIcon } from "@hugeicons/react"; import { openUrl } from "@tauri-apps/plugin-opener"; import { useEffect, useState } from "react"; import { ProviderIcon } from "./ProviderIcon"; type Props = { provider: ProviderInfo; currentKey: string | null; onSave: (key: string) => Promise; onClear: () => Promise; }; function maskKey(key: string): string { if (key.length <= 8) return "•".repeat(key.length); return `${key.slice(0, 4)}${"•".repeat(8)}${key.slice(-4)}`; } export function ProviderKeyCard({ provider, currentKey, onSave, onClear, }: Props) { const [editing, setEditing] = useState(!currentKey); const [value, setValue] = useState(""); const [reveal, setReveal] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); useEffect(() => { setEditing(!currentKey); }, [currentKey]); const submit = async () => { const trimmed = value.trim(); if (!trimmed) { setError("Enter your API key."); return; } if (provider.keyPrefix && !trimmed.startsWith(provider.keyPrefix)) { setError(`${provider.label} keys start with "${provider.keyPrefix}".`); return; } setSaving(true); setError(null); try { await onSave(trimmed); setValue(""); setReveal(false); } catch (e) { setError(`Failed to save: ${String(e)}`); } finally { setSaving(false); } }; return (
{provider.label} {currentKey ? ( Configured ) : null}
{editing ? (
{ setValue(e.target.value); if (error) setError(null); }} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); void submit(); } else if (e.key === "Escape" && currentKey) { setValue(""); setReveal(false); setError(null); setEditing(false); } }} className="h-8 pr-7 font-mono text-[11.5px]" />
{error ? (

{error}

) : null}
) : (
{maskKey(currentKey ?? "")}
)}
); }