import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Link from 'next/link' import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline' import ProviderInput from '../provider-input' import type { ValidatedStatusState } from '../provider-input/useValidateToken' import useValidateToken, { ValidatedStatus } from '../provider-input/useValidateToken' import { ValidatedErrorIcon, ValidatedErrorOnOpenaiTip, ValidatedSuccessIcon, ValidatingTip, } from '../provider-input/Validate' import type { Provider, ProviderAnthropicToken } from '@/models/common' type AnthropicProviderProps = { provider: Provider onValidatedStatus: (status?: ValidatedStatusState) => void onTokenChange: (token: ProviderAnthropicToken) => void } const AnthropicProvider = ({ provider, onValidatedStatus, onTokenChange, }: AnthropicProviderProps) => { const { t } = useTranslation() const [token, setToken] = useState((provider.token as ProviderAnthropicToken) || { anthropic_api_key: '' }) const [validating, validatedStatus, setValidatedStatus, validate] = useValidateToken(provider.provider_name) const handleFocus = () => { if (token.anthropic_api_key === (provider.token as ProviderAnthropicToken).anthropic_api_key) { setToken({ anthropic_api_key: '' }) onTokenChange({ anthropic_api_key: '' }) setValidatedStatus({}) } } const handleChange = (v: string) => { const apiKey = { anthropic_api_key: v } setToken(apiKey) onTokenChange(apiKey) validate(apiKey, { beforeValidating: () => { if (!v) { setValidatedStatus({}) return false } return true }, }) } useEffect(() => { if (typeof onValidatedStatus === 'function') onValidatedStatus(validatedStatus) }, [validatedStatus]) const getValidatedIcon = () => { if (validatedStatus?.status === ValidatedStatus.Error || validatedStatus.status === ValidatedStatus.Exceed) return if (validatedStatus.status === ValidatedStatus.Success) return } const getValidatedTip = () => { if (validating) return if (validatedStatus?.status === ValidatedStatus.Error) return } return (
{t('common.provider.anthropic.keyFrom')}
) } export default AnthropicProvider