list.tsx 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useEffect, useMemo, useState } from 'react'
  4. import { useBoolean, useDebounceFn } from 'ahooks'
  5. import { ArrowDownIcon } from '@heroicons/react/24/outline'
  6. import { pick, uniq } from 'lodash-es'
  7. import {
  8. RiArchive2Line,
  9. RiBarChartBoxLine,
  10. RiDeleteBinLine,
  11. RiDownloadLine,
  12. RiEditLine,
  13. RiEqualizer2Line,
  14. RiEyeLine,
  15. RiLockLine,
  16. RiLoopLeftLine,
  17. RiMoreFill,
  18. RiPauseCircleLine,
  19. RiPlayCircleLine,
  20. } from '@remixicon/react'
  21. import { useContext } from 'use-context-selector'
  22. import { useRouter } from 'next/navigation'
  23. import { useTranslation } from 'react-i18next'
  24. import { Globe01 } from '../../base/icons/src/vender/line/mapsAndTravel'
  25. import ChunkingModeLabel from '../common/chunking-mode-label'
  26. import FileTypeIcon from '../../base/file-uploader/file-type-icon'
  27. import s from './style.module.css'
  28. import RenameModal from './rename-modal'
  29. import PermissionModal from './permission-modal'
  30. import DownloadLogsModal from './download-logs-modal'
  31. import BatchAction from './detail/completed/common/batch-action'
  32. import cn from '@/utils/classnames'
  33. import Switch from '@/app/components/base/switch'
  34. import Divider from '@/app/components/base/divider'
  35. import Popover from '@/app/components/base/popover'
  36. import Confirm from '@/app/components/base/confirm'
  37. import Tooltip from '@/app/components/base/tooltip'
  38. import Toast, { ToastContext } from '@/app/components/base/toast'
  39. import type { Item } from '@/app/components/base/select'
  40. import type { ColorMap, IndicatorProps } from '@/app/components/header/indicator'
  41. import Indicator from '@/app/components/header/indicator'
  42. import { asyncRunSafe } from '@/utils'
  43. import { formatNumber } from '@/utils/format'
  44. import NotionIcon from '@/app/components/base/notion-icon'
  45. import ProgressBar from '@/app/components/base/progress-bar'
  46. import { ChunkingMode, DataSourceType, DocumentActionType, type DocumentDisplayStatus, type SimpleDocumentDetail } from '@/models/datasets'
  47. import type { CommonResponse } from '@/models/common'
  48. import useTimestamp from '@/hooks/use-timestamp'
  49. import { useDatasetDetailContextWithSelector as useDatasetDetailContext } from '@/context/dataset-detail'
  50. import type { Props as PaginationProps } from '@/app/components/base/pagination'
  51. import Pagination from '@/app/components/base/pagination'
  52. import Checkbox from '@/app/components/base/checkbox'
  53. import { useDocumentArchive, useDocumentDelete, useDocumentDisable, useDocumentEnable, useDocumentPause, useDocumentResume, useDocumentUnArchive, useSyncDocument, useSyncWebsite, useBatchDownloadDocuments } from '@/service/knowledge/use-document'
  54. import { extensionToFileType } from '@/app/components/datasets/hit-testing/utils/extension-to-file-type'
  55. import useBatchEditDocumentMetadata from '../metadata/hooks/use-batch-edit-document-metadata'
  56. import EditMetadataBatchModal from '@/app/components/datasets/metadata/edit-metadata-batch/modal'
  57. import { noop } from 'lodash-es'
  58. import { get } from '@/service/base'
  59. import { API_BASE_URL, KKFILEVIEW_URL } from '@/config'
  60. import KKFileViewPreview from './kkfileview-preview'
  61. export const useIndexStatus = () => {
  62. const { t } = useTranslation()
  63. return {
  64. queuing: { color: 'orange', text: t('datasetDocuments.list.status.queuing') }, // waiting
  65. indexing: { color: 'blue', text: t('datasetDocuments.list.status.indexing') }, // indexing splitting parsing cleaning
  66. paused: { color: 'orange', text: t('datasetDocuments.list.status.paused') }, // paused
  67. error: { color: 'red', text: t('datasetDocuments.list.status.error') }, // error
  68. available: { color: 'green', text: t('datasetDocuments.list.status.available') }, // completed,archived = false,enabled = true
  69. enabled: { color: 'green', text: t('datasetDocuments.list.status.enabled') }, // completed,archived = false,enabled = true
  70. disabled: { color: 'gray', text: t('datasetDocuments.list.status.disabled') }, // completed,archived = false,enabled = false
  71. archived: { color: 'gray', text: t('datasetDocuments.list.status.archived') }, // completed,archived = true
  72. }
  73. }
  74. const STATUS_TEXT_COLOR_MAP: ColorMap = {
  75. green: 'text-util-colors-green-green-600',
  76. orange: 'text-util-colors-warning-warning-600',
  77. red: 'text-util-colors-red-red-600',
  78. blue: 'text-util-colors-blue-light-blue-light-600',
  79. yellow: 'text-util-colors-warning-warning-600',
  80. gray: 'text-text-tertiary',
  81. }
  82. // status item for list
  83. export const StatusItem: FC<{
  84. status: DocumentDisplayStatus
  85. reverse?: boolean
  86. scene?: 'list' | 'detail'
  87. textCls?: string
  88. errorMessage?: string
  89. detail?: {
  90. enabled: boolean
  91. archived: boolean
  92. id: string
  93. }
  94. datasetId?: string
  95. onUpdate?: (operationName?: string) => void
  96. }> = ({ status, reverse = false, scene = 'list', textCls = '', errorMessage, datasetId = '', detail, onUpdate }) => {
  97. const DOC_INDEX_STATUS_MAP = useIndexStatus()
  98. const localStatus = status.toLowerCase() as keyof typeof DOC_INDEX_STATUS_MAP
  99. const { enabled = false, archived = false, id = '' } = detail || {}
  100. const { notify } = useContext(ToastContext)
  101. const { t } = useTranslation()
  102. const { mutateAsync: enableDocument } = useDocumentEnable()
  103. const { mutateAsync: disableDocument } = useDocumentDisable()
  104. const { mutateAsync: deleteDocument } = useDocumentDelete()
  105. const onOperate = async (operationName: OperationName) => {
  106. let opApi = deleteDocument
  107. switch (operationName) {
  108. case 'enable':
  109. opApi = enableDocument
  110. break
  111. case 'disable':
  112. opApi = disableDocument
  113. break
  114. }
  115. const [e] = await asyncRunSafe<CommonResponse>(opApi({ datasetId, documentId: id }) as Promise<CommonResponse>)
  116. if (!e) {
  117. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  118. onUpdate?.()
  119. // onUpdate?.(operationName)
  120. }
  121. else { notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) }
  122. }
  123. const { run: handleSwitch } = useDebounceFn((operationName: OperationName) => {
  124. if (operationName === 'enable' && enabled)
  125. return
  126. if (operationName === 'disable' && !enabled)
  127. return
  128. onOperate(operationName)
  129. }, { wait: 500 })
  130. const embedding = useMemo(() => {
  131. return ['queuing', 'indexing', 'paused'].includes(localStatus)
  132. }, [localStatus])
  133. return <div className={
  134. cn('flex items-center',
  135. reverse ? 'flex-row-reverse' : '',
  136. scene === 'detail' ? s.statusItemDetail : '')
  137. }>
  138. <Indicator color={DOC_INDEX_STATUS_MAP[localStatus]?.color as IndicatorProps['color']} className={reverse ? 'ml-2' : 'mr-2'} />
  139. <span className={cn(`${STATUS_TEXT_COLOR_MAP[DOC_INDEX_STATUS_MAP[localStatus].color as keyof typeof STATUS_TEXT_COLOR_MAP]} text-sm`, textCls)}>
  140. {DOC_INDEX_STATUS_MAP[localStatus]?.text}
  141. </span>
  142. {
  143. errorMessage && (
  144. <Tooltip
  145. popupContent={
  146. <div className='max-w-[260px] break-all'>{errorMessage}</div>
  147. }
  148. triggerClassName='ml-1 w-4 h-4'
  149. />
  150. )
  151. }
  152. {
  153. scene === 'detail' && (
  154. <div className='ml-1.5 flex items-center justify-between'>
  155. <Tooltip
  156. popupContent={t('datasetDocuments.list.action.enableWarning')}
  157. popupClassName='text-text-secondary system-xs-medium'
  158. disabled={!archived}
  159. >
  160. <Switch
  161. defaultValue={archived ? false : enabled}
  162. onChange={v => !archived && handleSwitch(v ? 'enable' : 'disable')}
  163. disabled={embedding || archived}
  164. size='md'
  165. />
  166. </Tooltip>
  167. </div>
  168. )
  169. }
  170. </div>
  171. }
  172. type OperationName = 'delete' | 'archive' | 'enable' | 'disable' | 'sync' | 'un_archive' | 'pause' | 'resume'
  173. // operation action for list and detail
  174. export const OperationAction: FC<{
  175. embeddingAvailable: boolean
  176. detail: {
  177. name: string
  178. enabled: boolean
  179. archived: boolean
  180. id: string
  181. data_source_type: string
  182. doc_form: string
  183. display_status?: string
  184. }
  185. datasetId: string
  186. onUpdate: (operationName?: string) => void
  187. scene?: 'list' | 'detail'
  188. className?: string
  189. }> = ({ embeddingAvailable, datasetId, detail, onUpdate, scene = 'list', className = '' }) => {
  190. const { id, enabled = false, archived = false, data_source_type, display_status } = detail || {}
  191. const [showModal, setShowModal] = useState(false)
  192. const [deleting, setDeleting] = useState(false)
  193. const { notify } = useContext(ToastContext)
  194. const { t } = useTranslation()
  195. const router = useRouter()
  196. const [previewUrl, setPreviewUrl] = useState('')
  197. const [showPreview, setShowPreview] = useState(false)
  198. const { mutateAsync: archiveDocument } = useDocumentArchive()
  199. const { mutateAsync: unArchiveDocument } = useDocumentUnArchive()
  200. const { mutateAsync: enableDocument } = useDocumentEnable()
  201. const { mutateAsync: disableDocument } = useDocumentDisable()
  202. const { mutateAsync: deleteDocument } = useDocumentDelete()
  203. const { mutateAsync: syncDocument } = useSyncDocument()
  204. const { mutateAsync: syncWebsite } = useSyncWebsite()
  205. const { mutateAsync: pauseDocument } = useDocumentPause()
  206. const { mutateAsync: resumeDocument } = useDocumentResume()
  207. const isListScene = scene === 'list'
  208. const onOperate = async (operationName: OperationName) => {
  209. let opApi
  210. switch (operationName) {
  211. case 'archive':
  212. opApi = archiveDocument
  213. break
  214. case 'un_archive':
  215. opApi = unArchiveDocument
  216. break
  217. case 'enable':
  218. opApi = enableDocument
  219. break
  220. case 'disable':
  221. opApi = disableDocument
  222. break
  223. case 'sync':
  224. if (data_source_type === 'notion_import')
  225. opApi = syncDocument
  226. else
  227. opApi = syncWebsite
  228. break
  229. case 'pause':
  230. opApi = pauseDocument
  231. break
  232. case 'resume':
  233. opApi = resumeDocument
  234. break
  235. default:
  236. opApi = deleteDocument
  237. setDeleting(true)
  238. break
  239. }
  240. const [e] = await asyncRunSafe<CommonResponse>(opApi({ datasetId, documentId: id }) as Promise<CommonResponse>)
  241. if (!e) {
  242. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  243. onUpdate(operationName)
  244. }
  245. else { notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) }
  246. if (operationName === 'delete')
  247. setDeleting(false)
  248. }
  249. const { run: handleSwitch } = useDebounceFn((operationName: OperationName) => {
  250. if (operationName === 'enable' && enabled)
  251. return
  252. if (operationName === 'disable' && !enabled)
  253. return
  254. onOperate(operationName)
  255. }, { wait: 500 })
  256. const [currDocument, setCurrDocument] = useState<{
  257. id: string
  258. name: string
  259. } | null>(null)
  260. const [isShowRenameModal, {
  261. setTrue: setShowRenameModalTrue,
  262. setFalse: setShowRenameModalFalse,
  263. }] = useBoolean(false)
  264. const handleShowRenameModal = useCallback((doc: {
  265. id: string
  266. name: string
  267. }) => {
  268. setCurrDocument(doc)
  269. setShowRenameModalTrue()
  270. }, [setShowRenameModalTrue])
  271. const handleRenamed = useCallback(() => {
  272. onUpdate()
  273. }, [onUpdate])
  274. // Permission modal state
  275. const [isShowPermissionModal, {
  276. setTrue: setShowPermissionModalTrue,
  277. setFalse: setShowPermissionModalFalse,
  278. }] = useBoolean(false)
  279. // Download logs modal state
  280. const [isShowDownloadLogsModal, {
  281. setTrue: setShowDownloadLogsModalTrue,
  282. setFalse: setShowDownloadLogsModalFalse,
  283. }] = useBoolean(false)
  284. return <div className='flex items-center' onClick={e => e.stopPropagation()}>
  285. {isListScene && !embeddingAvailable && (
  286. <Switch defaultValue={false} onChange={noop} disabled={true} size='md' />
  287. )}
  288. {isListScene && embeddingAvailable && (
  289. <>
  290. {archived
  291. ? <Tooltip
  292. popupContent={t('datasetDocuments.list.action.enableWarning')}
  293. popupClassName='!font-semibold'
  294. >
  295. <div>
  296. <Switch defaultValue={false} onChange={noop} disabled={true} size='md' />
  297. </div>
  298. </Tooltip>
  299. : <Switch defaultValue={enabled} onChange={v => handleSwitch(v ? 'enable' : 'disable')} size='md' />
  300. }
  301. <Divider className='!ml-4 !mr-2 !h-3' type='vertical' />
  302. </>
  303. )}
  304. {embeddingAvailable && (
  305. <>
  306. {/* 预览按钮 */}
  307. {KKFILEVIEW_URL && detail.data_source_type === 'upload_file' && (
  308. <Tooltip
  309. popupContent={t('datasetDocuments.list.action.preview')}
  310. popupClassName='text-text-secondary system-xs-medium'
  311. needsDelay={false}
  312. >
  313. <button
  314. className={cn('mr-2 cursor-pointer rounded-lg',
  315. !isListScene
  316. ? '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'
  317. : 'p-0.5 hover:bg-state-base-hover')}
  318. onClick={async () => {
  319. try {
  320. // 调用后端 API 获取签名的 URL
  321. const response = await get(`/datasets/${datasetId}/documents/${detail.id}/file-proxy-url`)
  322. setPreviewUrl(response.url)
  323. setShowPreview(true)
  324. }
  325. catch (error) {
  326. console.error('获取文件预览链接失败:', error)
  327. notify({ type: 'error', message: t('datasetDocuments.list.preview.failed') })
  328. }
  329. }}>
  330. <RiEyeLine className='h-4 w-4 text-components-button-secondary-text' />
  331. </button>
  332. </Tooltip>
  333. )}
  334. {/* 下载按钮 */}
  335. <Tooltip
  336. popupContent={t('datasetDocuments.list.action.download')}
  337. popupClassName='text-text-secondary system-xs-medium'
  338. needsDelay={false}
  339. >
  340. <button
  341. className={cn('mr-2 cursor-pointer rounded-lg',
  342. !isListScene
  343. ? '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'
  344. : 'p-0.5 hover:bg-state-base-hover')}
  345. onClick={async () => {
  346. try {
  347. // 第一步:使用项目的API服务获取下载信息
  348. console.log('Getting download info for document:', detail.id)
  349. const downloadInfo = await get<{ download_url?: string; url?: string; name?: string }>(
  350. `/datasets/${datasetId}/documents/${detail.id}/download`,
  351. )
  352. console.log('Download info:', downloadInfo)
  353. // 第二步:使用返回的URL下载实际文件
  354. const fileUrl = downloadInfo.download_url || downloadInfo.url
  355. if (!fileUrl)
  356. throw new Error('未获取到有效的下载链接')
  357. // 创建下载链接
  358. const link = document.createElement('a')
  359. link.href = fileUrl
  360. link.download = downloadInfo.name || detail.name
  361. link.target = '_blank'
  362. document.body.appendChild(link)
  363. link.click()
  364. document.body.removeChild(link)
  365. console.log('文件下载已开始:', downloadInfo.name || detail.name)
  366. }
  367. catch (error) {
  368. console.error('下载文件失败:', error)
  369. // 这里可以添加用户友好的错误提示
  370. // notify({ type: 'error', message: `下载失败: ${error.message}` })
  371. }
  372. }}>
  373. <RiDownloadLine className='h-4 w-4 text-components-button-secondary-text' />
  374. </button>
  375. </Tooltip>
  376. {/* 分段设置按钮 */}
  377. <Tooltip
  378. popupContent={t('datasetDocuments.list.action.settings')}
  379. popupClassName='text-text-secondary system-xs-medium'
  380. needsDelay={false}
  381. >
  382. <button
  383. className={cn('mr-2 cursor-pointer rounded-lg',
  384. !isListScene
  385. ? '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'
  386. : 'p-0.5 hover:bg-state-base-hover')}
  387. onClick={() => router.push(`/datasets/${datasetId}/documents/${detail.id}/settings`)}>
  388. <RiEqualizer2Line className='h-4 w-4 text-components-button-secondary-text' />
  389. </button>
  390. </Tooltip>
  391. <Popover
  392. htmlContent={
  393. <div className='w-full py-1'>
  394. {!archived && (
  395. <>
  396. <div className={s.actionItem} onClick={() => {
  397. handleShowRenameModal({
  398. id: detail.id,
  399. name: detail.name,
  400. })
  401. }}>
  402. <RiEditLine className='h-4 w-4 text-text-tertiary' />
  403. <span className={s.actionName}>{t('datasetDocuments.list.table.rename')}</span>
  404. </div>
  405. {['notion_import', DataSourceType.WEB].includes(data_source_type) && (
  406. <div className={s.actionItem} onClick={() => onOperate('sync')}>
  407. <RiLoopLeftLine className='h-4 w-4 text-text-tertiary' />
  408. <span className={s.actionName}>{t('datasetDocuments.list.action.sync')}</span>
  409. </div>
  410. )}
  411. <div className={s.actionItem} onClick={() => setShowPermissionModalTrue()}>
  412. <RiLockLine className='h-4 w-4 text-text-tertiary' />
  413. <span className={s.actionName}>{t('datasetDocuments.list.action.configPermission')}</span>
  414. </div>
  415. <div className={s.actionItem} onClick={() => setShowDownloadLogsModalTrue()}>
  416. <RiBarChartBoxLine className='h-4 w-4 text-text-tertiary' />
  417. <span className={s.actionName}>{t('datasetDocuments.list.action.downloadLogs')}</span>
  418. </div>
  419. <Divider className='my-1' />
  420. </>
  421. )}
  422. {!archived && display_status?.toLowerCase() === 'indexing' && (
  423. <div className={s.actionItem} onClick={() => onOperate('pause')}>
  424. <RiPauseCircleLine className='h-4 w-4 text-text-tertiary' />
  425. <span className={s.actionName}>{t('datasetDocuments.list.action.pause')}</span>
  426. </div>
  427. )}
  428. {!archived && display_status?.toLowerCase() === 'paused' && (
  429. <div className={s.actionItem} onClick={() => onOperate('resume')}>
  430. <RiPlayCircleLine className='h-4 w-4 text-text-tertiary' />
  431. <span className={s.actionName}>{t('datasetDocuments.list.action.resume')}</span>
  432. </div>
  433. )}
  434. {!archived && <div className={s.actionItem} onClick={() => onOperate('archive')}>
  435. <RiArchive2Line className='h-4 w-4 text-text-tertiary' />
  436. <span className={s.actionName}>{t('datasetDocuments.list.action.archive')}</span>
  437. </div>}
  438. {archived && (
  439. <div className={s.actionItem} onClick={() => onOperate('un_archive')}>
  440. <RiArchive2Line className='h-4 w-4 text-text-tertiary' />
  441. <span className={s.actionName}>{t('datasetDocuments.list.action.unarchive')}</span>
  442. </div>
  443. )}
  444. <div className={cn(s.actionItem, s.deleteActionItem, 'group')} onClick={() => setShowModal(true)}>
  445. <RiDeleteBinLine className={'h-4 w-4 text-text-tertiary group-hover:text-text-destructive'} />
  446. <span className={cn(s.actionName, 'group-hover:text-text-destructive')}>{t('datasetDocuments.list.action.delete')}</span>
  447. </div>
  448. </div>
  449. }
  450. trigger='click'
  451. position='br'
  452. btnElement={
  453. <div className={cn(s.commonIcon)}>
  454. <RiMoreFill className='h-4 w-4 text-components-button-secondary-text' />
  455. </div>
  456. }
  457. btnClassName={open => cn(isListScene ? s.actionIconWrapperList : s.actionIconWrapperDetail, open ? '!hover:bg-state-base-hover !shadow-none' : '!bg-transparent')}
  458. popupClassName='!w-full'
  459. className={`!z-20 flex h-fit !w-[200px] justify-end ${className}`}
  460. />
  461. </>
  462. )}
  463. {showModal
  464. && <Confirm
  465. isShow={showModal}
  466. isLoading={deleting}
  467. isDisabled={deleting}
  468. title={t('datasetDocuments.list.delete.title')}
  469. content={t('datasetDocuments.list.delete.content')}
  470. confirmText={t('common.operation.sure')}
  471. onConfirm={() => onOperate('delete')}
  472. onCancel={() => setShowModal(false)}
  473. />
  474. }
  475. {isShowRenameModal && currDocument && (
  476. <RenameModal
  477. datasetId={datasetId}
  478. documentId={currDocument.id}
  479. name={currDocument.name}
  480. onClose={setShowRenameModalFalse}
  481. onSaved={handleRenamed}
  482. />
  483. )}
  484. {isShowPermissionModal && (
  485. <PermissionModal
  486. datasetId={datasetId}
  487. documentId={detail.id}
  488. documentName={detail.name}
  489. onClose={setShowPermissionModalFalse}
  490. />
  491. )}
  492. {isShowDownloadLogsModal && (
  493. <DownloadLogsModal
  494. datasetId={datasetId}
  495. documentId={detail.id}
  496. documentName={detail.name}
  497. onClose={setShowDownloadLogsModalFalse}
  498. />
  499. )}
  500. {showPreview && previewUrl && (
  501. <KKFileViewPreview
  502. url={previewUrl}
  503. fileName={detail.name}
  504. onCancel={() => {
  505. setShowPreview(false)
  506. setPreviewUrl('')
  507. }}
  508. />
  509. )}
  510. </div>
  511. }
  512. export const renderTdValue = (value: string | number | null, isEmptyStyle = false) => {
  513. return (
  514. <div className={cn(isEmptyStyle ? 'text-text-tertiary' : 'text-text-secondary', s.tdValue)}>
  515. {value ?? '-'}
  516. </div>
  517. )
  518. }
  519. const renderCount = (count: number | undefined) => {
  520. if (!count)
  521. return renderTdValue(0, true)
  522. if (count < 1000)
  523. return count
  524. return `${formatNumber((count / 1000).toFixed(1))}k`
  525. }
  526. type LocalDoc = SimpleDocumentDetail & { percent?: number }
  527. type IDocumentListProps = {
  528. embeddingAvailable: boolean
  529. documents: LocalDoc[]
  530. selectedIds: string[]
  531. onSelectedIdChange: (selectedIds: string[]) => void
  532. datasetId: string
  533. pagination: PaginationProps
  534. onUpdate: () => void
  535. onManageMetadata: () => void
  536. statusFilter: Item
  537. onStatusFilterChange: (filter: string) => void
  538. }
  539. /**
  540. * Document list component including basic information
  541. */
  542. const DocumentList: FC<IDocumentListProps> = ({
  543. embeddingAvailable,
  544. documents = [],
  545. selectedIds,
  546. onSelectedIdChange,
  547. datasetId,
  548. pagination,
  549. onUpdate,
  550. onManageMetadata,
  551. statusFilter,
  552. }) => {
  553. const { t } = useTranslation()
  554. const { formatTime } = useTimestamp()
  555. const router = useRouter()
  556. const [datasetConfig] = useDatasetDetailContext(s => [s.dataset])
  557. const chunkingMode = datasetConfig?.doc_form
  558. const isGeneralMode = chunkingMode !== ChunkingMode.parentChild
  559. const isQAMode = chunkingMode === ChunkingMode.qa
  560. const [localDocs, setLocalDocs] = useState<LocalDoc[]>(documents)
  561. const [sortField, setSortField] = useState<'name' | 'word_count' | 'hit_count' | 'created_at' | null>('created_at')
  562. const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc')
  563. const {
  564. isShowEditModal,
  565. showEditModal,
  566. hideEditModal,
  567. originalList,
  568. handleSave,
  569. } = useBatchEditDocumentMetadata({
  570. datasetId,
  571. docList: documents.filter(doc => selectedIds.includes(doc.id)),
  572. selectedDocumentIds: selectedIds, // Pass all selected IDs separately
  573. onUpdate,
  574. })
  575. useEffect(() => {
  576. let filteredDocs = documents
  577. if (statusFilter.value !== 'all') {
  578. filteredDocs = filteredDocs.filter(doc =>
  579. typeof doc.display_status === 'string'
  580. && typeof statusFilter.value === 'string'
  581. && doc.display_status.toLowerCase() === statusFilter.value.toLowerCase(),
  582. )
  583. }
  584. if (!sortField) {
  585. setLocalDocs(filteredDocs)
  586. return
  587. }
  588. const sortedDocs = [...filteredDocs].sort((a, b) => {
  589. let aValue: any
  590. let bValue: any
  591. switch (sortField) {
  592. case 'name':
  593. aValue = a.name?.toLowerCase() || ''
  594. bValue = b.name?.toLowerCase() || ''
  595. break
  596. case 'word_count':
  597. aValue = a.word_count || 0
  598. bValue = b.word_count || 0
  599. break
  600. case 'hit_count':
  601. aValue = a.hit_count || 0
  602. bValue = b.hit_count || 0
  603. break
  604. case 'created_at':
  605. aValue = a.created_at
  606. bValue = b.created_at
  607. break
  608. default:
  609. return 0
  610. }
  611. if (sortField === 'name') {
  612. const result = aValue.localeCompare(bValue)
  613. return sortOrder === 'asc' ? result : -result
  614. }
  615. else {
  616. const result = aValue - bValue
  617. return sortOrder === 'asc' ? result : -result
  618. }
  619. })
  620. setLocalDocs(sortedDocs)
  621. }, [documents, sortField, sortOrder, statusFilter])
  622. const handleSort = (field: 'name' | 'word_count' | 'hit_count' | 'created_at') => {
  623. if (sortField === field) {
  624. setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc')
  625. }
  626. else {
  627. setSortField(field)
  628. setSortOrder('desc')
  629. }
  630. }
  631. const renderSortHeader = (field: 'name' | 'word_count' | 'hit_count' | 'created_at', label: string) => {
  632. const isActive = sortField === field
  633. const isDesc = isActive && sortOrder === 'desc'
  634. return (
  635. <div className='flex cursor-pointer items-center hover:text-text-secondary' onClick={() => handleSort(field)}>
  636. {label}
  637. <ArrowDownIcon
  638. className={cn('ml-0.5 h-3 w-3 stroke-current stroke-2 transition-all',
  639. isActive ? 'text-text-tertiary' : 'text-text-disabled',
  640. isActive && !isDesc ? 'rotate-180' : '',
  641. )}
  642. />
  643. </div>
  644. )
  645. }
  646. const [currDocument, setCurrDocument] = useState<LocalDoc | null>(null)
  647. const [isShowRenameModal, {
  648. setTrue: setShowRenameModalTrue,
  649. setFalse: setShowRenameModalFalse,
  650. }] = useBoolean(false)
  651. const handleShowRenameModal = useCallback((doc: LocalDoc) => {
  652. setCurrDocument(doc)
  653. setShowRenameModalTrue()
  654. }, [setShowRenameModalTrue])
  655. const handleRenamed = useCallback(() => {
  656. onUpdate()
  657. }, [onUpdate])
  658. const isAllSelected = useMemo(() => {
  659. return localDocs.length > 0 && localDocs.every(doc => selectedIds.includes(doc.id))
  660. }, [localDocs, selectedIds])
  661. const isSomeSelected = useMemo(() => {
  662. return localDocs.some(doc => selectedIds.includes(doc.id))
  663. }, [localDocs, selectedIds])
  664. const onSelectedAll = useCallback(() => {
  665. if (isAllSelected)
  666. onSelectedIdChange([])
  667. else
  668. onSelectedIdChange(uniq([...selectedIds, ...localDocs.map(doc => doc.id)]))
  669. }, [isAllSelected, localDocs, onSelectedIdChange, selectedIds])
  670. const { mutateAsync: archiveDocument } = useDocumentArchive()
  671. const { mutateAsync: enableDocument } = useDocumentEnable()
  672. const { mutateAsync: disableDocument } = useDocumentDisable()
  673. const { mutateAsync: deleteDocument } = useDocumentDelete()
  674. const { mutateAsync: batchDownload } = useBatchDownloadDocuments()
  675. const handleAction = (actionName: DocumentActionType) => {
  676. return async () => {
  677. let opApi
  678. switch (actionName) {
  679. case DocumentActionType.archive:
  680. opApi = archiveDocument
  681. break
  682. case DocumentActionType.enable:
  683. opApi = enableDocument
  684. break
  685. case DocumentActionType.disable:
  686. opApi = disableDocument
  687. break
  688. default:
  689. opApi = deleteDocument
  690. break
  691. }
  692. const [e] = await asyncRunSafe<CommonResponse>(opApi({ datasetId, documentIds: selectedIds }) as Promise<CommonResponse>)
  693. if (!e) {
  694. Toast.notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  695. onUpdate()
  696. }
  697. else { Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) }
  698. }
  699. }
  700. const handleBatchDownload = async () => {
  701. try {
  702. const response = await batchDownload({
  703. datasetId,
  704. documentIds: selectedIds,
  705. })
  706. const successDownloads = response.downloads.filter(item => item.status === 'success')
  707. const failedDownloads = response.downloads.filter(item => item.status === 'failed')
  708. if (successDownloads.length === 0) {
  709. Toast.notify({
  710. type: 'error',
  711. message: t('datasetDocuments.list.batchDownload.allFailed'),
  712. })
  713. return
  714. }
  715. // Show browser notification first
  716. if (successDownloads.length > 1) {
  717. Toast.notify({
  718. type: 'info',
  719. message: t('datasetDocuments.list.batchDownload.starting', { count: successDownloads.length }),
  720. })
  721. }
  722. // Trigger downloads with delay to avoid browser blocking
  723. // Use longer delay (800ms) for better browser compatibility
  724. successDownloads.forEach((item, index) => {
  725. setTimeout(() => {
  726. const link = document.createElement('a')
  727. link.href = item.download_url!
  728. link.download = item.document_name
  729. link.style.display = 'none'
  730. document.body.appendChild(link)
  731. link.click()
  732. // Remove link after a delay to ensure download starts
  733. setTimeout(() => {
  734. document.body.removeChild(link)
  735. }, 1000)
  736. }, index * 800)
  737. })
  738. // Show final notification after all downloads are triggered
  739. setTimeout(() => {
  740. if (failedDownloads.length > 0) {
  741. Toast.notify({
  742. type: 'warning',
  743. message: t('datasetDocuments.list.batchDownload.partialSuccess', {
  744. success: successDownloads.length,
  745. failed: failedDownloads.length,
  746. }),
  747. })
  748. }
  749. else {
  750. Toast.notify({
  751. type: 'success',
  752. message: t('datasetDocuments.list.batchDownload.success', { count: successDownloads.length }),
  753. })
  754. }
  755. }, successDownloads.length * 800 + 500)
  756. }
  757. catch (error) {
  758. Toast.notify({
  759. type: 'error',
  760. message: t('datasetDocuments.list.batchDownload.failed'),
  761. })
  762. }
  763. }
  764. return (
  765. <div className='relative flex h-full w-full flex-col'>
  766. <div className='relative grow overflow-x-auto'>
  767. <table className={`mt-3 w-full min-w-[700px] max-w-full border-collapse border-0 text-sm ${s.documentTable}`}>
  768. <thead className="h-8 border-b border-divider-subtle text-xs font-medium uppercase leading-8 text-text-tertiary">
  769. <tr>
  770. <td className='w-12'>
  771. <div className='flex items-center' onClick={e => e.stopPropagation()}>
  772. {embeddingAvailable && (
  773. <Checkbox
  774. className='mr-2 shrink-0'
  775. checked={isAllSelected}
  776. indeterminate={!isAllSelected && isSomeSelected}
  777. onCheck={onSelectedAll}
  778. />
  779. )}
  780. #
  781. </div>
  782. </td>
  783. <td>
  784. {renderSortHeader('name', t('datasetDocuments.list.table.header.fileName'))}
  785. </td>
  786. <td className='w-[130px]'>{t('datasetDocuments.list.table.header.chunkingMode')}</td>
  787. <td className='w-24'>
  788. {renderSortHeader('word_count', t('datasetDocuments.list.table.header.words'))}
  789. </td>
  790. <td className='w-44'>
  791. {renderSortHeader('hit_count', t('datasetDocuments.list.table.header.hitCount'))}
  792. </td>
  793. <td className='w-44'>
  794. {renderSortHeader('created_at', t('datasetDocuments.list.table.header.uploadTime'))}
  795. </td>
  796. <td className='w-40'>{t('datasetDocuments.list.table.header.status')}</td>
  797. <td className='w-20'>{t('datasetDocuments.list.table.header.action')}</td>
  798. </tr>
  799. </thead>
  800. <tbody className="text-text-secondary">
  801. {localDocs.map((doc, index) => {
  802. const isFile = doc.data_source_type === DataSourceType.FILE
  803. const fileType = isFile ? doc.data_source_detail_dict?.upload_file?.extension : ''
  804. return <tr
  805. key={doc.id}
  806. className={'h-8 cursor-pointer border-b border-divider-subtle hover:bg-background-default-hover'}
  807. onClick={() => {
  808. router.push(`/datasets/${datasetId}/documents/${doc.id}`)
  809. }}>
  810. <td className='text-left align-middle text-xs text-text-tertiary'>
  811. <div className='flex items-center' onClick={e => e.stopPropagation()}>
  812. <Checkbox
  813. className='mr-2 shrink-0'
  814. checked={selectedIds.includes(doc.id)}
  815. onCheck={() => {
  816. onSelectedIdChange(
  817. selectedIds.includes(doc.id)
  818. ? selectedIds.filter(id => id !== doc.id)
  819. : [...selectedIds, doc.id],
  820. )
  821. }}
  822. />
  823. {index + 1}
  824. </div>
  825. </td>
  826. <td>
  827. <div className={'group mr-6 flex max-w-[460px] items-center hover:mr-0'}>
  828. <div className='shrink-0'>
  829. {doc?.data_source_type === DataSourceType.NOTION && <NotionIcon className='mr-1.5 mt-[-3px] inline-flex align-middle' type='page' src={doc.data_source_info.notion_page_icon} />}
  830. {doc?.data_source_type === DataSourceType.FILE && <FileTypeIcon type={extensionToFileType(doc?.data_source_info?.upload_file?.extension ?? fileType)} className='mr-1.5' />}
  831. {doc?.data_source_type === DataSourceType.WEB && <Globe01 className='mr-1.5 mt-[-3px] inline-flex align-middle' />}
  832. </div>
  833. <Tooltip
  834. popupContent={doc.name}
  835. >
  836. <span className='grow-1 truncate text-sm'>{doc.name}</span>
  837. </Tooltip>
  838. <div className='hidden shrink-0 group-hover:ml-auto group-hover:flex'>
  839. <Tooltip
  840. popupContent={t('datasetDocuments.list.table.rename')}
  841. >
  842. <div
  843. className='cursor-pointer rounded-md p-1 hover:bg-state-base-hover'
  844. onClick={(e) => {
  845. e.stopPropagation()
  846. handleShowRenameModal(doc)
  847. }}
  848. >
  849. <RiEditLine className='h-4 w-4 text-text-tertiary' />
  850. </div>
  851. </Tooltip>
  852. </div>
  853. </div>
  854. </td>
  855. <td>
  856. <ChunkingModeLabel
  857. isGeneralMode={isGeneralMode}
  858. isQAMode={isQAMode}
  859. />
  860. </td>
  861. <td>{renderCount(doc.word_count)}</td>
  862. <td>{renderCount(doc.hit_count)}</td>
  863. <td className='text-[13px] text-text-secondary'>
  864. {formatTime(doc.created_at, t('datasetHitTesting.dateTimeFormat') as string)}
  865. </td>
  866. <td>
  867. {
  868. (['indexing', 'splitting', 'parsing', 'cleaning'].includes(doc.indexing_status) && doc?.data_source_type === DataSourceType.NOTION)
  869. ? <ProgressBar percent={doc.percent || 0} />
  870. : <StatusItem status={doc.display_status} />
  871. }
  872. </td>
  873. <td>
  874. <OperationAction
  875. embeddingAvailable={embeddingAvailable}
  876. datasetId={datasetId}
  877. detail={pick(doc, ['name', 'enabled', 'archived', 'id', 'data_source_type', 'doc_form', 'display_status'])}
  878. onUpdate={onUpdate}
  879. />
  880. </td>
  881. </tr>
  882. })}
  883. </tbody>
  884. </table>
  885. </div>
  886. {(selectedIds.length > 0) && (
  887. <BatchAction
  888. className='absolute bottom-16 left-0 z-20'
  889. selectedIds={selectedIds}
  890. onArchive={handleAction(DocumentActionType.archive)}
  891. onBatchEnable={handleAction(DocumentActionType.enable)}
  892. onBatchDisable={handleAction(DocumentActionType.disable)}
  893. onBatchDelete={handleAction(DocumentActionType.delete)}
  894. onEditMetadata={showEditModal}
  895. onBatchDownload={handleBatchDownload}
  896. onCancel={() => {
  897. onSelectedIdChange([])
  898. }}
  899. />
  900. )}
  901. {/* Show Pagination only if the total is more than the limit */}
  902. {pagination.total && (
  903. <Pagination
  904. {...pagination}
  905. className='w-full shrink-0 px-0 pb-0'
  906. />
  907. )}
  908. {isShowRenameModal && currDocument && (
  909. <RenameModal
  910. datasetId={datasetId}
  911. documentId={currDocument.id}
  912. name={currDocument.name}
  913. onClose={setShowRenameModalFalse}
  914. onSaved={handleRenamed}
  915. />
  916. )}
  917. {isShowEditModal && (
  918. <EditMetadataBatchModal
  919. datasetId={datasetId}
  920. documentNum={selectedIds.length}
  921. list={originalList}
  922. onSave={handleSave}
  923. onHide={hideEditModal}
  924. onShowManage={() => {
  925. hideEditModal()
  926. onManageMetadata()
  927. }}
  928. />
  929. )}
  930. </div>
  931. )
  932. }
  933. export default DocumentList