dify/web/app/components/header/account-setting/provider-page/azure-provider/index.tsx

101 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-05-15 08:51:32 +08:00
import { useTranslation } from 'react-i18next'
import Link from 'next/link'
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
import { useEffect, useState } from 'react'
2023-05-23 14:15:33 +08:00
import ProviderInput from '../provider-input'
import type { ValidatedStatusState } from '../provider-input/useValidateToken'
import useValidateToken, { ValidatedStatus } from '../provider-input/useValidateToken'
import {
ValidatedErrorIcon,
ValidatedErrorOnAzureOpenaiTip,
2023-05-23 14:15:33 +08:00
ValidatedSuccessIcon,
ValidatingTip,
} from '../provider-input/Validate'
import { ProviderName } from '@/models/common'
import type { Provider, ProviderAzureToken } from '@/models/common'
2023-05-15 08:51:32 +08:00
type IAzureProviderProps = {
2023-05-15 08:51:32 +08:00
provider: Provider
onValidatedStatus: (status?: ValidatedStatusState) => void
2023-05-15 08:51:32 +08:00
onTokenChange: (token: ProviderAzureToken) => void
}
const AzureProvider = ({
provider,
2023-05-15 08:51:32 +08:00
onTokenChange,
onValidatedStatus,
2023-05-15 08:51:32 +08:00
}: IAzureProviderProps) => {
const { t } = useTranslation()
const [token, setToken] = useState<ProviderAzureToken>(provider.provider_name === ProviderName.AZURE_OPENAI ? { ...provider.token } : {})
const [validating, validatedStatus, setValidatedStatus, validate] = useValidateToken(provider.provider_name)
2023-05-23 14:15:33 +08:00
const handleFocus = (type: keyof ProviderAzureToken) => {
if (token[type] === (provider?.token as ProviderAzureToken)[type]) {
token[type] = ''
setToken({ ...token })
onTokenChange({ ...token })
setValidatedStatus({})
2023-05-15 08:51:32 +08:00
}
}
2023-05-23 14:15:33 +08:00
const handleChange = (type: keyof ProviderAzureToken, v: string, validate: any) => {
2023-05-15 08:51:32 +08:00
token[type] = v
setToken({ ...token })
onTokenChange({ ...token })
validate({ ...token }, {
2023-05-23 14:15:33 +08:00
beforeValidating: () => {
if (!token.openai_api_base || !token.openai_api_key) {
setValidatedStatus({})
2023-05-23 14:15:33 +08:00
return false
}
return true
},
2023-05-23 14:15:33 +08:00
})
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
const getValidatedIcon = () => {
if (validatedStatus.status === ValidatedStatus.Error || validatedStatus.status === ValidatedStatus.Exceed)
2023-05-23 14:15:33 +08:00
return <ValidatedErrorIcon />
if (validatedStatus.status === ValidatedStatus.Success)
2023-05-23 14:15:33 +08:00
return <ValidatedSuccessIcon />
}
const getValidatedTip = () => {
if (validating)
2023-05-23 14:15:33 +08:00
return <ValidatingTip />
if (validatedStatus.status === ValidatedStatus.Error)
return <ValidatedErrorOnAzureOpenaiTip errorMessage={validatedStatus.message ?? ''} />
2023-05-23 14:15:33 +08:00
}
useEffect(() => {
if (typeof onValidatedStatus === 'function')
2023-05-23 14:15:33 +08:00
onValidatedStatus(validatedStatus)
}, [validatedStatus])
2023-05-15 08:51:32 +08:00
return (
<div className='px-4 py-3'>
<ProviderInput
2023-05-15 08:51:32 +08:00
className='mb-4'
name={t('common.provider.azure.apiBase')}
placeholder={t('common.provider.azure.apiBasePlaceholder')}
value={token.openai_api_base}
onChange={v => handleChange('openai_api_base', v, validate)}
2023-05-23 14:15:33 +08:00
onFocus={() => handleFocus('openai_api_base')}
validatedIcon={getValidatedIcon()}
2023-05-15 08:51:32 +08:00
/>
<ProviderInput
2023-05-15 08:51:32 +08:00
className='mb-4'
name={t('common.provider.azure.apiKey')}
placeholder={t('common.provider.azure.apiKeyPlaceholder')}
value={token.openai_api_key}
onChange={v => handleChange('openai_api_key', v, validate)}
2023-05-23 14:15:33 +08:00
onFocus={() => handleFocus('openai_api_key')}
validatedIcon={getValidatedIcon()}
validatedTip={getValidatedTip()}
2023-05-15 08:51:32 +08:00
/>
<Link className="flex items-center text-xs cursor-pointer text-primary-600" href="https://platform.openai.com/account/api-keys" target={'_blank'}>
{t('common.provider.azure.helpTip')}
<ArrowTopRightOnSquareIcon className='w-3 h-3 ml-1 text-primary-600' aria-hidden="true" />
</Link>
</div>
)
}
export default AzureProvider