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.

switch-credential-in-load-balancing.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import type { Dispatch, SetStateAction } from 'react'
  2. import {
  3. memo,
  4. useCallback,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { RiArrowDownSLine } from '@remixicon/react'
  8. import Button from '@/app/components/base/button'
  9. import Indicator from '@/app/components/header/indicator'
  10. import Authorized from './authorized'
  11. import type {
  12. Credential,
  13. CustomModel,
  14. ModelProvider,
  15. } from '../declarations'
  16. import { ConfigurationMethodEnum, ModelModalModeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import cn from '@/utils/classnames'
  18. import Tooltip from '@/app/components/base/tooltip'
  19. import Badge from '@/app/components/base/badge'
  20. type SwitchCredentialInLoadBalancingProps = {
  21. provider: ModelProvider
  22. model: CustomModel
  23. credentials?: Credential[]
  24. customModelCredential?: Credential
  25. setCustomModelCredential: Dispatch<SetStateAction<Credential | undefined>>
  26. onUpdate?: (payload?: any, formValues?: Record<string, any>) => void
  27. onRemove?: (credentialId: string) => void
  28. }
  29. const SwitchCredentialInLoadBalancing = ({
  30. provider,
  31. model,
  32. customModelCredential,
  33. setCustomModelCredential,
  34. credentials,
  35. onUpdate,
  36. onRemove,
  37. }: SwitchCredentialInLoadBalancingProps) => {
  38. const { t } = useTranslation()
  39. const handleItemClick = useCallback((credential: Credential) => {
  40. setCustomModelCredential(credential)
  41. }, [setCustomModelCredential])
  42. const renderTrigger = useCallback(() => {
  43. const selectedCredentialId = customModelCredential?.credential_id
  44. const authRemoved = !selectedCredentialId && !!credentials?.length
  45. let color = 'green'
  46. if (authRemoved && !customModelCredential?.not_allowed_to_use)
  47. color = 'red'
  48. if (customModelCredential?.not_allowed_to_use)
  49. color = 'gray'
  50. const Item = (
  51. <Button
  52. variant='secondary'
  53. className={cn(
  54. 'shrink-0 space-x-1',
  55. authRemoved && 'text-components-button-destructive-secondary-text',
  56. customModelCredential?.not_allowed_to_use && 'cursor-not-allowed opacity-50',
  57. )}
  58. >
  59. <Indicator
  60. className='mr-2'
  61. color={color as any}
  62. />
  63. {
  64. authRemoved && !customModelCredential?.not_allowed_to_use && t('common.modelProvider.auth.authRemoved')
  65. }
  66. {
  67. !authRemoved && customModelCredential?.not_allowed_to_use && t('plugin.auth.credentialUnavailable')
  68. }
  69. {
  70. !authRemoved && !customModelCredential?.not_allowed_to_use && customModelCredential?.credential_name
  71. }
  72. {
  73. customModelCredential?.from_enterprise && (
  74. <Badge className='ml-2'>Enterprise</Badge>
  75. )
  76. }
  77. <RiArrowDownSLine className='h-4 w-4' />
  78. </Button>
  79. )
  80. if (customModelCredential?.not_allowed_to_use) {
  81. return (
  82. <Tooltip
  83. asChild
  84. popupContent={t('plugin.auth.credentialUnavailable')}
  85. >
  86. {Item}
  87. </Tooltip>
  88. )
  89. }
  90. return Item
  91. }, [customModelCredential, t, credentials])
  92. return (
  93. <Authorized
  94. provider={provider}
  95. configurationMethod={ConfigurationMethodEnum.customizableModel}
  96. currentCustomConfigurationModelFixedFields={model ? {
  97. __model_name: model.model,
  98. __model_type: model.model_type,
  99. } : undefined}
  100. authParams={{
  101. isModelCredential: true,
  102. mode: ModelModalModeEnum.configModelCredential,
  103. onUpdate,
  104. onRemove,
  105. }}
  106. items={[
  107. {
  108. model,
  109. credentials: credentials || [],
  110. selectedCredential: customModelCredential ? {
  111. credential_id: customModelCredential?.credential_id || '',
  112. credential_name: customModelCredential?.credential_name || '',
  113. } : undefined,
  114. },
  115. ]}
  116. renderTrigger={renderTrigger}
  117. onItemClick={handleItemClick}
  118. enableAddModelCredential
  119. showItemSelectedIcon
  120. popupTitle={t('common.modelProvider.auth.modelCredentials')}
  121. />
  122. )
  123. }
  124. export default memo(SwitchCredentialInLoadBalancing)