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.

operations.tsx 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import React, { useCallback, useState } from 'react'
  2. import { useContext } from 'use-context-selector'
  3. import { ToastContext } from '../../base/toast'
  4. import { useTranslation } from 'react-i18next'
  5. import { useRouter } from 'next/navigation'
  6. import { useDocumentArchive, useDocumentDelete, useDocumentDisable, useDocumentEnable, useDocumentUnArchive, useSyncDocument, useSyncWebsite } from '@/service/knowledge/use-document'
  7. import type { OperationName } from './types'
  8. import { asyncRunSafe } from '@/utils'
  9. import type { CommonResponse } from '@/models/common'
  10. import { useBoolean, useDebounceFn } from 'ahooks'
  11. import Switch from '../../base/switch'
  12. import { noop } from 'lodash'
  13. import Tooltip from '../../base/tooltip'
  14. import Divider from '../../base/divider'
  15. import cn from '@/utils/classnames'
  16. import { RiArchive2Line, RiDeleteBinLine, RiEditLine, RiEqualizer2Line, RiLoopLeftLine, RiMoreFill } from '@remixicon/react'
  17. import CustomPopover from '../../base/popover'
  18. import s from './style.module.css'
  19. import { DataSourceType } from '@/models/datasets'
  20. import Confirm from '../../base/confirm'
  21. import RenameModal from './rename-modal'
  22. type OperationsProps = {
  23. embeddingAvailable: boolean
  24. detail: {
  25. name: string
  26. enabled: boolean
  27. archived: boolean
  28. id: string
  29. data_source_type: string
  30. doc_form: string
  31. }
  32. datasetId: string
  33. onUpdate: (operationName?: string) => void
  34. scene?: 'list' | 'detail'
  35. className?: string
  36. }
  37. const Operations = ({
  38. embeddingAvailable,
  39. datasetId,
  40. detail,
  41. onUpdate,
  42. scene = 'list',
  43. className = '',
  44. }: OperationsProps) => {
  45. const { t } = useTranslation()
  46. const router = useRouter()
  47. const { id, enabled = false, archived = false, data_source_type } = detail || {}
  48. const [showModal, setShowModal] = useState(false)
  49. const [deleting, setDeleting] = useState(false)
  50. const { notify } = useContext(ToastContext)
  51. const { mutateAsync: archiveDocument } = useDocumentArchive()
  52. const { mutateAsync: unArchiveDocument } = useDocumentUnArchive()
  53. const { mutateAsync: enableDocument } = useDocumentEnable()
  54. const { mutateAsync: disableDocument } = useDocumentDisable()
  55. const { mutateAsync: deleteDocument } = useDocumentDelete()
  56. const { mutateAsync: syncDocument } = useSyncDocument()
  57. const { mutateAsync: syncWebsite } = useSyncWebsite()
  58. const isListScene = scene === 'list'
  59. const onOperate = async (operationName: OperationName) => {
  60. let opApi
  61. switch (operationName) {
  62. case 'archive':
  63. opApi = archiveDocument
  64. break
  65. case 'un_archive':
  66. opApi = unArchiveDocument
  67. break
  68. case 'enable':
  69. opApi = enableDocument
  70. break
  71. case 'disable':
  72. opApi = disableDocument
  73. break
  74. case 'sync':
  75. if (data_source_type === 'notion_import')
  76. opApi = syncDocument
  77. else
  78. opApi = syncWebsite
  79. break
  80. default:
  81. opApi = deleteDocument
  82. setDeleting(true)
  83. break
  84. }
  85. const [e] = await asyncRunSafe<CommonResponse>(opApi({ datasetId, documentId: id }) as Promise<CommonResponse>)
  86. if (!e) {
  87. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  88. onUpdate(operationName)
  89. }
  90. else { notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) }
  91. if (operationName === 'delete')
  92. setDeleting(false)
  93. }
  94. const { run: handleSwitch } = useDebounceFn((operationName: OperationName) => {
  95. if (operationName === 'enable' && enabled)
  96. return
  97. if (operationName === 'disable' && !enabled)
  98. return
  99. onOperate(operationName)
  100. }, { wait: 500 })
  101. const [currDocument, setCurrDocument] = useState<{
  102. id: string
  103. name: string
  104. } | null>(null)
  105. const [isShowRenameModal, {
  106. setTrue: setShowRenameModalTrue,
  107. setFalse: setShowRenameModalFalse,
  108. }] = useBoolean(false)
  109. const handleShowRenameModal = useCallback((doc: {
  110. id: string
  111. name: string
  112. }) => {
  113. setCurrDocument(doc)
  114. setShowRenameModalTrue()
  115. }, [setShowRenameModalTrue])
  116. const handleRenamed = useCallback(() => {
  117. onUpdate()
  118. }, [onUpdate])
  119. return <div className='flex items-center' onClick={e => e.stopPropagation()}>
  120. {isListScene && !embeddingAvailable && (
  121. <Switch defaultValue={false} onChange={noop} disabled={true} size='md' />
  122. )}
  123. {isListScene && embeddingAvailable && (
  124. <>
  125. {archived
  126. ? <Tooltip
  127. popupContent={t('datasetDocuments.list.action.enableWarning')}
  128. popupClassName='!font-semibold'
  129. needsDelay
  130. >
  131. <div>
  132. <Switch defaultValue={false} onChange={noop} disabled={true} size='md' />
  133. </div>
  134. </Tooltip>
  135. : <Switch defaultValue={enabled} onChange={v => handleSwitch(v ? 'enable' : 'disable')} size='md' />
  136. }
  137. <Divider className='!ml-4 !mr-2 !h-3' type='vertical' />
  138. </>
  139. )}
  140. {embeddingAvailable && (
  141. <>
  142. <Tooltip
  143. popupContent={t('datasetDocuments.list.action.settings')}
  144. popupClassName='text-text-secondary system-xs-medium'
  145. >
  146. <button
  147. className={cn('mr-2 cursor-pointer rounded-lg',
  148. !isListScene
  149. ? 'border-[0.5px] border-components-button-secondary-border bg-components-button-secondary-bg p-2 shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px] hover:border-components-button-secondary-border-hover hover:bg-components-button-secondary-bg-hover'
  150. : 'p-0.5 hover:bg-state-base-hover')}
  151. onClick={() => router.push(`/datasets/${datasetId}/documents/${detail.id}/settings`)}>
  152. <RiEqualizer2Line className='h-4 w-4 text-components-button-secondary-text' />
  153. </button>
  154. </Tooltip>
  155. <CustomPopover
  156. htmlContent={
  157. <div className='w-full py-1'>
  158. {!archived && (
  159. <>
  160. <div className={s.actionItem} onClick={() => {
  161. handleShowRenameModal({
  162. id: detail.id,
  163. name: detail.name,
  164. })
  165. }}>
  166. <RiEditLine className='h-4 w-4 text-text-tertiary' />
  167. <span className={s.actionName}>{t('datasetDocuments.list.table.rename')}</span>
  168. </div>
  169. {['notion_import', DataSourceType.WEB].includes(data_source_type) && (
  170. <div className={s.actionItem} onClick={() => onOperate('sync')}>
  171. <RiLoopLeftLine className='h-4 w-4 text-text-tertiary' />
  172. <span className={s.actionName}>{t('datasetDocuments.list.action.sync')}</span>
  173. </div>
  174. )}
  175. <Divider className='my-1' />
  176. </>
  177. )}
  178. {!archived && <div className={s.actionItem} onClick={() => onOperate('archive')}>
  179. <RiArchive2Line className='h-4 w-4 text-text-tertiary' />
  180. <span className={s.actionName}>{t('datasetDocuments.list.action.archive')}</span>
  181. </div>}
  182. {archived && (
  183. <div className={s.actionItem} onClick={() => onOperate('un_archive')}>
  184. <RiArchive2Line className='h-4 w-4 text-text-tertiary' />
  185. <span className={s.actionName}>{t('datasetDocuments.list.action.unarchive')}</span>
  186. </div>
  187. )}
  188. <div className={cn(s.actionItem, s.deleteActionItem, 'group')} onClick={() => setShowModal(true)}>
  189. <RiDeleteBinLine className={'h-4 w-4 text-text-tertiary group-hover:text-text-destructive'} />
  190. <span className={cn(s.actionName, 'group-hover:text-text-destructive')}>{t('datasetDocuments.list.action.delete')}</span>
  191. </div>
  192. </div>
  193. }
  194. trigger='click'
  195. position='br'
  196. btnElement={
  197. <div className={cn(s.commonIcon)}>
  198. <RiMoreFill className='h-4 w-4 text-components-button-secondary-text' />
  199. </div>
  200. }
  201. btnClassName={open => cn(isListScene ? s.actionIconWrapperList : s.actionIconWrapperDetail, open ? '!hover:bg-state-base-hover !shadow-none' : '!bg-transparent')}
  202. popupClassName='!w-full'
  203. className={`!z-20 flex h-fit !w-[200px] justify-end ${className}`}
  204. />
  205. </>
  206. )}
  207. {showModal
  208. && <Confirm
  209. isShow={showModal}
  210. isLoading={deleting}
  211. isDisabled={deleting}
  212. title={t('datasetDocuments.list.delete.title')}
  213. content={t('datasetDocuments.list.delete.content')}
  214. confirmText={t('common.operation.sure')}
  215. onConfirm={() => onOperate('delete')}
  216. onCancel={() => setShowModal(false)}
  217. />
  218. }
  219. {isShowRenameModal && currDocument && (
  220. <RenameModal
  221. datasetId={datasetId}
  222. documentId={currDocument.id}
  223. name={currDocument.name}
  224. onClose={setShowRenameModalFalse}
  225. onSaved={handleRenamed}
  226. />
  227. )}
  228. </div>
  229. }
  230. export default React.memo(Operations)