Commit 4643c041 authored by crazywoola's avatar crazywoola

feat: add retry button

parent 3021f025
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React from 'react' import React, { useEffect, useReducer } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import classNames from 'classnames' import classNames from 'classnames'
import useSWR from 'swr' import useSWR from 'swr'
import s from './style.module.css' import s from './style.module.css'
import Divider from '@/app/components/base/divider' import Divider from '@/app/components/base/divider'
import { getErrorDocs } from '@/service/datasets' import { getErrorDocs, retryErrorDocs } from '@/service/datasets'
import type { IndexingStatusResponse } from '@/models/datasets'
const WarningIcon = () => const WarningIcon = () =>
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000 /svg"> <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000 /svg">
...@@ -16,17 +17,75 @@ const WarningIcon = () => ...@@ -16,17 +17,75 @@ const WarningIcon = () =>
type Props = { type Props = {
datasetId: string datasetId: string
} }
type IIndexState = {
value: string
}
type IAction = {
type: string
}
const indexStateReducer = (state: IIndexState, action: IAction) => {
switch (action.type) {
case 'retry':
return {
...state,
value: 'retrying',
}
case 'success':
return {
...state,
value: 'success',
}
case 'error':
return {
...state,
value: 'error',
}
default:
return {
...state,
value: 'success',
}
}
}
const RetryButton: FC<Props> = ( const RetryButton: FC<Props> = (
{ datasetId }, { datasetId },
) => { ) => {
const { t } = useTranslation() const { t } = useTranslation()
const [indexState, dispatch] = useReducer(indexStateReducer, { value: 'success' })
const { data: errorDocs } = useSWR({ datasetId }, getErrorDocs) const { data: errorDocs } = useSWR({ datasetId }, getErrorDocs)
const onRetryErrorDocs = async () => {
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 <div className={classNames('inline-flex justify-center items-center gap-2', s.retryBtn)}> return <div className={classNames('inline-flex justify-center items-center gap-2', s.retryBtn)}>
<WarningIcon /> <WarningIcon />
<span className='flex shrink-0 text-sm text-gray-500'>{errorDocs?.total} {t('dataset.docsFailedNotice')}</span> <span className='flex shrink-0 text-sm text-gray-500'>{errorDocs?.total} {t('dataset.docsFailedNotice')}</span>
<Divider type='vertical' className='!h-4' /> <Divider type='vertical' className='!h-4' />
<span className='text-primary-600 font-semibold text-sm'>{t('dataset.retry')}</span> <span
className={
classNames(
'text-primary-600 font-semibold text-sm cursor-pointer',
indexState.value === 'retrying' && 'text-gray-500 cursor-not-allowed',
)
}
onClick={indexState.value === 'error' ? onRetryErrorDocs : undefined}
>{t('dataset.retry')}</span>
</div> </div>
} }
export default RetryButton export default RetryButton
.retryBtn { .retryBtn {
@apply inline-flex justify-center items-center content-center h-9 leading-5 rounded-lg px-4 py-2 text-base cursor-pointer; @apply inline-flex justify-center items-center content-center h-9 leading-5 rounded-lg px-4 py-2 text-base;
@apply border-solid border border-gray-200 cursor-pointer text-gray-500 hover:bg-white hover:shadow-sm hover:border-gray-300; @apply border-solid border border-gray-200 text-gray-500 hover:bg-white hover:shadow-sm hover:border-gray-300;
} }
...@@ -413,6 +413,6 @@ export enum DocForm { ...@@ -413,6 +413,6 @@ export enum DocForm {
} }
export type ErrorDocsResponse = { export type ErrorDocsResponse = {
data: any[] data: IndexingStatusResponse[]
total: number total: number
} }
...@@ -232,3 +232,7 @@ export const fetchSupportFileTypes: Fetcher<FileTypesRes, { url: string }> = ({ ...@@ -232,3 +232,7 @@ export const fetchSupportFileTypes: Fetcher<FileTypesRes, { url: string }> = ({
export const getErrorDocs: Fetcher<ErrorDocsResponse, { datasetId: string }> = ({ datasetId }) => { export const getErrorDocs: Fetcher<ErrorDocsResponse, { datasetId: string }> = ({ datasetId }) => {
return get<ErrorDocsResponse>(`/datasets/${datasetId}/error-docs`) return get<ErrorDocsResponse>(`/datasets/${datasetId}/error-docs`)
} }
export const retryErrorDocs: Fetcher<CommonResponse, { datasetId: string; document_ids: string[] }> = ({ datasetId, document_ids }) => {
return post<CommonResponse>(`/datasets/${datasetId}/retry`, { body: { document_ids } })
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment