segment-card.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import React, { type FC, useCallback, useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { RiDeleteBinLine, RiEditLine } from '@remixicon/react'
  4. import { StatusItem } from '../../list'
  5. import { useDocumentContext } from '../index'
  6. import ChildSegmentList from './child-segment-list'
  7. import Tag from './common/tag'
  8. import Dot from './common/dot'
  9. import { SegmentIndexTag } from './common/segment-index-tag'
  10. import ParentChunkCardSkeleton from './skeleton/parent-chunk-card-skeleton'
  11. import { useSegmentListContext } from './index'
  12. import type { ChildChunkDetail, SegmentDetailModel } from '@/models/datasets'
  13. import Switch from '@/app/components/base/switch'
  14. import Divider from '@/app/components/base/divider'
  15. import { formatNumber } from '@/utils/format'
  16. import Confirm from '@/app/components/base/confirm'
  17. import cn from '@/utils/classnames'
  18. import Badge from '@/app/components/base/badge'
  19. import { isAfter } from '@/utils/time'
  20. import Tooltip from '@/app/components/base/tooltip'
  21. type ISegmentCardProps = {
  22. loading: boolean
  23. detail?: SegmentDetailModel & { document?: { name: string } }
  24. onClick?: () => void
  25. onChangeSwitch?: (enabled: boolean, segId?: string) => Promise<void>
  26. onDelete?: (segId: string) => Promise<void>
  27. onDeleteChildChunk?: (segId: string, childChunkId: string) => Promise<void>
  28. handleAddNewChildChunk?: (parentChunkId: string) => void
  29. onClickSlice?: (childChunk: ChildChunkDetail) => void
  30. onClickEdit?: () => void
  31. className?: string
  32. archived?: boolean
  33. embeddingAvailable?: boolean
  34. focused: {
  35. segmentIndex: boolean
  36. segmentContent: boolean
  37. }
  38. }
  39. const SegmentCard: FC<ISegmentCardProps> = ({
  40. detail = {},
  41. onClick,
  42. onChangeSwitch,
  43. onDelete,
  44. onDeleteChildChunk,
  45. handleAddNewChildChunk,
  46. onClickSlice,
  47. onClickEdit,
  48. loading = true,
  49. className = '',
  50. archived,
  51. embeddingAvailable,
  52. focused,
  53. }) => {
  54. const { t } = useTranslation()
  55. const {
  56. id,
  57. position,
  58. enabled,
  59. content,
  60. word_count,
  61. hit_count,
  62. answer,
  63. keywords,
  64. child_chunks = [],
  65. created_at,
  66. updated_at,
  67. } = detail as Required<ISegmentCardProps>['detail']
  68. const [showModal, setShowModal] = useState(false)
  69. const isCollapsed = useSegmentListContext(s => s.isCollapsed)
  70. const mode = useDocumentContext(s => s.mode)
  71. const parentMode = useDocumentContext(s => s.parentMode)
  72. const isGeneralMode = useMemo(() => {
  73. return mode === 'custom'
  74. }, [mode])
  75. const isParentChildMode = useMemo(() => {
  76. return mode === 'hierarchical'
  77. }, [mode])
  78. const isParagraphMode = useMemo(() => {
  79. return mode === 'hierarchical' && parentMode === 'paragraph'
  80. }, [mode, parentMode])
  81. const isFullDocMode = useMemo(() => {
  82. return mode === 'hierarchical' && parentMode === 'full-doc'
  83. }, [mode, parentMode])
  84. const chunkEdited = useMemo(() => {
  85. if (mode === 'hierarchical' && parentMode === 'full-doc')
  86. return false
  87. return isAfter(updated_at * 1000, created_at * 1000)
  88. }, [mode, parentMode, updated_at, created_at])
  89. const contentOpacity = useMemo(() => {
  90. return (enabled || focused.segmentContent) ? '' : 'opacity-50 group-hover/card:opacity-100'
  91. }, [enabled, focused.segmentContent])
  92. const handleClickCard = useCallback(() => {
  93. if (mode !== 'hierarchical' || parentMode !== 'full-doc')
  94. onClick?.()
  95. }, [mode, parentMode, onClick])
  96. const renderContent = () => {
  97. if (answer) {
  98. return (
  99. <>
  100. <div className='flex gap-x-1'>
  101. <div className='w-4 text-[13px] font-medium leading-[20px] text-text-tertiary shrink-0'>Q</div>
  102. <div
  103. className={cn('text-text-secondary body-md-regular',
  104. isCollapsed ? 'line-clamp-2' : 'line-clamp-20',
  105. )}>
  106. {content}
  107. </div>
  108. </div>
  109. <div className='flex gap-x-1'>
  110. <div className='w-4 text-[13px] font-medium leading-[20px] text-text-tertiary shrink-0'>A</div>
  111. <div className={cn('text-text-secondary body-md-regular',
  112. isCollapsed ? 'line-clamp-2' : 'line-clamp-20',
  113. )}>
  114. {answer}
  115. </div>
  116. </div>
  117. </>
  118. )
  119. }
  120. return content
  121. }
  122. const wordCountText = useMemo(() => {
  123. const total = formatNumber(word_count)
  124. return `${total} ${t('datasetDocuments.segment.characters', { count: word_count })}`
  125. // eslint-disable-next-line react-hooks/exhaustive-deps
  126. }, [word_count])
  127. const labelPrefix = useMemo(() => {
  128. return isParentChildMode ? t('datasetDocuments.segment.parentChunk') : t('datasetDocuments.segment.chunk')
  129. // eslint-disable-next-line react-hooks/exhaustive-deps
  130. }, [isParentChildMode])
  131. if (loading)
  132. return <ParentChunkCardSkeleton />
  133. return (
  134. <div
  135. className={cn(
  136. 'w-full px-3 rounded-xl group/card',
  137. isFullDocMode ? '' : 'pt-2.5 pb-2 hover:bg-dataset-chunk-detail-card-hover-bg',
  138. focused.segmentContent ? 'bg-dataset-chunk-detail-card-hover-bg' : '',
  139. className,
  140. )}
  141. onClick={handleClickCard}
  142. >
  143. <div className='h-5 relative flex items-center justify-between'>
  144. <>
  145. <div className='flex items-center gap-x-2'>
  146. <SegmentIndexTag
  147. className={cn(contentOpacity)}
  148. iconClassName={focused.segmentIndex ? 'text-text-accent' : ''}
  149. labelClassName={focused.segmentIndex ? 'text-text-accent' : ''}
  150. positionId={position}
  151. label={isFullDocMode ? labelPrefix : ''}
  152. labelPrefix={labelPrefix}
  153. />
  154. <Dot />
  155. <div className={cn('text-text-tertiary system-xs-medium', contentOpacity)}>{wordCountText}</div>
  156. <Dot />
  157. <div className={cn('text-text-tertiary system-xs-medium', contentOpacity)}>{`${formatNumber(hit_count)} ${t('datasetDocuments.segment.hitCount')}`}</div>
  158. {chunkEdited && (
  159. <>
  160. <Dot />
  161. <Badge text={t('datasetDocuments.segment.edited') as string} uppercase className={contentOpacity} />
  162. </>
  163. )}
  164. </div>
  165. {!isFullDocMode
  166. ? <div className='flex items-center'>
  167. <StatusItem status={enabled ? 'enabled' : 'disabled'} reverse textCls="text-text-tertiary system-xs-regular" />
  168. {embeddingAvailable && (
  169. <div className="absolute -top-2 -right-2.5 z-20 hidden group-hover/card:flex items-center gap-x-0.5 p-1
  170. rounded-[10px] border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg shadow-md backdrop-blur-[5px]">
  171. {!archived && (
  172. <>
  173. <Tooltip
  174. popupContent='Edit'
  175. popupClassName='text-text-secondary system-xs-medium'
  176. >
  177. <div
  178. className='shrink-0 w-6 h-6 flex items-center justify-center rounded-lg hover:bg-state-base-hover cursor-pointer'
  179. onClick={(e) => {
  180. e.stopPropagation()
  181. onClickEdit?.()
  182. }}>
  183. <RiEditLine className='w-4 h-4 text-text-tertiary' />
  184. </div>
  185. </Tooltip>
  186. <Tooltip
  187. popupContent='Delete'
  188. popupClassName='text-text-secondary system-xs-medium'
  189. >
  190. <div className='shrink-0 w-6 h-6 flex items-center justify-center rounded-lg hover:bg-state-destructive-hover cursor-pointer group/delete'
  191. onClick={(e) => {
  192. e.stopPropagation()
  193. setShowModal(true)
  194. }
  195. }>
  196. <RiDeleteBinLine className='w-4 h-4 text-text-tertiary group-hover/delete:text-text-destructive' />
  197. </div>
  198. </Tooltip>
  199. <Divider type="vertical" className="h-3.5 bg-divider-regular" />
  200. </>
  201. )}
  202. <div
  203. onClick={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) =>
  204. e.stopPropagation()
  205. }
  206. className="flex items-center"
  207. >
  208. <Switch
  209. size='md'
  210. disabled={archived || detail?.status !== 'completed'}
  211. defaultValue={enabled}
  212. onChange={async (val) => {
  213. await onChangeSwitch?.(val, id)
  214. }}
  215. />
  216. </div>
  217. </div>
  218. )}
  219. </div>
  220. : null}
  221. </>
  222. </div>
  223. <div className={cn('text-text-secondary body-md-regular -tracking-[0.07px] mt-0.5',
  224. contentOpacity,
  225. isFullDocMode ? 'line-clamp-3' : isCollapsed ? 'line-clamp-2' : 'line-clamp-20',
  226. )}>
  227. {renderContent()}
  228. </div>
  229. {isGeneralMode && <div className={cn('flex flex-wrap items-center gap-2 py-1.5', contentOpacity)}>
  230. {keywords?.map(keyword => <Tag key={keyword} text={keyword} />)}
  231. </div>}
  232. {
  233. isFullDocMode
  234. ? <button
  235. type='button'
  236. className='mt-0.5 mb-2 text-text-accent system-xs-semibold-uppercase'
  237. onClick={() => onClick?.()}
  238. >{t('common.operation.viewMore')}</button>
  239. : null
  240. }
  241. {
  242. isParagraphMode && child_chunks.length > 0
  243. && <ChildSegmentList
  244. parentChunkId={id}
  245. childChunks={child_chunks}
  246. enabled={enabled}
  247. onDelete={onDeleteChildChunk!}
  248. handleAddNewChildChunk={handleAddNewChildChunk}
  249. onClickSlice={onClickSlice}
  250. focused={focused.segmentContent}
  251. />
  252. }
  253. {showModal
  254. && <Confirm
  255. isShow={showModal}
  256. title={t('datasetDocuments.segment.delete')}
  257. confirmText={t('common.operation.sure')}
  258. onConfirm={async () => { await onDelete?.(id) }}
  259. onCancel={() => setShowModal(false)}
  260. />
  261. }
  262. </div>
  263. )
  264. }
  265. export default React.memo(SegmentCard)