Refactor datasets service toward TanStack Query (#29008)

Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com>
This commit is contained in:
yyh
2025-12-09 13:44:45 +08:00
committed by GitHub
parent 61a6c6dbcf
commit 545a34fbaf
12 changed files with 270 additions and 152 deletions

View File

@ -2,11 +2,11 @@
import type { FC } from 'react'
import React, { useEffect, useReducer } from 'react'
import { useTranslation } from 'react-i18next'
import useSWR from 'swr'
import StatusWithAction from './status-with-action'
import { getErrorDocs, retryErrorDocs } from '@/service/datasets'
import { retryErrorDocs } from '@/service/datasets'
import type { IndexingStatusResponse } from '@/models/datasets'
import { noop } from 'lodash-es'
import { useDatasetErrorDocs } from '@/service/knowledge/use-dataset'
type Props = {
datasetId: string
@ -35,16 +35,19 @@ const indexStateReducer = (state: IIndexState, action: IAction) => {
const RetryButton: FC<Props> = ({ datasetId }) => {
const { t } = useTranslation()
const [indexState, dispatch] = useReducer(indexStateReducer, { value: 'success' })
const { data: errorDocs, isLoading } = useSWR({ datasetId }, getErrorDocs)
const { data: errorDocs, isLoading, refetch: refetchErrorDocs } = useDatasetErrorDocs(datasetId)
const onRetryErrorDocs = async () => {
dispatch({ type: 'retry' })
const document_ids = errorDocs?.data.map((doc: IndexingStatusResponse) => doc.id) || []
const res = await retryErrorDocs({ datasetId, document_ids })
if (res.result === 'success')
if (res.result === 'success') {
refetchErrorDocs()
dispatch({ type: 'success' })
else
}
else {
dispatch({ type: 'error' })
}
}
useEffect(() => {