index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useMemo } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. RiAddLine,
  7. RiArrowDownSLine,
  8. RiErrorWarningFill,
  9. RiLoader2Line,
  10. } from '@remixicon/react'
  11. import cn from '@/utils/classnames'
  12. import { CheckCircle } from '@/app/components/base/icons/src/vender/solid/general'
  13. import Popover from '@/app/components/base/popover'
  14. export type ISegmentAddProps = {
  15. importStatus: ProcessStatus | string | undefined
  16. clearProcessStatus: () => void
  17. showNewSegmentModal: () => void
  18. showBatchModal: () => void
  19. embedding: boolean
  20. }
  21. export enum ProcessStatus {
  22. WAITING = 'waiting',
  23. PROCESSING = 'processing',
  24. COMPLETED = 'completed',
  25. ERROR = 'error',
  26. }
  27. const SegmentAdd: FC<ISegmentAddProps> = ({
  28. importStatus,
  29. clearProcessStatus,
  30. showNewSegmentModal,
  31. showBatchModal,
  32. embedding,
  33. }) => {
  34. const { t } = useTranslation()
  35. const textColor = useMemo(() => {
  36. return embedding
  37. ? 'text-components-button-secondary-accent-text-disabled'
  38. : 'text-components-button-secondary-accent-text'
  39. }, [embedding])
  40. if (importStatus) {
  41. return (
  42. <>
  43. {(importStatus === ProcessStatus.WAITING || importStatus === ProcessStatus.PROCESSING) && (
  44. <div className='relative overflow-hidden inline-flex items-center mr-2 px-2.5 py-2 text-components-button-secondary-accent-text
  45. bg-components-progress-bar-border rounded-lg border-[0.5px] border-components-progress-bar-border
  46. shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px]'>
  47. <div className={cn('absolute left-0 top-0 h-full bg-components-progress-bar-progress border-r-[1.5px] border-r-components-progress-bar-progress-highlight z-0', importStatus === ProcessStatus.WAITING ? 'w-3/12' : 'w-2/3')} />
  48. <RiLoader2Line className='animate-spin mr-1 w-4 h-4' />
  49. <span className='system-sm-medium z-10 pr-0.5'>{t('datasetDocuments.list.batchModal.processing')}</span>
  50. </div>
  51. )}
  52. {importStatus === ProcessStatus.COMPLETED && (
  53. <div className='relative inline-flex items-center mr-2 bg-components-panel-bg rounded-lg border-[0.5px] border-components-panel-border shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px] overflow-hidden'>
  54. <div className='inline-flex items-center px-2.5 py-2 text-text-success border-r border-r-divider-subtle'>
  55. <CheckCircle className='mr-1 w-4 h-4' />
  56. <span className='system-sm-medium pr-0.5'>{t('datasetDocuments.list.batchModal.completed')}</span>
  57. </div>
  58. <div className='m-1 inline-flex items-center'>
  59. <span className='system-xs-medium text-components-button-ghost-text hover:bg-components-button-ghost-bg-hover px-1.5 py-1 rounded-md cursor-pointer' onClick={clearProcessStatus}>{t('datasetDocuments.list.batchModal.ok')}</span>
  60. </div>
  61. <div className='absolute top-0 left-0 w-full h-full bg-dataset-chunk-process-success-bg opacity-40 -z-10' />
  62. </div>
  63. )}
  64. {importStatus === ProcessStatus.ERROR && (
  65. <div className='relative inline-flex items-center mr-2 bg-components-panel-bg rounded-lg border-[0.5px] border-components-panel-border shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px] overflow-hidden'>
  66. <div className='inline-flex items-center px-2.5 py-2 text-text-destructive border-r border-r-divider-subtle'>
  67. <RiErrorWarningFill className='mr-1 w-4 h-4' />
  68. <span className='system-sm-medium pr-0.5'>{t('datasetDocuments.list.batchModal.error')}</span>
  69. </div>
  70. <div className='m-1 inline-flex items-center'>
  71. <span className='system-xs-medium text-components-button-ghost-text hover:bg-components-button-ghost-bg-hover px-1.5 py-1 rounded-md cursor-pointer' onClick={clearProcessStatus}>{t('datasetDocuments.list.batchModal.ok')}</span>
  72. </div>
  73. <div className='absolute top-0 left-0 w-full h-full bg-dataset-chunk-process-error-bg opacity-40 -z-10' />
  74. </div>
  75. )}
  76. </>
  77. )
  78. }
  79. return (
  80. <div className={cn(
  81. 'flex items-center rounded-lg border-[0.5px] border-components-button-secondary-border bg-components-button-secondary-bg shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px] relative z-20',
  82. embedding && 'border-components-button-secondary-border-disabled bg-components-button-secondary-bg-disabled',
  83. )}>
  84. <button
  85. type='button'
  86. className={`inline-flex items-center px-2.5 py-2 rounded-l-lg border-r-[1px] border-r-divider-subtle
  87. hover:bg-state-base-hover disabled:cursor-not-allowed disabled:hover:bg-transparent`}
  88. onClick={showNewSegmentModal}
  89. disabled={embedding}
  90. >
  91. <RiAddLine className={cn('w-4 h-4', textColor)} />
  92. <span className={cn('text-[13px] leading-[16px] font-medium capitalize px-0.5 ml-0.5', textColor)}>
  93. {t('datasetDocuments.list.action.addButton')}
  94. </span>
  95. </button>
  96. <Popover
  97. position='br'
  98. manualClose
  99. trigger='click'
  100. htmlContent={
  101. <button
  102. type='button'
  103. className='w-full py-1.5 px-2 flex items-center rounded-lg text-text-secondary system-md-regular'
  104. onClick={showBatchModal}
  105. >
  106. {t('datasetDocuments.list.action.batchAdd')}
  107. </button>
  108. }
  109. btnElement={
  110. <div className='flex justify-center items-center' >
  111. <RiArrowDownSLine className={cn('w-4 h-4', textColor)}/>
  112. </div>
  113. }
  114. btnClassName={open => cn(
  115. `!p-2 !border-0 !rounded-l-none !rounded-r-lg !hover:bg-state-base-hover backdrop-blur-[5px]
  116. disabled:cursor-not-allowed disabled:bg-transparent disabled:hover:bg-transparent`,
  117. open ? '!bg-state-base-hover' : '',
  118. )}
  119. popupClassName='!min-w-[128px] !bg-components-panel-bg-blur !rounded-xl border-[0.5px] !ring-0
  120. border-components-panel-border !shadow-xl !shadow-shadow-shadow-5 backdrop-blur-[5px]'
  121. className='min-w-[128px] h-fit'
  122. disabled={embedding}
  123. />
  124. </div>
  125. )
  126. }
  127. export default React.memo(SegmentAdd)