index.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import type { Provider, ProviderAzureToken } from '@/models/common'
  2. import { useTranslation } from 'react-i18next'
  3. import Link from 'next/link'
  4. import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline'
  5. import ProviderInput, { ProviderValidateTokenInput} from '../provider-input'
  6. import { useState } from 'react'
  7. import { ValidatedStatus } from '../provider-input/useValidateToken'
  8. interface IAzureProviderProps {
  9. provider: Provider
  10. onValidatedStatus: (status?: ValidatedStatus) => void
  11. onTokenChange: (token: ProviderAzureToken) => void
  12. }
  13. const AzureProvider = ({
  14. provider,
  15. onTokenChange,
  16. onValidatedStatus
  17. }: IAzureProviderProps) => {
  18. const { t } = useTranslation()
  19. const [token, setToken] = useState(provider.token as ProviderAzureToken || {})
  20. const handleFocus = () => {
  21. if (token === provider.token) {
  22. token.openai_api_key = ''
  23. setToken({...token})
  24. onTokenChange({...token})
  25. }
  26. }
  27. const handleChange = (type: keyof ProviderAzureToken, v: string) => {
  28. token[type] = v
  29. setToken({...token})
  30. onTokenChange({...token})
  31. }
  32. return (
  33. <div className='px-4 py-3'>
  34. <ProviderInput
  35. className='mb-4'
  36. name={t('common.provider.azure.apiBase')}
  37. placeholder={t('common.provider.azure.apiBasePlaceholder')}
  38. value={token.openai_api_base}
  39. onChange={(v) => handleChange('openai_api_base', v)}
  40. />
  41. <ProviderValidateTokenInput
  42. className='mb-4'
  43. name={t('common.provider.azure.apiKey')}
  44. placeholder={t('common.provider.azure.apiKeyPlaceholder')}
  45. value={token.openai_api_key}
  46. onChange={v => handleChange('openai_api_key', v)}
  47. onFocus={handleFocus}
  48. onValidatedStatus={onValidatedStatus}
  49. providerName={provider.provider_name}
  50. />
  51. <Link className="flex items-center text-xs cursor-pointer text-primary-600" href="https://platform.openai.com/account/api-keys" target={'_blank'}>
  52. {t('common.provider.azure.helpTip')}
  53. <ArrowTopRightOnSquareIcon className='w-3 h-3 ml-1 text-primary-600' aria-hidden="true" />
  54. </Link>
  55. </div>
  56. )
  57. }
  58. export default AzureProvider