index.tsx 10 KB

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