Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index-failed.tsx 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useReducer } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import useSWR from 'swr'
  6. import StatusWithAction from './status-with-action'
  7. import { getErrorDocs, retryErrorDocs } from '@/service/datasets'
  8. import type { IndexingStatusResponse } from '@/models/datasets'
  9. import { noop } from 'lodash-es'
  10. type Props = {
  11. datasetId: string
  12. }
  13. type IIndexState = {
  14. value: string
  15. }
  16. type ActionType = 'retry' | 'success' | 'error'
  17. type IAction = {
  18. type: ActionType
  19. }
  20. const indexStateReducer = (state: IIndexState, action: IAction) => {
  21. const actionMap = {
  22. retry: 'retry',
  23. success: 'success',
  24. error: 'error',
  25. }
  26. return {
  27. ...state,
  28. value: actionMap[action.type] || state.value,
  29. }
  30. }
  31. const RetryButton: FC<Props> = ({ datasetId }) => {
  32. const { t } = useTranslation()
  33. const [indexState, dispatch] = useReducer(indexStateReducer, { value: 'success' })
  34. const { data: errorDocs, isLoading } = useSWR({ datasetId }, getErrorDocs)
  35. const onRetryErrorDocs = async () => {
  36. dispatch({ type: 'retry' })
  37. const document_ids = errorDocs?.data.map((doc: IndexingStatusResponse) => doc.id) || []
  38. const res = await retryErrorDocs({ datasetId, document_ids })
  39. if (res.result === 'success')
  40. dispatch({ type: 'success' })
  41. else
  42. dispatch({ type: 'error' })
  43. }
  44. useEffect(() => {
  45. if (errorDocs?.total === 0)
  46. dispatch({ type: 'success' })
  47. else
  48. dispatch({ type: 'error' })
  49. }, [errorDocs?.total])
  50. if (isLoading || indexState.value === 'success')
  51. return null
  52. return (
  53. <StatusWithAction
  54. type='warning'
  55. description={`${errorDocs?.total} ${t('dataset.docsFailedNotice')}`}
  56. actionText={t('dataset.retry')}
  57. disabled={indexState.value === 'retry'}
  58. onAction={indexState.value === 'error' ? onRetryErrorDocs : noop}
  59. />
  60. )
  61. }
  62. export default RetryButton