Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 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. 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. sign_content,
  61. word_count,
  62. hit_count,
  63. answer,
  64. keywords,
  65. child_chunks = [],
  66. created_at,
  67. updated_at,
  68. } = detail as Required<ISegmentCardProps>['detail']
  69. const [showModal, setShowModal] = useState(false)
  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 wordCountText = useMemo(() => {
  97. const total = formatNumber(word_count)
  98. return `${total} ${t('datasetDocuments.segment.characters', { count: word_count })}`
  99. }, [word_count, t])
  100. const labelPrefix = useMemo(() => {
  101. return isParentChildMode ? t('datasetDocuments.segment.parentChunk') : t('datasetDocuments.segment.chunk')
  102. }, [isParentChildMode, t])
  103. if (loading)
  104. return <ParentChunkCardSkeleton />
  105. return (
  106. <div
  107. className={cn(
  108. 'group/card w-full rounded-xl px-3',
  109. isFullDocMode ? '' : 'pb-2 pt-2.5 hover:bg-dataset-chunk-detail-card-hover-bg',
  110. focused.segmentContent ? 'bg-dataset-chunk-detail-card-hover-bg' : '',
  111. className,
  112. )}
  113. onClick={handleClickCard}
  114. >
  115. <div className='relative flex h-5 items-center justify-between'>
  116. <>
  117. <div className='flex items-center gap-x-2'>
  118. <SegmentIndexTag
  119. className={cn(contentOpacity)}
  120. iconClassName={focused.segmentIndex ? 'text-text-accent' : ''}
  121. labelClassName={focused.segmentIndex ? 'text-text-accent' : ''}
  122. positionId={position}
  123. label={isFullDocMode ? labelPrefix : ''}
  124. labelPrefix={labelPrefix}
  125. />
  126. <Dot />
  127. <div className={cn('system-xs-medium text-text-tertiary', contentOpacity)}>{wordCountText}</div>
  128. <Dot />
  129. <div className={cn('system-xs-medium text-text-tertiary', contentOpacity)}>{`${formatNumber(hit_count)} ${t('datasetDocuments.segment.hitCount')}`}</div>
  130. {chunkEdited && (
  131. <>
  132. <Dot />
  133. <Badge text={t('datasetDocuments.segment.edited') as string} uppercase className={contentOpacity} />
  134. </>
  135. )}
  136. </div>
  137. {!isFullDocMode
  138. ? <div className='flex items-center'>
  139. <StatusItem status={enabled ? 'enabled' : 'disabled'} reverse textCls="text-text-tertiary system-xs-regular" />
  140. {embeddingAvailable && (
  141. <div className="absolute -right-2.5 -top-2 z-20 hidden items-center gap-x-0.5 rounded-[10px] border-[0.5px]
  142. border-components-actionbar-border bg-components-actionbar-bg p-1 shadow-md backdrop-blur-[5px] group-hover/card:flex">
  143. {!archived && (
  144. <>
  145. <Tooltip
  146. popupContent='Edit'
  147. popupClassName='text-text-secondary system-xs-medium'
  148. >
  149. <div
  150. className='flex h-6 w-6 shrink-0 cursor-pointer items-center justify-center rounded-lg hover:bg-state-base-hover'
  151. onClick={(e) => {
  152. e.stopPropagation()
  153. onClickEdit?.()
  154. }}>
  155. <RiEditLine className='h-4 w-4 text-text-tertiary' />
  156. </div>
  157. </Tooltip>
  158. <Tooltip
  159. popupContent='Delete'
  160. popupClassName='text-text-secondary system-xs-medium'
  161. >
  162. <div className='group/delete flex h-6 w-6 shrink-0 cursor-pointer items-center justify-center rounded-lg hover:bg-state-destructive-hover'
  163. onClick={(e) => {
  164. e.stopPropagation()
  165. setShowModal(true)
  166. }
  167. }>
  168. <RiDeleteBinLine className='h-4 w-4 text-text-tertiary group-hover/delete:text-text-destructive' />
  169. </div>
  170. </Tooltip>
  171. <Divider type="vertical" className="h-3.5 bg-divider-regular" />
  172. </>
  173. )}
  174. <div
  175. onClick={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) =>
  176. e.stopPropagation()
  177. }
  178. className="flex items-center"
  179. >
  180. <Switch
  181. size='md'
  182. disabled={archived || detail?.status !== 'completed'}
  183. defaultValue={enabled}
  184. onChange={async (val) => {
  185. await onChangeSwitch?.(val, id)
  186. }}
  187. />
  188. </div>
  189. </div>
  190. )}
  191. </div>
  192. : null}
  193. </>
  194. </div>
  195. <ChunkContent
  196. detail={{
  197. answer,
  198. content,
  199. sign_content,
  200. }}
  201. isFullDocMode={isFullDocMode}
  202. className={contentOpacity}
  203. />
  204. {isGeneralMode && <div className={cn('flex flex-wrap items-center gap-2 py-1.5', contentOpacity)}>
  205. {keywords?.map(keyword => <Tag key={keyword} text={keyword} />)}
  206. </div>}
  207. {
  208. isFullDocMode
  209. ? <button
  210. type='button'
  211. className='system-xs-semibold-uppercase mb-2 mt-0.5 text-text-accent'
  212. onClick={() => onClick?.()}
  213. >{t('common.operation.viewMore')}</button>
  214. : null
  215. }
  216. {
  217. isParagraphMode && child_chunks.length > 0
  218. && <ChildSegmentList
  219. parentChunkId={id}
  220. childChunks={child_chunks}
  221. enabled={enabled}
  222. onDelete={onDeleteChildChunk!}
  223. handleAddNewChildChunk={handleAddNewChildChunk}
  224. onClickSlice={onClickSlice}
  225. focused={focused.segmentContent}
  226. />
  227. }
  228. {showModal
  229. && <Confirm
  230. isShow={showModal}
  231. title={t('datasetDocuments.segment.delete')}
  232. confirmText={t('common.operation.sure')}
  233. onConfirm={async () => { await onDelete?.(id) }}
  234. onCancel={() => setShowModal(false)}
  235. />
  236. }
  237. </div>
  238. )
  239. }
  240. export default React.memo(SegmentCard)