Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

plugin-auth-in-agent.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. memo,
  3. useCallback,
  4. useState,
  5. } from 'react'
  6. import { RiArrowDownSLine } from '@remixicon/react'
  7. import { useTranslation } from 'react-i18next'
  8. import Authorize from './authorize'
  9. import Authorized from './authorized'
  10. import type {
  11. Credential,
  12. PluginPayload,
  13. } from './types'
  14. import { usePluginAuth } from './hooks/use-plugin-auth'
  15. import Button from '@/app/components/base/button'
  16. import Indicator from '@/app/components/header/indicator'
  17. import cn from '@/utils/classnames'
  18. type PluginAuthInAgentProps = {
  19. pluginPayload: PluginPayload
  20. credentialId?: string
  21. onAuthorizationItemClick?: (id: string) => void
  22. }
  23. const PluginAuthInAgent = ({
  24. pluginPayload,
  25. credentialId,
  26. onAuthorizationItemClick,
  27. }: PluginAuthInAgentProps) => {
  28. const { t } = useTranslation()
  29. const [isOpen, setIsOpen] = useState(false)
  30. const {
  31. isAuthorized,
  32. canOAuth,
  33. canApiKey,
  34. credentials,
  35. disabled,
  36. invalidPluginCredentialInfo,
  37. } = usePluginAuth(pluginPayload, true)
  38. const extraAuthorizationItems: Credential[] = [
  39. {
  40. id: '__workspace_default__',
  41. name: t('plugin.auth.workspaceDefault'),
  42. provider: '',
  43. is_default: !credentialId,
  44. isWorkspaceDefault: true,
  45. },
  46. ]
  47. const handleAuthorizationItemClick = useCallback((id: string) => {
  48. onAuthorizationItemClick?.(id)
  49. setIsOpen(false)
  50. }, [
  51. onAuthorizationItemClick,
  52. setIsOpen,
  53. ])
  54. const renderTrigger = useCallback((isOpen?: boolean) => {
  55. let label = ''
  56. let removed = false
  57. if (!credentialId) {
  58. label = t('plugin.auth.workspaceDefault')
  59. }
  60. else {
  61. const credential = credentials.find(c => c.id === credentialId)
  62. label = credential ? credential.name : t('plugin.auth.authRemoved')
  63. removed = !credential
  64. }
  65. return (
  66. <Button
  67. className={cn(
  68. 'w-full',
  69. isOpen && 'bg-components-button-secondary-bg-hover',
  70. removed && 'text-text-destructive',
  71. )}>
  72. <Indicator
  73. className='mr-2'
  74. color={removed ? 'red' : 'green'}
  75. />
  76. {label}
  77. <RiArrowDownSLine className='ml-0.5 h-4 w-4' />
  78. </Button>
  79. )
  80. }, [credentialId, credentials, t])
  81. return (
  82. <>
  83. {
  84. !isAuthorized && (
  85. <Authorize
  86. pluginPayload={pluginPayload}
  87. canOAuth={canOAuth}
  88. canApiKey={canApiKey}
  89. disabled={disabled}
  90. onUpdate={invalidPluginCredentialInfo}
  91. />
  92. )
  93. }
  94. {
  95. isAuthorized && (
  96. <Authorized
  97. pluginPayload={pluginPayload}
  98. credentials={credentials}
  99. canOAuth={canOAuth}
  100. canApiKey={canApiKey}
  101. disabled={disabled}
  102. disableSetDefault
  103. onItemClick={handleAuthorizationItemClick}
  104. extraAuthorizationItems={extraAuthorizationItems}
  105. showItemSelectedIcon
  106. renderTrigger={renderTrigger}
  107. isOpen={isOpen}
  108. onOpenChange={setIsOpen}
  109. selectedCredentialId={credentialId || '__workspace_default__'}
  110. onUpdate={invalidPluginCredentialInfo}
  111. />
  112. )
  113. }
  114. </>
  115. )
  116. }
  117. export default memo(PluginAuthInAgent)