SegmentCard.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import type { FC } from 'react'
  2. import React, { useState } from 'react'
  3. import cn from 'classnames'
  4. import { ArrowUpRightIcon } from '@heroicons/react/24/outline'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiDeleteBinLine,
  8. RiErrorWarningFill,
  9. } from '@remixicon/react'
  10. import { StatusItem } from '../../list'
  11. import { DocumentTitle } from '../index'
  12. import s from './style.module.css'
  13. import { SegmentIndexTag } from './index'
  14. import Modal from '@/app/components/base/modal'
  15. import Button from '@/app/components/base/button'
  16. import Switch from '@/app/components/base/switch'
  17. import Divider from '@/app/components/base/divider'
  18. import Indicator from '@/app/components/header/indicator'
  19. import { formatNumber } from '@/utils/format'
  20. import type { SegmentDetailModel } from '@/models/datasets'
  21. const ProgressBar: FC<{ percent: number; loading: boolean }> = ({ percent, loading }) => {
  22. return (
  23. <div className={s.progressWrapper}>
  24. <div className={cn(s.progress, loading ? s.progressLoading : '')}>
  25. <div
  26. className={s.progressInner}
  27. style={{ width: `${loading ? 0 : (Math.min(percent, 1) * 100).toFixed(2)}%` }}
  28. />
  29. </div>
  30. <div className={loading ? s.progressTextLoading : s.progressText}>{loading ? null : percent.toFixed(2)}</div>
  31. </div>
  32. )
  33. }
  34. export type UsageScene = 'doc' | 'hitTesting'
  35. type ISegmentCardProps = {
  36. loading: boolean
  37. detail?: SegmentDetailModel & { document: { name: string } }
  38. score?: number
  39. onClick?: () => void
  40. onChangeSwitch?: (segId: string, enabled: boolean) => Promise<void>
  41. onDelete?: (segId: string) => Promise<void>
  42. scene?: UsageScene
  43. className?: string
  44. archived?: boolean
  45. embeddingAvailable?: boolean
  46. }
  47. const SegmentCard: FC<ISegmentCardProps> = ({
  48. detail = {},
  49. score,
  50. onClick,
  51. onChangeSwitch,
  52. onDelete,
  53. loading = true,
  54. scene = 'doc',
  55. className = '',
  56. archived,
  57. embeddingAvailable,
  58. }) => {
  59. const { t } = useTranslation()
  60. const {
  61. id,
  62. position,
  63. enabled,
  64. content,
  65. word_count,
  66. hit_count,
  67. index_node_hash,
  68. answer,
  69. } = detail as Required<ISegmentCardProps>['detail']
  70. const isDocScene = scene === 'doc'
  71. const [showModal, setShowModal] = useState(false)
  72. const renderContent = () => {
  73. if (answer) {
  74. return (
  75. <>
  76. <div className='flex mb-2'>
  77. <div className='mr-2 text-[13px] font-semibold text-gray-400'>Q</div>
  78. <div className='text-[13px]'>{content}</div>
  79. </div>
  80. <div className='flex'>
  81. <div className='mr-2 text-[13px] font-semibold text-gray-400'>A</div>
  82. <div className='text-[13px]'>{answer}</div>
  83. </div>
  84. </>
  85. )
  86. }
  87. return content
  88. }
  89. return (
  90. <div
  91. className={cn(
  92. s.segWrapper,
  93. (isDocScene && !enabled) ? 'bg-gray-25' : '',
  94. 'group',
  95. !loading ? 'pb-4 hover:pb-[10px]' : '',
  96. className,
  97. )}
  98. onClick={() => onClick?.()}
  99. >
  100. <div className={s.segTitleWrapper}>
  101. {isDocScene
  102. ? <>
  103. <SegmentIndexTag positionId={position} className={cn('w-fit group-hover:opacity-100', (isDocScene && !enabled) ? 'opacity-50' : '')} />
  104. <div className={s.segStatusWrapper}>
  105. {loading
  106. ? (
  107. <Indicator
  108. color="gray"
  109. className="bg-gray-200 border-gray-300 shadow-none"
  110. />
  111. )
  112. : (
  113. <>
  114. <StatusItem status={enabled ? 'enabled' : 'disabled'} reverse textCls="text-gray-500 text-xs" />
  115. {embeddingAvailable && (
  116. <div className="hidden group-hover:inline-flex items-center">
  117. <Divider type="vertical" className="!h-2" />
  118. <div
  119. onClick={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) =>
  120. e.stopPropagation()
  121. }
  122. className="inline-flex items-center"
  123. >
  124. <Switch
  125. size='md'
  126. disabled={archived || detail.status !== 'completed'}
  127. defaultValue={enabled}
  128. onChange={async (val) => {
  129. await onChangeSwitch?.(id, val)
  130. }}
  131. />
  132. </div>
  133. </div>
  134. )}
  135. </>
  136. )}
  137. </div>
  138. </>
  139. : (
  140. score !== null
  141. ? (
  142. <div className={s.hitTitleWrapper}>
  143. <div className={cn(s.commonIcon, s.targetIcon, loading ? '!bg-gray-300' : '', '!w-3.5 !h-3.5')} />
  144. <ProgressBar percent={score ?? 0} loading={loading} />
  145. </div>
  146. )
  147. : null
  148. )}
  149. </div>
  150. {loading
  151. ? (
  152. <div className={cn(s.cardLoadingWrapper, s.cardLoadingIcon)}>
  153. <div className={cn(s.cardLoadingBg)} />
  154. </div>
  155. )
  156. : (
  157. isDocScene
  158. ? <>
  159. <div
  160. className={cn(
  161. s.segContent,
  162. enabled ? '' : 'opacity-50',
  163. 'group-hover:text-transparent group-hover:bg-clip-text group-hover:bg-gradient-to-b',
  164. )}
  165. >
  166. {renderContent()}
  167. </div>
  168. <div className={cn('group-hover:flex', s.segData)}>
  169. <div className="flex items-center mr-6">
  170. <div className={cn(s.commonIcon, s.typeSquareIcon)}></div>
  171. <div className={s.segDataText}>{formatNumber(word_count)}</div>
  172. </div>
  173. <div className="flex items-center mr-6">
  174. <div className={cn(s.commonIcon, s.targetIcon)} />
  175. <div className={s.segDataText}>{formatNumber(hit_count)}</div>
  176. </div>
  177. <div className="grow flex items-center">
  178. <div className={cn(s.commonIcon, s.bezierCurveIcon)} />
  179. <div className={s.segDataText}>{index_node_hash}</div>
  180. </div>
  181. {!archived && embeddingAvailable && (
  182. <div className='shrink-0 w-6 h-6 flex items-center justify-center rounded-md hover:bg-red-100 hover:text-red-600 cursor-pointer group/delete' onClick={(e) => {
  183. e.stopPropagation()
  184. setShowModal(true)
  185. }}>
  186. <RiDeleteBinLine className='w-[14px] h-[14px] text-gray-500 group-hover/delete:text-red-600' />
  187. </div>
  188. )}
  189. </div>
  190. </>
  191. : <>
  192. <div className="h-[140px] overflow-hidden text-ellipsis text-sm font-normal text-gray-800">
  193. {renderContent()}
  194. </div>
  195. <div className={cn('w-full bg-gray-50 group-hover:bg-white')}>
  196. <Divider />
  197. <div className="relative flex items-center w-full">
  198. <DocumentTitle
  199. name={detail?.document?.name || ''}
  200. extension={(detail?.document?.name || '').split('.').pop() || 'txt'}
  201. wrapperCls='w-full'
  202. iconCls="!h-4 !w-4 !bg-contain"
  203. textCls="text-xs text-gray-700 !font-normal overflow-hidden whitespace-nowrap text-ellipsis"
  204. />
  205. <div className={cn(s.chartLinkText, 'group-hover:inline-flex')}>
  206. {t('datasetHitTesting.viewChart')}
  207. <ArrowUpRightIcon className="w-3 h-3 ml-1 stroke-current stroke-2" />
  208. </div>
  209. </div>
  210. </div>
  211. </>
  212. )}
  213. {showModal && <Modal isShow={showModal} onClose={() => setShowModal(false)} className={s.delModal} closable>
  214. <div>
  215. <div className={s.warningWrapper}>
  216. <RiErrorWarningFill className='w-6 h-6 text-red-600' />
  217. </div>
  218. <div className='text-xl font-semibold text-gray-900 mb-1'>{t('datasetDocuments.segment.delete')}</div>
  219. <div className='flex gap-2 justify-end'>
  220. <Button onClick={() => setShowModal(false)}>{t('common.operation.cancel')}</Button>
  221. <Button
  222. variant='warning'
  223. onClick={async () => {
  224. await onDelete?.(id)
  225. }}
  226. className='border-red-700'
  227. >
  228. {t('common.operation.sure')}
  229. </Button>
  230. </div>
  231. </div>
  232. </Modal>}
  233. </div>
  234. )
  235. }
  236. export default SegmentCard