SegmentCard.tsx 8.4 KB

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