Skip to content

Commit

Permalink
fix: enhance chunk list management with new invalidation keys and imp… (
Browse files Browse the repository at this point in the history
  • Loading branch information
WTW0313 authored Jan 6, 2025
1 parent 147d578 commit 34519de
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
70 changes: 50 additions & 20 deletions web/app/components/datasets/documents/detail/completed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import Checkbox from '@/app/components/base/checkbox'
import {
useChildSegmentList,
useChildSegmentListKey,
useChunkListAllKey,
useChunkListDisabledKey,
useChunkListEnabledKey,
useDeleteChildSegment,
useDeleteSegment,
useDisableSegment,
Expand Down Expand Up @@ -156,18 +159,18 @@ const Completed: FC<ICompletedProps> = ({
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])
Expand All @@ -185,12 +188,12 @@ const Completed: FC<ICompletedProps> = ({
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)

Expand All @@ -204,21 +207,20 @@ const Completed: FC<ICompletedProps> = ({
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
}, [])
Expand All @@ -244,6 +246,20 @@ const Completed: FC<ICompletedProps> = ({

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
Expand All @@ -255,6 +271,7 @@ const Completed: FC<ICompletedProps> = ({
seg.enabled = enable
}
setSegments([...segments])
refreshChunkListWithStatusChanged()
},
onError: () => {
notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
Expand All @@ -281,6 +298,23 @@ const Completed: FC<ICompletedProps> = ({

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,
Expand Down Expand Up @@ -330,6 +364,7 @@ const Completed: FC<ICompletedProps> = ({
}
}
setSegments([...segments])
refreshChunkListDataWithDetailChanged()
eventEmitter?.emit('update-segment-success')
},
onSettled() {
Expand Down Expand Up @@ -442,6 +477,7 @@ const Completed: FC<ICompletedProps> = ({
seg.child_chunks?.push(newChildChunk!)
}
setSegments([...segments])
refreshChunkListDataWithDetailChanged()
}
else {
resetChildList()
Expand Down Expand Up @@ -506,17 +542,10 @@ const Completed: FC<ICompletedProps> = ({
}
}
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: () => {
Expand Down Expand Up @@ -554,12 +583,13 @@ const Completed: FC<ICompletedProps> = ({
<SimpleSelect
onSelect={onChangeStatus}
items={statusList.current}
defaultValue={'all'}
defaultValue={selectedStatus === 'all' ? 'all' : selectedStatus ? 1 : 0}
className={s.select}
wrapperClassName='h-fit mr-2'
optionWrapClassName='w-[160px]'
optionClassName='p-0'
renderOption={({ item, selected }) => <StatusItem item={item} selected={selected} />}
notClearable
/>
<Input
showLeftIcon
Expand Down
9 changes: 6 additions & 3 deletions web/service/knowledge/use-segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import type {
const NAME_SPACE = 'segment'

export const useSegmentListKey = [NAME_SPACE, 'chunkList']
export const useChunkListEnabledKey = [NAME_SPACE, 'chunkList', { enabled: true }]
export const useChunkListDisabledKey = [NAME_SPACE, 'chunkList', { enabled: false }]
export const useChunkListAllKey = [NAME_SPACE, 'chunkList', { enabled: 'all' }]

export const useSegmentList = (
payload: {
Expand All @@ -23,15 +26,15 @@ export const useSegmentList = (
page: number
limit: number
keyword: string
enabled: boolean | 'all'
enabled: boolean | 'all' | ''
}
},
disable?: boolean,
) => {
const { datasetId, documentId, params } = payload
const { page, limit, keyword, enabled } = params
return useQuery<SegmentsResponse>({
queryKey: [...useSegmentListKey, datasetId, documentId, page, limit, keyword, enabled],
queryKey: [...useSegmentListKey, { datasetId, documentId, page, limit, keyword, enabled }],
queryFn: () => {
return get<SegmentsResponse>(`/datasets/${datasetId}/documents/${documentId}/segments`, { params })
},
Expand Down Expand Up @@ -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<ChildSegmentsResponse>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`, { params })
},
Expand Down
6 changes: 4 additions & 2 deletions web/service/use-base.ts
Original file line number Diff line number Diff line change
@@ -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,
})
},
)
}
}

0 comments on commit 34519de

Please sign in to comment.