'use client'; import { type FC, useState, useCallback } from 'react'; import { type AIProvider, type ConnectionStatus, getProviderColorClasses, } from '@/lib/infrastructure'; interface APIKeyInputProps { provider: AIProvider; value: string; onChange: (value: string) => void; onValidate?: () => Promise; status?: ConnectionStatus; errorMessage?: string; } /** * API key input component with validation * Used for provider configuration */ export const APIKeyInput: FC = ({ provider, value, onChange, onValidate, status, errorMessage, }) => { const [showKey, setShowKey] = useState(false); const [isValidating, setIsValidating] = useState(false); const colors = getProviderColorClasses(provider.color); const isOllama = provider.id === 'ollama'; const inputType = isOllama ? 'url' : showKey ? 'text' : 'password'; const inputLabel = isOllama ? 'Ollama Server URL' : `${provider.name} API Key`; const handleValidate = useCallback(async () => { if (!onValidate || !value) return; setIsValidating(true); try { await onValidate(); } finally { setIsValidating(false); } }, [onValidate, value]); const getStatusIcon = () => { switch (status) { case 'connected': return ( ); case 'error': return ( ); case 'validating': return ( ); default: return null; } }; return (
onChange(e.target.value)} placeholder={provider.keyPlaceholder} className={`w-full px-4 py-3 pr-24 border rounded-xl focus:outline-none focus:ring-2 transition-colors ${ status === 'error' ? 'border-red-300 focus:ring-red-500' : status === 'connected' ? 'border-green-300 focus:ring-green-500' : `border-gray-200 ${colors.focusRing}` }`} />
{/* Status indicator */} {status && getStatusIcon()} {/* Show/hide toggle for API keys (not for URLs) */} {!isOllama && value && ( )}
{/* Error message */} {status === 'error' && errorMessage &&

{errorMessage}

} {/* Help text and validate button */}
Get {isOllama ? 'Ollama' : 'API key'} → {onValidate && value && status !== 'connected' && ( )}
); };