95 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-05-23 14:15:33 +08:00
import type { Provider } from '@/models/common'
import { useState, useEffect } from 'react'
2023-05-15 08:51:32 +08:00
import { useTranslation } from 'react-i18next'
2023-05-23 14:15:33 +08:00
import ProviderInput from '../provider-input'
2023-05-15 08:51:32 +08:00
import Link from 'next/link'
2023-05-23 14:15:33 +08:00
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
import useValidateToken, { ValidatedStatus } from '../provider-input/useValidateToken'
import {
ValidatedErrorIcon,
ValidatedSuccessIcon,
ValidatingTip,
ValidatedExceedOnOpenaiTip,
ValidatedErrorOnOpenaiTip
} from '../provider-input/Validate'
2023-05-15 08:51:32 +08:00
2023-05-23 14:15:33 +08:00
interface IOpenaiProviderProps {
provider: Provider
onValidatedStatus: (status?: ValidatedStatus) => void
onTokenChange: (token: string) => void
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
const OpenaiProvider = ({
provider,
onValidatedStatus,
onTokenChange
}: IOpenaiProviderProps) => {
2023-05-15 08:51:32 +08:00
const { t } = useTranslation()
2023-05-23 14:15:33 +08:00
const [token, setToken] = useState(provider.token as string || '')
const [ validating, validatedStatus, setValidatedStatus, validate ] = useValidateToken(provider.provider_name)
const handleFocus = () => {
if (token === provider.token) {
setToken('')
onTokenChange('')
setValidatedStatus(undefined)
}
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
const handleChange = (v: string) => {
setToken(v)
onTokenChange(v)
validate(v, {
beforeValidating: () => {
if (!v) {
setValidatedStatus(undefined)
return false
}
return true
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
})
2023-05-15 08:51:32 +08:00
}
useEffect(() => {
2023-05-23 14:15:33 +08:00
if (typeof onValidatedStatus === 'function') {
onValidatedStatus(validatedStatus)
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
}, [validatedStatus])
2023-05-15 08:51:32 +08:00
2023-05-23 14:15:33 +08:00
const getValidatedIcon = () => {
if (validatedStatus === ValidatedStatus.Error || validatedStatus === ValidatedStatus.Exceed) {
return <ValidatedErrorIcon />
}
if (validatedStatus === ValidatedStatus.Success) {
return <ValidatedSuccessIcon />
}
}
const getValidatedTip = () => {
2023-05-15 08:51:32 +08:00
if (validating) {
2023-05-23 14:15:33 +08:00
return <ValidatingTip />
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
if (validatedStatus === ValidatedStatus.Exceed) {
return <ValidatedExceedOnOpenaiTip />
2023-05-15 08:51:32 +08:00
}
2023-05-23 14:15:33 +08:00
if (validatedStatus === ValidatedStatus.Error) {
return <ValidatedErrorOnOpenaiTip />
2023-05-15 08:51:32 +08:00
}
}
return (
<div className='px-4 pt-3 pb-4'>
2023-05-23 14:15:33 +08:00
<ProviderInput
value={token}
name={t('common.provider.apiKey')}
placeholder={t('common.provider.enterYourKey')}
onChange={handleChange}
onFocus={handleFocus}
validatedIcon={getValidatedIcon()}
validatedTip={getValidatedTip()}
/>
<Link className="inline-flex items-center mt-3 text-xs font-normal cursor-pointer text-primary-600 w-fit" href="https://platform.openai.com/account/api-keys" target={'_blank'}>
{t('appOverview.welcome.getKeyTip')}
<ArrowTopRightOnSquareIcon className='w-3 h-3 ml-1 text-primary-600' aria-hidden="true" />
</Link>
</div>
2023-05-15 08:51:32 +08:00
)
}
export default OpenaiProvider