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.

index.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import AddOAuthButton from './add-oauth-button'
  7. import type { AddOAuthButtonProps } from './add-oauth-button'
  8. import AddApiKeyButton from './add-api-key-button'
  9. import type { AddApiKeyButtonProps } from './add-api-key-button'
  10. import type { PluginPayload } from '../types'
  11. import cn from '@/utils/classnames'
  12. import Tooltip from '@/app/components/base/tooltip'
  13. type AuthorizeProps = {
  14. pluginPayload: PluginPayload
  15. theme?: 'primary' | 'secondary'
  16. showDivider?: boolean
  17. canOAuth?: boolean
  18. canApiKey?: boolean
  19. disabled?: boolean
  20. onUpdate?: () => void
  21. notAllowCustomCredential?: boolean
  22. }
  23. const Authorize = ({
  24. pluginPayload,
  25. theme = 'primary',
  26. showDivider = true,
  27. canOAuth,
  28. canApiKey,
  29. disabled,
  30. onUpdate,
  31. notAllowCustomCredential,
  32. }: AuthorizeProps) => {
  33. const { t } = useTranslation()
  34. const oAuthButtonProps: AddOAuthButtonProps = useMemo(() => {
  35. if (theme === 'secondary') {
  36. return {
  37. buttonText: !canApiKey ? t('plugin.auth.useOAuthAuth') : t('plugin.auth.addOAuth'),
  38. buttonVariant: 'secondary',
  39. className: 'hover:bg-components-button-secondary-bg',
  40. buttonLeftClassName: 'hover:bg-components-button-secondary-bg-hover',
  41. buttonRightClassName: 'hover:bg-components-button-secondary-bg-hover',
  42. dividerClassName: 'bg-divider-regular opacity-100',
  43. pluginPayload,
  44. }
  45. }
  46. return {
  47. buttonText: !canApiKey ? t('plugin.auth.useOAuthAuth') : t('plugin.auth.addOAuth'),
  48. pluginPayload,
  49. }
  50. }, [canApiKey, theme, pluginPayload, t])
  51. const apiKeyButtonProps: AddApiKeyButtonProps = useMemo(() => {
  52. if (theme === 'secondary') {
  53. return {
  54. pluginPayload,
  55. buttonVariant: 'secondary',
  56. buttonText: !canOAuth ? t('plugin.auth.useApiAuth') : t('plugin.auth.addApi'),
  57. }
  58. }
  59. return {
  60. pluginPayload,
  61. buttonText: !canOAuth ? t('plugin.auth.useApiAuth') : t('plugin.auth.addApi'),
  62. buttonVariant: !canOAuth ? 'primary' : 'secondary-accent',
  63. }
  64. }, [canOAuth, theme, pluginPayload, t])
  65. const OAuthButton = useMemo(() => {
  66. const Item = (
  67. <div className={cn('min-w-0 flex-[1]', notAllowCustomCredential && 'opacity-50')}>
  68. <AddOAuthButton
  69. {...oAuthButtonProps}
  70. disabled={disabled || notAllowCustomCredential}
  71. onUpdate={onUpdate}
  72. />
  73. </div>
  74. )
  75. if (notAllowCustomCredential) {
  76. return (
  77. <Tooltip popupContent={t('plugin.auth.credentialUnavailable')}>
  78. {Item}
  79. </Tooltip>
  80. )
  81. }
  82. return Item
  83. }, [notAllowCustomCredential, oAuthButtonProps, disabled, onUpdate, t])
  84. const ApiKeyButton = useMemo(() => {
  85. const Item = (
  86. <div className={cn('min-w-0 flex-[1]', notAllowCustomCredential && 'opacity-50')}>
  87. <AddApiKeyButton
  88. {...apiKeyButtonProps}
  89. disabled={disabled || notAllowCustomCredential}
  90. onUpdate={onUpdate}
  91. />
  92. </div>
  93. )
  94. if (notAllowCustomCredential) {
  95. return (
  96. <Tooltip popupContent={t('plugin.auth.credentialUnavailable')}>
  97. {Item}
  98. </Tooltip>
  99. )
  100. }
  101. return Item
  102. }, [notAllowCustomCredential, apiKeyButtonProps, disabled, onUpdate, t])
  103. return (
  104. <>
  105. <div className='flex items-center space-x-1.5'>
  106. {
  107. canOAuth && (
  108. OAuthButton
  109. )
  110. }
  111. {
  112. showDivider && canOAuth && canApiKey && (
  113. <div className='system-2xs-medium-uppercase flex shrink-0 flex-col items-center justify-between text-text-tertiary'>
  114. <div className='h-2 w-[1px] bg-divider-subtle'></div>
  115. or
  116. <div className='h-2 w-[1px] bg-divider-subtle'></div>
  117. </div>
  118. )
  119. }
  120. {
  121. canApiKey && (
  122. ApiKeyButton
  123. )
  124. }
  125. </div>
  126. </>
  127. )
  128. }
  129. export default memo(Authorize)