index.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import type { Provider } from '@/models/common'
  2. import { useState, useEffect } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import ProviderInput from '../provider-input'
  5. import Link from 'next/link'
  6. import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
  7. import useValidateToken, { ValidatedStatus, ValidatedStatusState } from '../provider-input/useValidateToken'
  8. import {
  9. ValidatedErrorIcon,
  10. ValidatedSuccessIcon,
  11. ValidatingTip,
  12. ValidatedExceedOnOpenaiTip,
  13. ValidatedErrorOnOpenaiTip
  14. } from '../provider-input/Validate'
  15. interface IOpenaiProviderProps {
  16. provider: Provider
  17. onValidatedStatus: (status?: ValidatedStatusState) => void
  18. onTokenChange: (token: string) => void
  19. }
  20. const OpenaiProvider = ({
  21. provider,
  22. onValidatedStatus,
  23. onTokenChange
  24. }: IOpenaiProviderProps) => {
  25. const { t } = useTranslation()
  26. const [token, setToken] = useState(provider.token as string || '')
  27. const [ validating, validatedStatus, setValidatedStatus, validate ] = useValidateToken(provider.provider_name)
  28. const handleFocus = () => {
  29. if (token === provider.token) {
  30. setToken('')
  31. onTokenChange('')
  32. setValidatedStatus({})
  33. }
  34. }
  35. const handleChange = (v: string) => {
  36. setToken(v)
  37. onTokenChange(v)
  38. validate(v, {
  39. beforeValidating: () => {
  40. if (!v) {
  41. setValidatedStatus({})
  42. return false
  43. }
  44. return true
  45. }
  46. })
  47. }
  48. useEffect(() => {
  49. if (typeof onValidatedStatus === 'function') {
  50. onValidatedStatus(validatedStatus)
  51. }
  52. }, [validatedStatus])
  53. const getValidatedIcon = () => {
  54. if (validatedStatus?.status === ValidatedStatus.Error || validatedStatus.status === ValidatedStatus.Exceed) {
  55. return <ValidatedErrorIcon />
  56. }
  57. if (validatedStatus.status === ValidatedStatus.Success) {
  58. return <ValidatedSuccessIcon />
  59. }
  60. }
  61. const getValidatedTip = () => {
  62. if (validating) {
  63. return <ValidatingTip />
  64. }
  65. if (validatedStatus?.status === ValidatedStatus.Success) {
  66. return <ValidatedExceedOnOpenaiTip />
  67. }
  68. if (validatedStatus?.status === ValidatedStatus.Error) {
  69. return <ValidatedErrorOnOpenaiTip errorMessage={validatedStatus.message ?? ''} />
  70. }
  71. }
  72. return (
  73. <div className='px-4 pt-3 pb-4'>
  74. <ProviderInput
  75. value={token}
  76. name={t('common.provider.apiKey')}
  77. placeholder={t('common.provider.enterYourKey')}
  78. onChange={handleChange}
  79. onFocus={handleFocus}
  80. validatedIcon={getValidatedIcon()}
  81. validatedTip={getValidatedTip()}
  82. />
  83. <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'}>
  84. {t('appOverview.welcome.getKeyTip')}
  85. <ArrowTopRightOnSquareIcon className='w-3 h-3 ml-1 text-primary-600' aria-hidden="true" />
  86. </Link>
  87. </div>
  88. )
  89. }
  90. export default OpenaiProvider