SegmentCard.tsx 8.7 KB

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