From 34519de3b743ce5f02b52613b833367e070bb86f Mon Sep 17 00:00:00 2001 From: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Date: Mon, 6 Jan 2025 18:22:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20enhance=20chunk=20list=20management=20wi?= =?UTF-8?q?th=20new=20invalidation=20keys=20and=20imp=E2=80=A6=20(#12396)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ok --- .../documents/detail/completed/index.tsx | 70 +++++++++++++------ web/service/knowledge/use-segment.ts | 9 ++- web/service/use-base.ts | 6 +- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/web/app/components/datasets/documents/detail/completed/index.tsx b/web/app/components/datasets/documents/detail/completed/index.tsx index 1185c97e0ff80f..69cba1d8cd9fe5 100644 --- a/web/app/components/datasets/documents/detail/completed/index.tsx +++ b/web/app/components/datasets/documents/detail/completed/index.tsx @@ -32,6 +32,9 @@ import Checkbox from '@/app/components/base/checkbox' import { useChildSegmentList, useChildSegmentListKey, + useChunkListAllKey, + useChunkListDisabledKey, + useChunkListEnabledKey, useDeleteChildSegment, useDeleteSegment, useDisableSegment, @@ -156,18 +159,18 @@ const Completed: FC = ({ page: isFullDocMode ? 1 : currentPage, limit: isFullDocMode ? 10 : limit, keyword: isFullDocMode ? '' : searchValue, - enabled: selectedStatus === 'all' ? 'all' : !!selectedStatus, + enabled: selectedStatus, }, }, - currentPage === 0, ) const invalidSegmentList = useInvalid(useSegmentListKey) useEffect(() => { if (segmentListData) { setSegments(segmentListData.data || []) - if (segmentListData.total_pages < currentPage) - setCurrentPage(segmentListData.total_pages) + const totalPages = segmentListData.total_pages + if (totalPages < currentPage) + setCurrentPage(totalPages === 0 ? 1 : totalPages) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [segmentListData]) @@ -185,12 +188,12 @@ const Completed: FC = ({ documentId, segmentId: segments[0]?.id || '', params: { - page: currentPage, + page: currentPage === 0 ? 1 : currentPage, limit, keyword: searchValue, }, }, - !isFullDocMode || segments.length === 0 || currentPage === 0, + !isFullDocMode || segments.length === 0, ) const invalidChildSegmentList = useInvalid(useChildSegmentListKey) @@ -204,21 +207,20 @@ const Completed: FC = ({ useEffect(() => { if (childChunkListData) { setChildSegments(childChunkListData.data || []) - if (childChunkListData.total_pages < currentPage) - setCurrentPage(childChunkListData.total_pages) + const totalPages = childChunkListData.total_pages + if (totalPages < currentPage) + setCurrentPage(totalPages === 0 ? 1 : totalPages) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [childChunkListData]) const resetList = useCallback(() => { - setSegments([]) setSelectedSegmentIds([]) invalidSegmentList() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const resetChildList = useCallback(() => { - setChildSegments([]) invalidChildSegmentList() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) @@ -244,6 +246,20 @@ const Completed: FC = ({ const { mutateAsync: enableSegment } = useEnableSegment() const { mutateAsync: disableSegment } = useDisableSegment() + const invalidChunkListAll = useInvalid(useChunkListAllKey) + const invalidChunkListEnabled = useInvalid(useChunkListEnabledKey) + const invalidChunkListDisabled = useInvalid(useChunkListDisabledKey) + + const refreshChunkListWithStatusChanged = () => { + switch (selectedStatus) { + case 'all': + invalidChunkListDisabled() + invalidChunkListEnabled() + break + default: + invalidSegmentList() + } + } const onChangeSwitch = useCallback(async (enable: boolean, segId?: string) => { const operationApi = enable ? enableSegment : disableSegment @@ -255,6 +271,7 @@ const Completed: FC = ({ seg.enabled = enable } setSegments([...segments]) + refreshChunkListWithStatusChanged() }, onError: () => { notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) @@ -281,6 +298,23 @@ const Completed: FC = ({ const { mutateAsync: updateSegment } = useUpdateSegment() + const refreshChunkListDataWithDetailChanged = () => { + switch (selectedStatus) { + case 'all': + invalidChunkListDisabled() + invalidChunkListEnabled() + break + case true: + invalidChunkListAll() + invalidChunkListDisabled() + break + case false: + invalidChunkListAll() + invalidChunkListEnabled() + break + } + } + const handleUpdateSegment = useCallback(async ( segmentId: string, question: string, @@ -330,6 +364,7 @@ const Completed: FC = ({ } } setSegments([...segments]) + refreshChunkListDataWithDetailChanged() eventEmitter?.emit('update-segment-success') }, onSettled() { @@ -442,6 +477,7 @@ const Completed: FC = ({ seg.child_chunks?.push(newChildChunk!) } setSegments([...segments]) + refreshChunkListDataWithDetailChanged() } else { resetChildList() @@ -506,17 +542,10 @@ const Completed: FC = ({ } } setSegments([...segments]) + refreshChunkListDataWithDetailChanged() } else { - for (const childSeg of childSegments) { - if (childSeg.id === childChunkId) { - childSeg.content = res.data.content - childSeg.type = res.data.type - childSeg.word_count = res.data.word_count - childSeg.updated_at = res.data.updated_at - } - } - setChildSegments([...childSegments]) + resetChildList() } }, onSettled: () => { @@ -554,12 +583,13 @@ const Completed: FC = ({ } + notClearable /> ({ - queryKey: [...useSegmentListKey, datasetId, documentId, page, limit, keyword, enabled], + queryKey: [...useSegmentListKey, { datasetId, documentId, page, limit, keyword, enabled }], queryFn: () => { return get(`/datasets/${datasetId}/documents/${documentId}/segments`, { params }) }, @@ -110,7 +113,7 @@ export const useChildSegmentList = ( const { datasetId, documentId, segmentId, params } = payload const { page, limit, keyword } = params return useQuery({ - queryKey: [...useChildSegmentListKey, datasetId, documentId, segmentId, page, limit, keyword], + queryKey: [...useChildSegmentListKey, { datasetId, documentId, segmentId, page, limit, keyword }], queryFn: () => { return get(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`, { params }) }, diff --git a/web/service/use-base.ts b/web/service/use-base.ts index 5eeca22668161a..d5dd908aa108a2 100644 --- a/web/service/use-base.ts +++ b/web/service/use-base.ts @@ -1,13 +1,15 @@ import { + type QueryKey, useQueryClient, } from '@tanstack/react-query' -export const useInvalid = (key: string[]) => { +export const useInvalid = (key: QueryKey) => { const queryClient = useQueryClient() return () => { queryClient.invalidateQueries( { queryKey: key, - }) + }, + ) } }