provider.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import type { Provider } from '@/models/common'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { ProviderValidateTokenInput } from '../provider-input'
  5. import Link from 'next/link'
  6. import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
  7. import { ValidatedStatus } from '../provider-input/useValidateToken'
  8. interface IOpenaiProviderProps {
  9. provider: Provider
  10. onValidatedStatus: (status?: ValidatedStatus) => void
  11. onTokenChange: (token: string) => void
  12. }
  13. const OpenaiProvider = ({
  14. provider,
  15. onValidatedStatus,
  16. onTokenChange
  17. }: IOpenaiProviderProps) => {
  18. const { t } = useTranslation()
  19. const [token, setToken] = useState(provider.token as string || '')
  20. const handleFocus = () => {
  21. if (token === provider.token) {
  22. setToken('')
  23. onTokenChange('')
  24. }
  25. }
  26. const handleChange = (v: string) => {
  27. setToken(v)
  28. onTokenChange(v)
  29. }
  30. return (
  31. <div className='px-4 pt-3 pb-4'>
  32. <ProviderValidateTokenInput
  33. value={token}
  34. name={t('common.provider.apiKey')}
  35. placeholder={t('common.provider.enterYourKey')}
  36. onChange={handleChange}
  37. onFocus={handleFocus}
  38. onValidatedStatus={onValidatedStatus}
  39. providerName={provider.provider_name}
  40. />
  41. <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'}>
  42. {t('appOverview.welcome.getKeyTip')}
  43. <ArrowTopRightOnSquareIcon className='w-3 h-3 ml-1 text-primary-600' aria-hidden="true" />
  44. </Link>
  45. </div>
  46. )
  47. }
  48. export default OpenaiProvider