SegmentCard.tsx 9.0 KB

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