您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

add-credential-in-load-balancing.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. } from 'react'
  6. import { RiAddLine } from '@remixicon/react'
  7. import { useTranslation } from 'react-i18next'
  8. import { Authorized } from '@/app/components/header/account-setting/model-provider-page/model-auth'
  9. import cn from '@/utils/classnames'
  10. import type {
  11. Credential,
  12. CustomModelCredential,
  13. ModelCredential,
  14. ModelProvider,
  15. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  16. import { ConfigurationMethodEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. type AddCredentialInLoadBalancingProps = {
  19. provider: ModelProvider
  20. model: CustomModelCredential
  21. configurationMethod: ConfigurationMethodEnum
  22. modelCredential: ModelCredential
  23. onSelectCredential: (credential: Credential) => void
  24. onUpdate?: () => void
  25. }
  26. const AddCredentialInLoadBalancing = ({
  27. provider,
  28. model,
  29. configurationMethod,
  30. modelCredential,
  31. onSelectCredential,
  32. onUpdate,
  33. }: AddCredentialInLoadBalancingProps) => {
  34. const { t } = useTranslation()
  35. const {
  36. available_credentials,
  37. } = modelCredential
  38. const customModel = configurationMethod === ConfigurationMethodEnum.customizableModel
  39. const notAllowCustomCredential = provider.allow_custom_token === false
  40. const ButtonComponent = useMemo(() => {
  41. const Item = (
  42. <div className={cn(
  43. 'system-sm-medium flex h-8 items-center rounded-lg px-3 text-text-accent hover:bg-state-base-hover',
  44. notAllowCustomCredential && 'cursor-not-allowed opacity-50',
  45. )}>
  46. <RiAddLine className='mr-2 h-4 w-4' />
  47. {
  48. customModel
  49. ? t('common.modelProvider.auth.addCredential')
  50. : t('common.modelProvider.auth.addApiKey')
  51. }
  52. </div>
  53. )
  54. if (notAllowCustomCredential) {
  55. return (
  56. <Tooltip
  57. asChild
  58. popupContent={t('plugin.auth.credentialUnavailable')}
  59. >
  60. {Item}
  61. </Tooltip>
  62. )
  63. }
  64. return Item
  65. }, [notAllowCustomCredential, t, customModel])
  66. const renderTrigger = useCallback((open?: boolean) => {
  67. const Item = (
  68. <div className={cn(
  69. 'system-sm-medium flex h-8 items-center rounded-lg px-3 text-text-accent hover:bg-state-base-hover',
  70. open && 'bg-state-base-hover',
  71. )}>
  72. <RiAddLine className='mr-2 h-4 w-4' />
  73. {
  74. customModel
  75. ? t('common.modelProvider.auth.addCredential')
  76. : t('common.modelProvider.auth.addApiKey')
  77. }
  78. </div>
  79. )
  80. return Item
  81. }, [t, customModel])
  82. if (!available_credentials?.length)
  83. return ButtonComponent
  84. return (
  85. <Authorized
  86. provider={provider}
  87. renderTrigger={renderTrigger}
  88. items={[
  89. {
  90. title: customModel ? t('common.modelProvider.auth.modelCredentials') : t('common.modelProvider.auth.apiKeys'),
  91. model: customModel ? model : undefined,
  92. credentials: available_credentials ?? [],
  93. },
  94. ]}
  95. configurationMethod={configurationMethod}
  96. currentCustomConfigurationModelFixedFields={customModel ? {
  97. __model_name: model.model,
  98. __model_type: model.model_type,
  99. } : undefined}
  100. onItemClick={onSelectCredential}
  101. placement='bottom-start'
  102. onUpdate={onUpdate}
  103. isModelCredential={customModel}
  104. />
  105. )
  106. }
  107. export default memo(AddCredentialInLoadBalancing)