'use client' import type { FC } from 'react' import React, { useEffect, useReducer } from 'react' import { useTranslation } from 'react-i18next' import classNames from 'classnames' import useSWR from 'swr' import s from './style.module.css' import Divider from '@/app/components/base/divider' import { getErrorDocs, retryErrorDocs } from '@/service/datasets' import type { IndexingStatusResponse } from '@/models/datasets' const WarningIcon = () => type Props = { datasetId: string } type IIndexState = { value: string } type ActionType = 'retry' | 'success' | 'error' type IAction = { type: ActionType } const indexStateReducer = (state: IIndexState, action: IAction) => { const actionMap = { retry: 'retry', success: 'success', error: 'error', } return { ...state, value: actionMap[action.type] || state.value, } } const RetryButton: FC = ({ datasetId }) => { const { t } = useTranslation() const [indexState, dispatch] = useReducer(indexStateReducer, { value: 'success' }) const { data: errorDocs } = useSWR({ datasetId }, getErrorDocs) 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') dispatch({ type: 'success' }) else dispatch({ type: 'error' }) } useEffect(() => { if (errorDocs?.total === 0) dispatch({ type: 'success' }) else dispatch({ type: 'error' }) }, [errorDocs?.total]) if (indexState.value === 'success') return null return (
{errorDocs?.total} {t('dataset.docsFailedNotice')} {t('dataset.retry')}
) } export default RetryButton