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.

configure.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. useState,
  6. } from 'react'
  7. import {
  8. RiAddLine,
  9. } from '@remixicon/react'
  10. import { useTranslation } from 'react-i18next'
  11. import {
  12. PortalToFollowElem,
  13. PortalToFollowElemContent,
  14. PortalToFollowElemTrigger,
  15. } from '@/app/components/base/portal-to-follow-elem'
  16. import Button from '@/app/components/base/button'
  17. import {
  18. AddApiKeyButton,
  19. AddOAuthButton,
  20. } from '@/app/components/plugins/plugin-auth'
  21. import type { DataSourceAuth } from './types'
  22. import type {
  23. AddApiKeyButtonProps,
  24. AddOAuthButtonProps,
  25. PluginPayload,
  26. } from '@/app/components/plugins/plugin-auth/types'
  27. type ConfigureProps = {
  28. item: DataSourceAuth
  29. pluginPayload: PluginPayload
  30. onUpdate?: () => void
  31. disabled?: boolean
  32. }
  33. const Configure = ({
  34. item,
  35. pluginPayload,
  36. onUpdate,
  37. disabled,
  38. }: ConfigureProps) => {
  39. const { t } = useTranslation()
  40. const [open, setOpen] = useState(false)
  41. const canApiKey = item.credential_schema?.length
  42. const oAuthData = item.oauth_schema || {}
  43. const canOAuth = oAuthData.client_schema?.length
  44. const oAuthButtonProps: AddOAuthButtonProps = useMemo(() => {
  45. return {
  46. buttonText: t('plugin.auth.addOAuth'),
  47. pluginPayload,
  48. }
  49. }, [pluginPayload, t])
  50. const apiKeyButtonProps: AddApiKeyButtonProps = useMemo(() => {
  51. return {
  52. pluginPayload,
  53. buttonText: t('plugin.auth.addApi'),
  54. }
  55. }, [pluginPayload, t])
  56. const handleToggle = useCallback(() => {
  57. setOpen(v => !v)
  58. }, [])
  59. const handleUpdate = useCallback(() => {
  60. setOpen(false)
  61. onUpdate?.()
  62. }, [onUpdate])
  63. return (
  64. <>
  65. <PortalToFollowElem
  66. open={open}
  67. onOpenChange={setOpen}
  68. placement='bottom-end'
  69. offset={{
  70. mainAxis: 4,
  71. crossAxis: -4,
  72. }}
  73. >
  74. <PortalToFollowElemTrigger onClick={handleToggle}>
  75. <Button
  76. variant='secondary-accent'
  77. >
  78. <RiAddLine className='h-4 w-4' />
  79. {t('common.dataSource.configure')}
  80. </Button>
  81. </PortalToFollowElemTrigger>
  82. <PortalToFollowElemContent className='z-[61]'>
  83. <div className='w-[240px] space-y-1.5 rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur p-2 shadow-lg'>
  84. {
  85. !!canOAuth && (
  86. <AddOAuthButton
  87. {...oAuthButtonProps}
  88. onUpdate={handleUpdate}
  89. oAuthData={{
  90. schema: oAuthData.client_schema || [],
  91. is_oauth_custom_client_enabled: oAuthData.is_oauth_custom_client_enabled,
  92. is_system_oauth_params_exists: oAuthData.is_system_oauth_params_exists,
  93. client_params: oAuthData.oauth_custom_client_params,
  94. redirect_uri: oAuthData.redirect_uri,
  95. }}
  96. disabled={disabled}
  97. />
  98. )
  99. }
  100. {
  101. !!canApiKey && !!canOAuth && (
  102. <div className='system-2xs-medium-uppercase flex h-4 items-center p-2 text-text-quaternary'>
  103. <div className='mr-2 h-[1px] grow bg-gradient-to-l from-[rgba(16,24,40,0.08)]' />
  104. OR
  105. <div className='ml-2 h-[1px] grow bg-gradient-to-r from-[rgba(16,24,40,0.08)]' />
  106. </div>
  107. )
  108. }
  109. {
  110. !!canApiKey && (
  111. <AddApiKeyButton
  112. {...apiKeyButtonProps}
  113. formSchemas={item.credential_schema}
  114. onUpdate={handleUpdate}
  115. disabled={disabled}
  116. />
  117. )
  118. }
  119. </div>
  120. </PortalToFollowElemContent>
  121. </PortalToFollowElem>
  122. </>
  123. )
  124. }
  125. export default memo(Configure)