SegmentCard.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import type { FC } from 'react'
  2. import React 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 Switch from '@/app/components/base/switch'
  11. import Divider from '@/app/components/base/divider'
  12. import Indicator from '@/app/components/header/indicator'
  13. import { formatNumber } from '@/utils/format'
  14. import type { SegmentDetailModel } from '@/models/datasets'
  15. const ProgressBar: FC<{ percent: number; loading: boolean }> = ({ percent, loading }) => {
  16. return (
  17. <div className={s.progressWrapper}>
  18. <div className={cn(s.progress, loading ? s.progressLoading : '')}>
  19. <div
  20. className={s.progressInner}
  21. style={{ width: `${loading ? 0 : (percent * 100).toFixed(2)}%` }}
  22. />
  23. </div>
  24. <div className={loading ? s.progressTextLoading : s.progressText}>{loading ? null : percent.toFixed(2)}</div>
  25. </div>
  26. )
  27. }
  28. export type UsageScene = 'doc' | 'hitTesting'
  29. type ISegmentCardProps = {
  30. loading: boolean
  31. detail?: SegmentDetailModel & { document: { name: string } }
  32. score?: number
  33. onClick?: () => void
  34. onChangeSwitch?: (segId: string, enabled: boolean) => Promise<void>
  35. scene?: UsageScene
  36. className?: string
  37. }
  38. const SegmentCard: FC<ISegmentCardProps> = ({
  39. detail = {},
  40. score,
  41. onClick,
  42. onChangeSwitch,
  43. loading = true,
  44. scene = 'doc',
  45. className = '',
  46. }) => {
  47. const { t } = useTranslation()
  48. const {
  49. id,
  50. position,
  51. enabled,
  52. content,
  53. word_count,
  54. hit_count,
  55. index_node_hash,
  56. answer,
  57. } = detail as any
  58. const isDocScene = scene === 'doc'
  59. const renderContent = () => {
  60. if (answer) {
  61. return (
  62. <>
  63. <div className='flex mb-2'>
  64. <div className='mr-2 text-[13px] font-semibold text-gray-400'>Q</div>
  65. <div className='text-[13px]'>{content}</div>
  66. </div>
  67. <div className='flex'>
  68. <div className='mr-2 text-[13px] font-semibold text-gray-400'>A</div>
  69. <div className='text-[13px]'>{answer}</div>
  70. </div>
  71. </>
  72. )
  73. }
  74. return content
  75. }
  76. return (
  77. <div
  78. className={cn(
  79. s.segWrapper,
  80. (isDocScene && !enabled) ? 'bg-gray-25' : '',
  81. 'group',
  82. !loading ? 'pb-4' : '',
  83. className,
  84. )}
  85. onClick={() => onClick?.()}
  86. >
  87. <div className={s.segTitleWrapper}>
  88. {isDocScene
  89. ? <>
  90. <SegmentIndexTag positionId={position} className={cn('w-fit group-hover:opacity-100', (isDocScene && !enabled) ? 'opacity-50' : '')} />
  91. <div className={s.segStatusWrapper}>
  92. {loading
  93. ? (
  94. <Indicator
  95. color="gray"
  96. className="bg-gray-200 border-gray-300 shadow-none"
  97. />
  98. )
  99. : (
  100. <>
  101. <StatusItem status={enabled ? 'enabled' : 'disabled'} reverse textCls="text-gray-500 text-xs" />
  102. <div className="hidden group-hover:inline-flex items-center">
  103. <Divider type="vertical" className="!h-2" />
  104. <div
  105. onClick={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) =>
  106. e.stopPropagation()
  107. }
  108. className="inline-flex items-center"
  109. >
  110. <Switch
  111. size='md'
  112. defaultValue={enabled}
  113. onChange={async (val) => {
  114. await onChangeSwitch?.(id, val)
  115. }}
  116. />
  117. </div>
  118. </div>
  119. </>
  120. )}
  121. </div>
  122. </>
  123. : <div className={s.hitTitleWrapper}>
  124. <div className={cn(s.commonIcon, s.targetIcon, loading ? '!bg-gray-300' : '', '!w-3.5 !h-3.5')} />
  125. <ProgressBar percent={score ?? 0} loading={loading} />
  126. </div>}
  127. </div>
  128. {loading
  129. ? (
  130. <div className={cn(s.cardLoadingWrapper, s.cardLoadingIcon)}>
  131. <div className={cn(s.cardLoadingBg)} />
  132. </div>
  133. )
  134. : (
  135. isDocScene
  136. ? <>
  137. <div
  138. className={cn(
  139. s.segContent,
  140. enabled ? '' : 'opacity-50',
  141. 'group-hover:text-transparent group-hover:bg-clip-text group-hover:bg-gradient-to-b',
  142. )}
  143. >
  144. {renderContent()}
  145. </div>
  146. <div className={cn('group-hover:flex', s.segData)}>
  147. <div className="flex items-center mr-6">
  148. <div className={cn(s.commonIcon, s.typeSquareIcon)}></div>
  149. <div className={s.segDataText}>{formatNumber(word_count)}</div>
  150. </div>
  151. <div className="flex items-center mr-6">
  152. <div className={cn(s.commonIcon, s.targetIcon)} />
  153. <div className={s.segDataText}>{formatNumber(hit_count)}</div>
  154. </div>
  155. <div className="flex items-center">
  156. <div className={cn(s.commonIcon, s.bezierCurveIcon)} />
  157. <div className={s.segDataText}>{index_node_hash}</div>
  158. </div>
  159. </div>
  160. </>
  161. : <>
  162. <div className="h-[140px] overflow-hidden text-ellipsis text-sm font-normal text-gray-800">
  163. {renderContent()}
  164. </div>
  165. <div className={cn('w-full bg-gray-50 group-hover:bg-white')}>
  166. <Divider />
  167. <div className="relative flex items-center w-full">
  168. <DocumentTitle
  169. name={detail?.document?.name || ''}
  170. extension={(detail?.document?.name || '').split('.').pop() || 'txt'}
  171. wrapperCls='w-full'
  172. iconCls="!h-4 !w-4 !bg-contain"
  173. textCls="text-xs text-gray-700 !font-normal overflow-hidden whitespace-nowrap text-ellipsis"
  174. />
  175. <div className={cn(s.chartLinkText, 'group-hover:inline-flex')}>
  176. {t('datasetHitTesting.viewChart')}
  177. <ArrowUpRightIcon className="w-3 h-3 ml-1 stroke-current stroke-2" />
  178. </div>
  179. </div>
  180. </div>
  181. </>
  182. )}
  183. </div>
  184. )
  185. }
  186. export default SegmentCard