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.

operator.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import {
  8. RiDeleteBinLine,
  9. RiEditLine,
  10. RiEqualizer2Line,
  11. RiHome9Line,
  12. RiStickyNoteAddLine,
  13. } from '@remixicon/react'
  14. import Dropdown from '@/app/components/base/dropdown'
  15. import type { Item } from '@/app/components/base/dropdown'
  16. import type {
  17. DataSourceCredential,
  18. } from './types'
  19. import { CredentialTypeEnum } from '@/app/components/plugins/plugin-auth/types'
  20. type OperatorProps = {
  21. credentialItem: DataSourceCredential
  22. onAction: (action: string, credentialItem: DataSourceCredential) => void
  23. onRename?: () => void
  24. }
  25. const Operator = ({
  26. credentialItem,
  27. onAction,
  28. onRename,
  29. }: OperatorProps) => {
  30. const { t } = useTranslation()
  31. const {
  32. type,
  33. } = credentialItem
  34. const items = useMemo(() => {
  35. const commonItems = [
  36. {
  37. value: 'setDefault',
  38. text: (
  39. <div className='flex items-center'>
  40. <RiHome9Line className='mr-2 h-4 w-4 text-text-tertiary' />
  41. <div className='system-sm-semibold text-text-secondary'>{t('plugin.auth.setDefault')}</div>
  42. </div>
  43. ),
  44. },
  45. ...(
  46. type === CredentialTypeEnum.OAUTH2
  47. ? [
  48. {
  49. value: 'rename',
  50. text: (
  51. <div className='flex items-center'>
  52. <RiEditLine className='mr-2 h-4 w-4 text-text-tertiary' />
  53. <div className='system-sm-semibold text-text-secondary'>{t('common.operation.rename')}</div>
  54. </div>
  55. ),
  56. },
  57. ]
  58. : []
  59. ),
  60. ...(
  61. type === CredentialTypeEnum.API_KEY
  62. ? [
  63. {
  64. value: 'edit',
  65. text: (
  66. <div className='flex items-center'>
  67. <RiEqualizer2Line className='mr-2 h-4 w-4 text-text-tertiary' />
  68. <div className='system-sm-semibold text-text-secondary'>{t('common.operation.edit')}</div>
  69. </div>
  70. ),
  71. },
  72. ]
  73. : []
  74. ),
  75. ]
  76. if (type === CredentialTypeEnum.OAUTH2) {
  77. const oAuthItems = [
  78. {
  79. value: 'change',
  80. text: (
  81. <div className='flex items-center'>
  82. <RiStickyNoteAddLine className='mr-2 h-4 w-4 text-text-tertiary' />
  83. <div className='system-sm-semibold mb-1 text-text-secondary'>{t('common.dataSource.notion.changeAuthorizedPages')}</div>
  84. </div>
  85. ),
  86. },
  87. ]
  88. commonItems.push(...oAuthItems)
  89. }
  90. return commonItems
  91. }, [t, type])
  92. const secondItems = useMemo(() => {
  93. return [
  94. {
  95. value: 'delete',
  96. text: (
  97. <div className='flex items-center'>
  98. <RiDeleteBinLine className='mr-2 h-4 w-4 text-text-tertiary' />
  99. <div className='system-sm-semibold text-text-secondary'>
  100. {t('common.operation.remove')}
  101. </div>
  102. </div>
  103. ),
  104. },
  105. ]
  106. }, [])
  107. const handleSelect = useCallback((item: Item) => {
  108. if (item.value === 'rename') {
  109. onRename?.()
  110. return
  111. }
  112. onAction(
  113. item.value as string,
  114. credentialItem,
  115. )
  116. }, [onAction, credentialItem, onRename])
  117. return (
  118. <Dropdown
  119. items={items}
  120. secondItems={secondItems}
  121. onSelect={handleSelect}
  122. popupClassName='z-[61]'
  123. triggerProps={{
  124. size: 'l',
  125. }}
  126. itemClassName='py-2 h-auto hover:bg-state-base-hover'
  127. secondItemClassName='py-2 h-auto hover:bg-state-base-hover'
  128. />
  129. )
  130. }
  131. export default memo(Operator)