SegmentCard.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 Switch from "@/app/components/base/switch";
  6. import Divider from "@/app/components/base/divider";
  7. import Indicator from "@/app/components/header/indicator";
  8. import { formatNumber } from "@/utils/format";
  9. import type { SegmentDetailModel } from "@/models/datasets";
  10. import { StatusItem } from "../../list";
  11. import s from "./style.module.css";
  12. import { SegmentIndexTag } from "./index";
  13. import { DocumentTitle } from '../index'
  14. import { useTranslation } from "react-i18next";
  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. } = detail as any;
  57. const isDocScene = scene === 'doc'
  58. return (
  59. <div
  60. className={cn(
  61. s.segWrapper,
  62. isDocScene && !enabled ? "bg-gray-25" : "",
  63. "group",
  64. !loading ? "pb-4" : "",
  65. className,
  66. )}
  67. onClick={() => onClick?.()}
  68. >
  69. <div className={s.segTitleWrapper}>
  70. {isDocScene ? <>
  71. <SegmentIndexTag positionId={position} className={cn("w-fit group-hover:opacity-100", isDocScene && !enabled ? 'opacity-50' : '')} />
  72. <div className={s.segStatusWrapper}>
  73. {loading ? (
  74. <Indicator
  75. color="gray"
  76. className="bg-gray-200 border-gray-300 shadow-none"
  77. />
  78. ) : (
  79. <>
  80. <StatusItem status={enabled ? "enabled" : "disabled"} reverse textCls="text-gray-500 text-xs" />
  81. <div className="hidden group-hover:inline-flex items-center">
  82. <Divider type="vertical" className="!h-2" />
  83. <div
  84. onClick={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) =>
  85. e.stopPropagation()
  86. }
  87. className="inline-flex items-center"
  88. >
  89. <Switch
  90. size='md'
  91. defaultValue={enabled}
  92. onChange={async (val) => {
  93. await onChangeSwitch?.(id, val)
  94. }}
  95. />
  96. </div>
  97. </div>
  98. </>
  99. )}
  100. </div>
  101. </> : <div className={s.hitTitleWrapper}>
  102. <div className={cn(s.commonIcon, s.targetIcon, loading ? '!bg-gray-300' : '', '!w-3.5 !h-3.5')} />
  103. <ProgressBar percent={score ?? 0} loading={loading} />
  104. </div>}
  105. </div>
  106. {loading ? (
  107. <div className={cn(s.cardLoadingWrapper, s.cardLoadingIcon)}>
  108. <div className={cn(s.cardLoadingBg)} />
  109. </div>
  110. ) : (
  111. isDocScene ? <>
  112. <div
  113. className={cn(
  114. s.segContent,
  115. enabled ? "" : "opacity-50",
  116. "group-hover:text-transparent group-hover:bg-clip-text group-hover:bg-gradient-to-b"
  117. )}
  118. >
  119. {content}
  120. </div>
  121. <div className={cn('group-hover:flex', s.segData)}>
  122. <div className="flex items-center mr-6">
  123. <div className={cn(s.commonIcon, s.typeSquareIcon)}></div>
  124. <div className={s.segDataText}>{formatNumber(word_count)}</div>
  125. </div>
  126. <div className="flex items-center mr-6">
  127. <div className={cn(s.commonIcon, s.targetIcon)} />
  128. <div className={s.segDataText}>{formatNumber(hit_count)}</div>
  129. </div>
  130. <div className="flex items-center">
  131. <div className={cn(s.commonIcon, s.bezierCurveIcon)} />
  132. <div className={s.segDataText}>{index_node_hash}</div>
  133. </div>
  134. </div>
  135. </> : <>
  136. <div className="h-[140px] overflow-hidden text-ellipsis text-sm font-normal text-gray-800">
  137. {content}
  138. </div>
  139. <div className={cn("w-full bg-gray-50 group-hover:bg-white")}>
  140. <Divider />
  141. <div className="relative flex items-center w-full">
  142. <DocumentTitle
  143. name={detail?.document?.name || ''}
  144. extension={(detail?.document?.name || '').split('.').pop() || 'txt'}
  145. wrapperCls='w-full'
  146. iconCls="!h-4 !w-4 !bg-contain"
  147. textCls="text-xs text-gray-700 !font-normal overflow-hidden whitespace-nowrap text-ellipsis"
  148. />
  149. <div className={cn(s.chartLinkText, 'group-hover:inline-flex')}>
  150. {t('datasetHitTesting.viewChart')}
  151. <ArrowUpRightIcon className="w-3 h-3 ml-1 stroke-current stroke-2" />
  152. </div>
  153. </div>
  154. </div>
  155. </>
  156. )}
  157. </div>
  158. );
  159. };
  160. export default SegmentCard;