index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { useTranslation } from 'react-i18next'
  2. import Link from 'next/link'
  3. import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
  4. import { useEffect, useState } from 'react'
  5. import ProviderInput from '../provider-input'
  6. import type { ValidatedStatusState } from '../provider-input/useValidateToken'
  7. import useValidateToken, { ValidatedStatus } from '../provider-input/useValidateToken'
  8. import {
  9. ValidatedErrorIcon,
  10. ValidatedErrorOnAzureOpenaiTip,
  11. ValidatedSuccessIcon,
  12. ValidatingTip,
  13. } from '../provider-input/Validate'
  14. import { ProviderName } from '@/models/common'
  15. import type { Provider, ProviderAzureToken } from '@/models/common'
  16. type IAzureProviderProps = {
  17. provider: Provider
  18. onValidatedStatus: (status?: ValidatedStatusState) => void
  19. onTokenChange: (token: ProviderAzureToken) => void
  20. }
  21. const AzureProvider = ({
  22. provider,
  23. onTokenChange,
  24. onValidatedStatus,
  25. }: IAzureProviderProps) => {
  26. const { t } = useTranslation()
  27. const [token, setToken] = useState<ProviderAzureToken>(provider.provider_name === ProviderName.AZURE_OPENAI ? { ...provider.token } : {})
  28. const [validating, validatedStatus, setValidatedStatus, validate] = useValidateToken(provider.provider_name)
  29. const handleFocus = (type: keyof ProviderAzureToken) => {
  30. if (token[type] === (provider?.token as ProviderAzureToken)[type]) {
  31. token[type] = ''
  32. setToken({ ...token })
  33. onTokenChange({ ...token })
  34. setValidatedStatus({})
  35. }
  36. }
  37. const handleChange = (type: keyof ProviderAzureToken, v: string, validate: any) => {
  38. token[type] = v
  39. setToken({ ...token })
  40. onTokenChange({ ...token })
  41. validate({ ...token }, {
  42. beforeValidating: () => {
  43. if (!token.openai_api_base || !token.openai_api_key) {
  44. setValidatedStatus({})
  45. return false
  46. }
  47. return true
  48. },
  49. })
  50. }
  51. const getValidatedIcon = () => {
  52. if (validatedStatus.status === ValidatedStatus.Error || validatedStatus.status === ValidatedStatus.Exceed)
  53. return <ValidatedErrorIcon />
  54. if (validatedStatus.status === ValidatedStatus.Success)
  55. return <ValidatedSuccessIcon />
  56. }
  57. const getValidatedTip = () => {
  58. if (validating)
  59. return <ValidatingTip />
  60. if (validatedStatus.status === ValidatedStatus.Error)
  61. return <ValidatedErrorOnAzureOpenaiTip errorMessage={validatedStatus.message ?? ''} />
  62. }
  63. useEffect(() => {
  64. if (typeof onValidatedStatus === 'function')
  65. onValidatedStatus(validatedStatus)
  66. }, [validatedStatus])
  67. return (
  68. <div className='px-4 py-3'>
  69. <ProviderInput
  70. className='mb-4'
  71. name={t('common.provider.azure.apiBase')}
  72. placeholder={t('common.provider.azure.apiBasePlaceholder')}
  73. value={token.openai_api_base}
  74. onChange={v => handleChange('openai_api_base', v, validate)}
  75. onFocus={() => handleFocus('openai_api_base')}
  76. validatedIcon={getValidatedIcon()}
  77. />
  78. <ProviderInput
  79. className='mb-4'
  80. name={t('common.provider.azure.apiKey')}
  81. placeholder={t('common.provider.azure.apiKeyPlaceholder')}
  82. value={token.openai_api_key}
  83. onChange={v => handleChange('openai_api_key', v, validate)}
  84. onFocus={() => handleFocus('openai_api_key')}
  85. validatedIcon={getValidatedIcon()}
  86. validatedTip={getValidatedTip()}
  87. />
  88. <Link className="flex items-center text-xs cursor-pointer text-primary-600" href="https://platform.openai.com/account/api-keys" target={'_blank'}>
  89. {t('common.provider.azure.helpTip')}
  90. <ArrowTopRightOnSquareIcon className='w-3 h-3 ml-1 text-primary-600' aria-hidden="true" />
  91. </Link>
  92. </div>
  93. )
  94. }
  95. export default AzureProvider