You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config-provider.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import {
  8. RiEqualizer2Line,
  9. } from '@remixicon/react'
  10. import {
  11. Button,
  12. } from '@/app/components/base/button'
  13. import type {
  14. CustomConfigurationModelFixedFields,
  15. ModelProvider,
  16. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import { ConfigurationMethodEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  18. import Authorized from './authorized'
  19. import { useAuth, useCredentialStatus } from './hooks'
  20. import Tooltip from '@/app/components/base/tooltip'
  21. import cn from '@/utils/classnames'
  22. type ConfigProviderProps = {
  23. provider: ModelProvider,
  24. configurationMethod: ConfigurationMethodEnum,
  25. currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields,
  26. }
  27. const ConfigProvider = ({
  28. provider,
  29. configurationMethod,
  30. currentCustomConfigurationModelFixedFields,
  31. }: ConfigProviderProps) => {
  32. const { t } = useTranslation()
  33. const {
  34. handleOpenModal,
  35. } = useAuth(provider, configurationMethod, currentCustomConfigurationModelFixedFields)
  36. const {
  37. hasCredential,
  38. authorized,
  39. current_credential_id,
  40. current_credential_name,
  41. available_credentials,
  42. } = useCredentialStatus(provider)
  43. const notAllowCustomCredential = provider.allow_custom_token === false
  44. const handleClick = useCallback(() => {
  45. if (!hasCredential && !notAllowCustomCredential)
  46. handleOpenModal()
  47. }, [handleOpenModal, hasCredential, notAllowCustomCredential])
  48. const ButtonComponent = useMemo(() => {
  49. const Item = (
  50. <Button
  51. className={cn('grow', notAllowCustomCredential && 'cursor-not-allowed opacity-50')}
  52. size='small'
  53. onClick={handleClick}
  54. variant={!authorized ? 'secondary-accent' : 'secondary'}
  55. >
  56. <RiEqualizer2Line className='mr-1 h-3.5 w-3.5' />
  57. {t('common.operation.setup')}
  58. </Button>
  59. )
  60. if (notAllowCustomCredential) {
  61. return (
  62. <Tooltip
  63. asChild
  64. popupContent={t('plugin.auth.credentialUnavailable')}
  65. >
  66. {Item}
  67. </Tooltip>
  68. )
  69. }
  70. return Item
  71. }, [handleClick, authorized, notAllowCustomCredential, t])
  72. if (!hasCredential)
  73. return ButtonComponent
  74. return (
  75. <Authorized
  76. provider={provider}
  77. configurationMethod={ConfigurationMethodEnum.predefinedModel}
  78. items={[
  79. {
  80. title: t('common.modelProvider.auth.apiKeys'),
  81. credentials: available_credentials ?? [],
  82. },
  83. ]}
  84. selectedCredential={{
  85. credential_id: current_credential_id ?? '',
  86. credential_name: current_credential_name ?? '',
  87. }}
  88. showItemSelectedIcon
  89. />
  90. )
  91. }
  92. export default memo(ConfigProvider)