'use client' import type { FC } from 'react' import React, { useCallback, useEffect, useMemo, useState } from 'react' import { useBoolean } from 'ahooks' import { ArrowDownIcon } from '@heroicons/react/24/outline' import { pick, uniq } from 'lodash-es' import { RiEditLine } from '@remixicon/react' import { useRouter } from 'next/navigation' import { useTranslation } from 'react-i18next' import dayjs from 'dayjs' import { Globe01 } from '../../base/icons/src/vender/line/mapsAndTravel' import ChunkingModeLabel from '../common/chunking-mode-label' import FileTypeIcon from '../../base/file-uploader/file-type-icon' import s from './style.module.css' import RenameModal from './rename-modal' import BatchAction from './detail/completed/common/batch-action' import cn from '@/utils/classnames' import Tooltip from '@/app/components/base/tooltip' import Toast from '@/app/components/base/toast' import { asyncRunSafe } from '@/utils' import { formatNumber } from '@/utils/format' import NotionIcon from '@/app/components/base/notion-icon' import ProgressBar from '@/app/components/base/progress-bar' import { ChunkingMode, DataSourceType, DocumentActionType, type SimpleDocumentDetail } from '@/models/datasets' import type { CommonResponse } from '@/models/common' import useTimestamp from '@/hooks/use-timestamp' import { useDatasetDetailContextWithSelector as useDatasetDetailContext } from '@/context/dataset-detail' import type { Props as PaginationProps } from '@/app/components/base/pagination' import Pagination from '@/app/components/base/pagination' import Checkbox from '@/app/components/base/checkbox' import { useDocumentArchive, useDocumentDelete, useDocumentDisable, useDocumentEnable } from '@/service/knowledge/use-document' import { extensionToFileType } from '@/app/components/datasets/hit-testing/utils/extension-to-file-type' import useBatchEditDocumentMetadata from '../metadata/hooks/use-batch-edit-document-metadata' import EditMetadataBatchModal from '@/app/components/datasets/metadata/edit-metadata-batch/modal' import StatusItem from './status-item' import Operations from './operations' export const renderTdValue = (value: string | number | null, isEmptyStyle = false) => { return (
{value ?? '-'}
) } const renderCount = (count: number | undefined) => { if (!count) return renderTdValue(0, true) if (count < 1000) return count return `${formatNumber((count / 1000).toFixed(1))}k` } type LocalDoc = SimpleDocumentDetail & { percent?: number } type IDocumentListProps = { embeddingAvailable: boolean documents: LocalDoc[] selectedIds: string[] onSelectedIdChange: (selectedIds: string[]) => void datasetId: string pagination: PaginationProps onUpdate: () => void onManageMetadata: () => void } /** * Document list component including basic information */ const DocumentList: FC = ({ embeddingAvailable, documents = [], selectedIds, onSelectedIdChange, datasetId, pagination, onUpdate, onManageMetadata, }) => { const { t } = useTranslation() const { formatTime } = useTimestamp() const router = useRouter() const [datasetConfig] = useDatasetDetailContext(s => [s.dataset]) const chunkingMode = datasetConfig?.doc_form const isGeneralMode = chunkingMode !== ChunkingMode.parentChild const isQAMode = chunkingMode === ChunkingMode.qa const [localDocs, setLocalDocs] = useState(documents) const [enableSort, setEnableSort] = useState(true) const { isShowEditModal, showEditModal, hideEditModal, originalList, handleSave, } = useBatchEditDocumentMetadata({ datasetId, docList: documents.filter(item => selectedIds.includes(item.id)), onUpdate, }) useEffect(() => { setLocalDocs(documents) }, [documents]) const onClickSort = () => { setEnableSort(!enableSort) if (enableSort) { const sortedDocs = [...localDocs].sort((a, b) => dayjs(a.created_at).isBefore(dayjs(b.created_at)) ? -1 : 1) setLocalDocs(sortedDocs) } else { setLocalDocs(documents) } } const [currDocument, setCurrDocument] = useState(null) const [isShowRenameModal, { setTrue: setShowRenameModalTrue, setFalse: setShowRenameModalFalse, }] = useBoolean(false) const handleShowRenameModal = useCallback((doc: LocalDoc) => { setCurrDocument(doc) setShowRenameModalTrue() }, [setShowRenameModalTrue]) const handleRenamed = useCallback(() => { onUpdate() }, [onUpdate]) const isAllSelected = useMemo(() => { return localDocs.length > 0 && localDocs.every(doc => selectedIds.includes(doc.id)) }, [localDocs, selectedIds]) const isSomeSelected = useMemo(() => { return localDocs.some(doc => selectedIds.includes(doc.id)) }, [localDocs, selectedIds]) const onSelectedAll = useCallback(() => { if (isAllSelected) onSelectedIdChange([]) else onSelectedIdChange(uniq([...selectedIds, ...localDocs.map(doc => doc.id)])) }, [isAllSelected, localDocs, onSelectedIdChange, selectedIds]) const { mutateAsync: archiveDocument } = useDocumentArchive() const { mutateAsync: enableDocument } = useDocumentEnable() const { mutateAsync: disableDocument } = useDocumentDisable() const { mutateAsync: deleteDocument } = useDocumentDelete() const handleAction = (actionName: DocumentActionType) => { return async () => { let opApi switch (actionName) { case DocumentActionType.archive: opApi = archiveDocument break case DocumentActionType.enable: opApi = enableDocument break case DocumentActionType.disable: opApi = disableDocument break default: opApi = deleteDocument break } const [e] = await asyncRunSafe(opApi({ datasetId, documentIds: selectedIds }) as Promise) if (!e) { if (actionName === DocumentActionType.delete) onSelectedIdChange([]) Toast.notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) onUpdate() } else { Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) } } } return (
{localDocs.map((doc, index) => { const isFile = doc.data_source_type === DataSourceType.FILE const fileType = isFile ? doc.data_source_detail_dict?.upload_file?.extension : '' return { router.push(`/datasets/${datasetId}/documents/${doc.id}`) }}> })}
e.stopPropagation()}> {embeddingAvailable && ( )} #
{t('datasetDocuments.list.table.header.fileName')}
{t('datasetDocuments.list.table.header.chunkingMode')} {t('datasetDocuments.list.table.header.words')} {t('datasetDocuments.list.table.header.hitCount')}
{t('datasetDocuments.list.table.header.uploadTime')}
{t('datasetDocuments.list.table.header.status')} {t('datasetDocuments.list.table.header.action')}
e.stopPropagation()}> { onSelectedIdChange( selectedIds.includes(doc.id) ? selectedIds.filter(id => id !== doc.id) : [...selectedIds, doc.id], ) }} /> {index + 1}
{doc?.data_source_type === DataSourceType.NOTION && } {doc?.data_source_type === DataSourceType.FILE && } {doc?.data_source_type === DataSourceType.WEB && }
{doc.name}
{ e.stopPropagation() handleShowRenameModal(doc) }} >
{renderCount(doc.word_count)} {renderCount(doc.hit_count)} {formatTime(doc.created_at, t('datasetHitTesting.dateTimeFormat') as string)} { (['indexing', 'splitting', 'parsing', 'cleaning'].includes(doc.indexing_status) && doc?.data_source_type === DataSourceType.NOTION) ? : }
{(selectedIds.length > 0) && ( { onSelectedIdChange([]) }} /> )} {/* Show Pagination only if the total is more than the limit */} {pagination.total && ( )} {isShowRenameModal && currDocument && ( )} {isShowEditModal && ( { hideEditModal() onManageMetadata() }} /> )}
) } export default DocumentList