Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 } 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. }
  27. const SwitchCredentialInLoadBalancing = ({
  28. provider,
  29. model,
  30. customModelCredential,
  31. setCustomModelCredential,
  32. credentials,
  33. }: SwitchCredentialInLoadBalancingProps) => {
  34. const { t } = useTranslation()
  35. const handleItemClick = useCallback((credential: Credential) => {
  36. setCustomModelCredential(credential)
  37. }, [setCustomModelCredential])
  38. const renderTrigger = useCallback(() => {
  39. const selectedCredentialId = customModelCredential?.credential_id
  40. const authRemoved = !selectedCredentialId && !!credentials?.length
  41. let color = 'green'
  42. if (authRemoved && !customModelCredential?.not_allowed_to_use)
  43. color = 'red'
  44. if (customModelCredential?.not_allowed_to_use)
  45. color = 'gray'
  46. const Item = (
  47. <Button
  48. variant='secondary'
  49. className={cn(
  50. 'shrink-0 space-x-1',
  51. authRemoved && 'text-components-button-destructive-secondary-text',
  52. customModelCredential?.not_allowed_to_use && 'cursor-not-allowed opacity-50',
  53. )}
  54. >
  55. <Indicator
  56. className='mr-2'
  57. color={color as any}
  58. />
  59. {
  60. authRemoved && !customModelCredential?.not_allowed_to_use && t('common.modelProvider.auth.authRemoved')
  61. }
  62. {
  63. !authRemoved && customModelCredential?.not_allowed_to_use && t('plugin.auth.credentialUnavailable')
  64. }
  65. {
  66. !authRemoved && !customModelCredential?.not_allowed_to_use && customModelCredential?.credential_name
  67. }
  68. {
  69. customModelCredential?.from_enterprise && (
  70. <Badge className='ml-2'>Enterprise</Badge>
  71. )
  72. }
  73. <RiArrowDownSLine className='h-4 w-4' />
  74. </Button>
  75. )
  76. if (customModelCredential?.not_allowed_to_use) {
  77. return (
  78. <Tooltip
  79. asChild
  80. popupContent={t('plugin.auth.credentialUnavailable')}
  81. >
  82. {Item}
  83. </Tooltip>
  84. )
  85. }
  86. return Item
  87. }, [customModelCredential, t, credentials])
  88. return (
  89. <Authorized
  90. provider={provider}
  91. configurationMethod={ConfigurationMethodEnum.customizableModel}
  92. items={[
  93. {
  94. title: t('common.modelProvider.auth.modelCredentials'),
  95. model,
  96. credentials: credentials || [],
  97. },
  98. ]}
  99. renderTrigger={renderTrigger}
  100. onItemClick={handleItemClick}
  101. isModelCredential
  102. enableAddModelCredential
  103. bottomAddModelCredentialText={t('common.modelProvider.auth.addModelCredential')}
  104. selectedCredential={
  105. customModelCredential
  106. ? {
  107. credential_id: customModelCredential?.credential_id || '',
  108. credential_name: customModelCredential?.credential_name || '',
  109. }
  110. : undefined
  111. }
  112. showItemSelectedIcon
  113. />
  114. )
  115. }
  116. export default memo(SwitchCredentialInLoadBalancing)