This commit is contained in:
Joel 2025-03-14 15:01:39 +08:00
commit ded36c4ab8
4 changed files with 48 additions and 45 deletions

View File

@ -1,11 +1,10 @@
'use client'
import type { FC } from 'react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import useSWR from 'swr'
import { useTranslation } from 'react-i18next'
import { useRouter } from 'next/navigation'
import { useDebounce, useDebounceFn } from 'ahooks'
import { groupBy, omit } from 'lodash-es'
import { groupBy } from 'lodash-es'
import { PlusIcon } from '@heroicons/react/24/solid'
import { RiDraftLine, RiExternalLinkLine } from '@remixicon/react'
import AutoDisabledDocument from '../common/document-status-with-action/auto-disabled-document'
@ -15,16 +14,16 @@ import Loading from '@/app/components/base/loading'
import Button from '@/app/components/base/button'
import Input from '@/app/components/base/input'
import { get } from '@/service/base'
import { createDocument, fetchDocuments } from '@/service/datasets'
import { createDocument } from '@/service/datasets'
import { useDatasetDetailContext } from '@/context/dataset-detail'
import { NotionPageSelectorModal } from '@/app/components/base/notion-page-selector'
import type { NotionPage } from '@/models/common'
import type { CreateDocumentReq } from '@/models/datasets'
import { DataSourceType } from '@/models/datasets'
import { DataSourceType, ProcessMode } from '@/models/datasets'
import IndexFailed from '@/app/components/datasets/common/document-status-with-action/index-failed'
import { useProviderContext } from '@/context/provider-context'
import cn from '@/utils/classnames'
import { useInvalidDocumentDetailKey } from '@/service/knowledge/use-document'
import { useDocumentList, useInvalidDocumentDetailKey, useInvalidDocumentList } from '@/service/knowledge/use-document'
import { useInvalid } from '@/service/use-base'
import { useChildSegmentListKey, useSegmentListKey } from '@/service/knowledge/use-segment'
import useEditDocumentMetadata from '../metadata/hooks/use-edit-dataset-metadata'
@ -81,7 +80,7 @@ type IDocumentsProps = {
}
export const fetcher = (url: string) => get(url, {}, {})
const DEFAULT_LIMIT = 15
const DEFAULT_LIMIT = 10
const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
const { t } = useTranslation()
@ -102,33 +101,33 @@ const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
const debouncedSearchValue = useDebounce(searchValue, { wait: 500 })
const query = useMemo(() => {
return { page: currPage + 1, limit, keyword: debouncedSearchValue, fetch: isDataSourceNotion ? true : '' }
}, [currPage, debouncedSearchValue, isDataSourceNotion, limit])
const { data: documentsRes, mutate, isLoading: isListLoading } = useSWR(
{
action: 'fetchDocuments',
datasetId,
params: query,
const { data: documentsRes, isFetching: isListLoading } = useDocumentList({
datasetId,
query: {
page: currPage + 1,
limit,
keyword: debouncedSearchValue,
},
apiParams => fetchDocuments(omit(apiParams, 'action')),
{ refreshInterval: (isDataSourceNotion && timerCanRun) ? 2500 : 0 },
)
refetchInterval: (isDataSourceNotion && timerCanRun) ? 2500 : 0,
})
const invalidDocumentList = useInvalidDocumentList(datasetId)
const [isMuting, setIsMuting] = useState(false)
useEffect(() => {
if (!isListLoading && isMuting)
setIsMuting(false)
}, [isListLoading, isMuting])
if (documentsRes) {
const totalPages = Math.ceil(documentsRes.total / limit)
if (totalPages < currPage + 1)
setCurrPage(totalPages === 0 ? 0 : totalPages - 1)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [documentsRes])
const invalidDocumentDetail = useInvalidDocumentDetailKey()
const invalidChunkList = useInvalid(useSegmentListKey)
const invalidChildChunkList = useInvalid(useChildSegmentListKey)
const handleUpdate = useCallback(() => {
setIsMuting(true)
mutate()
invalidDocumentList()
invalidDocumentDetail()
setTimeout(() => {
invalidChunkList()
@ -178,8 +177,6 @@ const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
router.push(`/datasets/${datasetId}/documents/create`)
}
const isLoading = isListLoading // !documentsRes && !error
const handleSaveNotionPageSelected = async (selectedPages: NotionPage[]) => {
const workspacesMap = groupBy(selectedPages, 'workspace_id')
const workspaces = Object.keys(workspacesMap).map((workspaceId) => {
@ -212,7 +209,7 @@ const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
indexing_technique: dataset?.indexing_technique,
process_rule: {
rules: {},
mode: 'automatic',
mode: ProcessMode.general,
},
} as CreateDocumentReq
@ -220,7 +217,7 @@ const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
datasetId,
body: params,
})
mutate()
invalidDocumentList()
setTimerCanRun(true)
// mutateDatasetIndexingStatus(undefined, { revalidate: true })
setNotionPageSelectorModalVisible(false)
@ -251,7 +248,7 @@ const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
} = useEditDocumentMetadata({
datasetId,
dataset,
onUpdateDocList: mutate,
onUpdateDocList: invalidDocumentList,
})
return (
@ -311,7 +308,7 @@ const Documents: FC<IDocumentsProps> = ({ datasetId }) => {
)}
</div>
</div>
{(isLoading && !isMuting)
{isListLoading
? <Loading type='app' />
: total > 0
? <List

View File

@ -517,18 +517,20 @@ const DocumentList: FC<IDocumentListProps> = ({
return (
<div className='flex flex-col relative w-full h-full'>
<div className='grow overflow-x-auto'>
<div className='relative grow overflow-x-auto'>
<table className={`min-w-[700px] max-w-full w-full border-collapse border-0 text-sm mt-3 ${s.documentTable}`}>
<thead className="h-8 leading-8 border-b border-divider-subtle text-text-tertiary font-medium text-xs uppercase">
<tr>
<td className='w-12'>
<div className='flex items-center' onClick={e => e.stopPropagation()}>
<Checkbox
className='shrink-0 mr-2'
checked={isAllSelected}
mixed={!isAllSelected && isSomeSelected}
onCheck={onSelectedAll}
/>
{embeddingAvailable && (
<Checkbox
className='shrink-0 mr-2'
checked={isAllSelected}
mixed={!isAllSelected && isSomeSelected}
onCheck={onSelectedAll}
/>
)}
#
</div>
</td>

View File

@ -11,7 +11,7 @@ import type { CommonResponse } from '@/models/common'
const NAME_SPACE = 'knowledge/document'
const useDocumentListKey = [NAME_SPACE, 'documentList']
export const useDocumentListKey = [NAME_SPACE, 'documentList']
export const useDocumentList = (payload: {
datasetId: string
query: {

View File

@ -1,6 +1,6 @@
import type { BuiltInMetadataItem, MetadataBatchEditToServer, MetadataItemWithValueLength } from '@/app/components/datasets/metadata/types'
import { del, get, patch, post } from '../base'
import { mutate } from 'swr'
import { useDocumentListKey, useInvalidDocumentList } from './use-document'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useInvalid } from '../use-base'
import type { DocumentDetailResponse } from '@/models/datasets'
@ -44,14 +44,14 @@ export const useInvalidAllDocumentMetaData = (datasetId: string) => {
const useInvalidAllMetaData = (datasetId: string) => {
const invalidDatasetMetaData = useInvalidDatasetMetaData(datasetId)
const invalidDocumentList = useInvalidDocumentList(datasetId)
const invalidateAllDocumentMetaData = useInvalidAllDocumentMetaData(datasetId)
return async () => {
// meta data in dataset
await invalidDatasetMetaData()
// meta data in document list
mutate(
(key: any) => typeof key === 'object' && key.action === 'fetchDocuments' && key.datasetId === datasetId,
)
invalidDocumentList()
// meta data in single document
await invalidateAllDocumentMetaData() // meta data in document
}
@ -118,9 +118,13 @@ export const useBatchUpdateDocMetadata = () => {
queryKey: [NAME_SPACE, 'dataset', payload.dataset_id],
})
// meta data in document list
mutate(
(key: any) => typeof key === 'object' && key.action === 'fetchDocuments' && key.datasetId === payload.dataset_id,
)
await queryClient.invalidateQueries({
queryKey: [NAME_SPACE, 'dataset', payload.dataset_id],
})
await queryClient.invalidateQueries({
queryKey: [...useDocumentListKey, payload.dataset_id],
})
// meta data in single document
await Promise.all(documentIds.map(documentId => queryClient.invalidateQueries(
{