index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 mr-2 inline-flex items-center overflow-hidden rounded-lg border-[0.5px] border-components-progress-bar-border
  45. bg-components-progress-bar-border px-2.5 py-2 text-components-button-secondary-accent-text
  46. shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px]'>
  47. <div className={cn('absolute left-0 top-0 z-0 h-full border-r-[1.5px] border-r-components-progress-bar-progress-highlight bg-components-progress-bar-progress', importStatus === ProcessStatus.WAITING ? 'w-3/12' : 'w-2/3')} />
  48. <RiLoader2Line className='mr-1 h-4 w-4 animate-spin' />
  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 mr-2 inline-flex items-center overflow-hidden rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px]'>
  54. <div className='inline-flex items-center border-r border-r-divider-subtle px-2.5 py-2 text-text-success'>
  55. <CheckCircle className='mr-1 h-4 w-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 cursor-pointer rounded-md px-1.5 py-1 text-components-button-ghost-text hover:bg-components-button-ghost-bg-hover' onClick={clearProcessStatus}>{t('datasetDocuments.list.batchModal.ok')}</span>
  60. </div>
  61. <div className='absolute left-0 top-0 -z-10 h-full w-full bg-dataset-chunk-process-success-bg opacity-40' />
  62. </div>
  63. )}
  64. {importStatus === ProcessStatus.ERROR && (
  65. <div className='relative mr-2 inline-flex items-center overflow-hidden rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px]'>
  66. <div className='inline-flex items-center border-r border-r-divider-subtle px-2.5 py-2 text-text-destructive'>
  67. <RiErrorWarningFill className='mr-1 h-4 w-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 cursor-pointer rounded-md px-1.5 py-1 text-components-button-ghost-text hover:bg-components-button-ghost-bg-hover' onClick={clearProcessStatus}>{t('datasetDocuments.list.batchModal.ok')}</span>
  72. </div>
  73. <div className='absolute left-0 top-0 -z-10 h-full w-full bg-dataset-chunk-process-error-bg opacity-40' />
  74. </div>
  75. )}
  76. </>
  77. )
  78. }
  79. return (
  80. <div className={cn(
  81. 'relative z-20 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]',
  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 rounded-l-lg border-r-[1px] border-r-divider-subtle px-2.5 py-2
  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('h-4 w-4', textColor)} />
  92. <span className={cn('ml-0.5 px-0.5 text-[13px] font-medium capitalize leading-[16px]', textColor)}>
  93. {t('datasetDocuments.list.action.addButton')}
  94. </span>
  95. </button>
  96. <Popover
  97. position='br'
  98. manualClose
  99. trigger='click'
  100. htmlContent={
  101. // need to wrapper the button with div when manualClose is true
  102. <div className='w-full p-1'>
  103. <button
  104. type='button'
  105. className='system-md-regular flex w-full items-center rounded-lg px-2 py-1.5 text-text-secondary'
  106. onClick={showBatchModal}
  107. >
  108. {t('datasetDocuments.list.action.batchAdd')}
  109. </button>
  110. </div>
  111. }
  112. btnElement={
  113. <div className='flex items-center justify-center' >
  114. <RiArrowDownSLine className={cn('h-4 w-4', textColor)}/>
  115. </div>
  116. }
  117. btnClassName={open => cn(
  118. `!hover:bg-state-base-hover !rounded-l-none !rounded-r-lg !border-0 !p-2 backdrop-blur-[5px]
  119. disabled:cursor-not-allowed disabled:bg-transparent disabled:hover:bg-transparent`,
  120. open ? '!bg-state-base-hover' : '',
  121. )}
  122. popupClassName='!min-w-[128px] !bg-components-panel-bg-blur !rounded-xl border-[0.5px] !ring-0
  123. border-components-panel-border !shadow-xl !shadow-shadow-shadow-5 backdrop-blur-[5px]'
  124. className='h-fit min-w-[128px]'
  125. disabled={embedding}
  126. />
  127. </div>
  128. )
  129. }
  130. export default React.memo(SegmentAdd)