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.5KB

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