import { Input } from '@/components/ui/input' import { useLocalApiServer } from '@/hooks/useLocalApiServer' import { useState, useEffect, useCallback } from 'react' import { Eye, EyeOff } from 'lucide-react' import { useTranslation } from '@/i18n/react-i18next-compat' interface ApiKeyInputProps { showError?: boolean onValidationChange?: (isValid: boolean) => void } export function ApiKeyInput({ showError = false, onValidationChange, }: ApiKeyInputProps) { const { apiKey, setApiKey } = useLocalApiServer() const [inputValue, setInputValue] = useState(apiKey.toString()) const [showPassword, setShowPassword] = useState(false) const [error, setError] = useState('') const { t } = useTranslation() const validateApiKey = useCallback((value: string) => { if (!value || value.trim().length === 0) { setError(t('common:apiKeyRequired')) onValidationChange?.(false) return false } setError('') onValidationChange?.(true) return true }, [onValidationChange, t]) useEffect(() => { if (showError) { validateApiKey(inputValue) } }, [showError, inputValue, validateApiKey]) const handleChange = (e: React.ChangeEvent) => { const value = e.target.value setInputValue(value) // Clear error when user starts typing if (error && value.trim().length > 0) { setError('') onValidationChange?.(true) } } const handleBlur = () => { setApiKey(inputValue) // Validate on blur if showError is true if (showError) { validateApiKey(inputValue) } } const hasError = error && showError return (
{hasError && (

{error}

)}
) }