index.tsx 3.6 KB

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