90 lines
2.8 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react'
2023-05-15 08:51:32 +08:00
import { useTranslation } from 'react-i18next'
import Link from 'next/link'
2023-05-23 14:15:33 +08:00
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,
2023-05-23 14:15:33 +08:00
ValidatedSuccessIcon,
ValidatingTip,
} from '../provider-input/Validate'
import type { Provider } from '@/models/common'
2023-05-15 08:51:32 +08:00
type IOpenaiProviderProps = {
2023-05-23 14:15:33 +08:00
provider: Provider
onValidatedStatus: (status?: ValidatedStatusState) => void
2023-05-23 14:15:33 +08:00
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,
2023-05-23 14:15:33 +08:00
}: 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)
2023-05-23 14:15:33 +08:00
const handleFocus = () => {
if (token === provider.token) {
setToken('')
onTokenChange('')
setValidatedStatus({})
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 handleChange = (v: string) => {
setToken(v)
onTokenChange(v)
validate(v, {
beforeValidating: () => {
if (!v) {
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
}
useEffect(() => {
if (typeof onValidatedStatus === 'function')
2023-05-23 14:15:33 +08:00
onValidatedStatus(validatedStatus)
}, [validatedStatus])
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 <ValidatedErrorOnOpenaiTip errorMessage={validatedStatus.message ?? ''} />
2023-05-15 08:51:32 +08:00
}
return (
<div className='px-4 pt-3 pb-4'>
<ProviderInput
2023-05-23 14:15:33 +08:00
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