import { ChangeEvent, useEffect } from 'react' import Link from 'next/link' import { CheckCircleIcon, ExclamationCircleIcon } from '@heroicons/react/24/solid' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import I18n from '@/context/i18n' import useValidateToken, { ValidatedStatus } from './useValidateToken' interface IProviderInputProps { value?: string name: string placeholder: string className?: string onChange: (v: string) => void onFocus?: () => void } const ProviderInput = ({ value, name, placeholder, className, onChange, onFocus, }: IProviderInputProps) => { const handleChange = (e: ChangeEvent) => { const inputValue = e.target.value onChange(inputValue) } return (
{name}
) } type TproviderInputProps = IProviderInputProps & { onValidatedStatus?: (status?: ValidatedStatus) => void providerName: string } export const ProviderValidateTokenInput = ({ value, name, placeholder, className, onChange, onFocus, onValidatedStatus, providerName }: TproviderInputProps) => { const { t } = useTranslation() const { locale } = useContext(I18n) const [ validating, validatedStatus, validate ] = useValidateToken(providerName) useEffect(() => { if (typeof onValidatedStatus === 'function') { onValidatedStatus(validatedStatus) } }, [validatedStatus]) const handleChange = (e: ChangeEvent) => { const inputValue = e.target.value onChange(inputValue) validate(inputValue) } return (
{name}
{ validatedStatus === ValidatedStatus.Error && } { validatedStatus === ValidatedStatus.Success && }
{ validating && (
{t('common.provider.validating')}
) } { validatedStatus === ValidatedStatus.Exceed && !validating && (
{t('common.provider.apiKeyExceedBill')}  {locale === 'en' ? 'this link' : '这篇文档'}
) } { validatedStatus === ValidatedStatus.Error && !validating && (
{t('common.provider.invalidKey')}
) }
) } export default ProviderInput