segment-detail.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { type FC, useCallback, useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import {
  4. RiCloseLine,
  5. RiCollapseDiagonalLine,
  6. RiExpandDiagonalLine,
  7. } from '@remixicon/react'
  8. import { useDocumentContext } from '../index'
  9. import ActionButtons from './common/action-buttons'
  10. import ChunkContent from './common/chunk-content'
  11. import Keywords from './common/keywords'
  12. import RegenerationModal from './common/regeneration-modal'
  13. import { SegmentIndexTag } from './common/segment-index-tag'
  14. import Dot from './common/dot'
  15. import { useSegmentListContext } from './index'
  16. import { ChunkingMode, type SegmentDetailModel } from '@/models/datasets'
  17. import { useEventEmitterContextContext } from '@/context/event-emitter'
  18. import { formatNumber } from '@/utils/format'
  19. import cn from '@/utils/classnames'
  20. import Divider from '@/app/components/base/divider'
  21. import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
  22. import { IndexingType } from '../../../create/step-two'
  23. type ISegmentDetailProps = {
  24. segInfo?: Partial<SegmentDetailModel> & { id: string }
  25. onUpdate: (segmentId: string, q: string, a: string, k: string[], needRegenerate?: boolean) => void
  26. onCancel: () => void
  27. isEditMode?: boolean
  28. docForm: ChunkingMode
  29. }
  30. /**
  31. * Show all the contents of the segment
  32. */
  33. const SegmentDetail: FC<ISegmentDetailProps> = ({
  34. segInfo,
  35. onUpdate,
  36. onCancel,
  37. isEditMode,
  38. docForm,
  39. }) => {
  40. const { t } = useTranslation()
  41. const [question, setQuestion] = useState(isEditMode ? segInfo?.content || '' : segInfo?.sign_content || '')
  42. const [answer, setAnswer] = useState(segInfo?.answer || '')
  43. const [keywords, setKeywords] = useState<string[]>(segInfo?.keywords || [])
  44. const { eventEmitter } = useEventEmitterContextContext()
  45. const [loading, setLoading] = useState(false)
  46. const [showRegenerationModal, setShowRegenerationModal] = useState(false)
  47. const fullScreen = useSegmentListContext(s => s.fullScreen)
  48. const toggleFullScreen = useSegmentListContext(s => s.toggleFullScreen)
  49. const mode = useDocumentContext(s => s.mode)
  50. const parentMode = useDocumentContext(s => s.parentMode)
  51. const indexingTechnique = useDatasetDetailContextWithSelector(s => s.dataset?.indexing_technique)
  52. eventEmitter?.useSubscription((v) => {
  53. if (v === 'update-segment')
  54. setLoading(true)
  55. if (v === 'update-segment-done')
  56. setLoading(false)
  57. })
  58. const handleCancel = useCallback(() => {
  59. onCancel()
  60. }, [onCancel])
  61. const handleSave = useCallback(() => {
  62. onUpdate(segInfo?.id || '', question, answer, keywords)
  63. }, [onUpdate, segInfo?.id, question, answer, keywords])
  64. const handleRegeneration = useCallback(() => {
  65. setShowRegenerationModal(true)
  66. }, [])
  67. const onCancelRegeneration = useCallback(() => {
  68. setShowRegenerationModal(false)
  69. }, [])
  70. const onConfirmRegeneration = useCallback(() => {
  71. onUpdate(segInfo?.id || '', question, answer, keywords, true)
  72. }, [onUpdate, segInfo?.id, question, answer, keywords])
  73. const wordCountText = useMemo(() => {
  74. const contentLength = docForm === ChunkingMode.qa ? (question.length + answer.length) : question.length
  75. const total = formatNumber(isEditMode ? contentLength : segInfo!.word_count as number)
  76. const count = isEditMode ? contentLength : segInfo!.word_count as number
  77. return `${total} ${t('datasetDocuments.segment.characters', { count })}`
  78. }, [isEditMode, question.length, answer.length, docForm, segInfo, t])
  79. const isFullDocMode = mode === 'hierarchical' && parentMode === 'full-doc'
  80. const titleText = isEditMode ? t('datasetDocuments.segment.editChunk') : t('datasetDocuments.segment.chunkDetail')
  81. const labelPrefix = mode === 'hierarchical' ? t('datasetDocuments.segment.parentChunk') : t('datasetDocuments.segment.chunk')
  82. const isECOIndexing = indexingTechnique === IndexingType.ECONOMICAL
  83. return (
  84. <div className={'flex h-full flex-col'}>
  85. <div className={cn('flex items-center justify-between', fullScreen ? 'border border-divider-subtle py-3 pl-6 pr-4' : 'pl-4 pr-3 pt-3')}>
  86. <div className='flex flex-col'>
  87. <div className='system-xl-semibold text-text-primary'>{titleText}</div>
  88. <div className='flex items-center gap-x-2'>
  89. <SegmentIndexTag positionId={segInfo?.position || ''} label={isFullDocMode ? labelPrefix : ''} labelPrefix={labelPrefix} />
  90. <Dot />
  91. <span className='system-xs-medium text-text-tertiary'>{wordCountText}</span>
  92. </div>
  93. </div>
  94. <div className='flex items-center'>
  95. {isEditMode && fullScreen && (
  96. <>
  97. <ActionButtons
  98. handleCancel={handleCancel}
  99. handleRegeneration={handleRegeneration}
  100. handleSave={handleSave}
  101. loading={loading}
  102. />
  103. <Divider type='vertical' className='ml-4 mr-2 h-3.5 bg-divider-regular' />
  104. </>
  105. )}
  106. <div className='mr-1 flex h-8 w-8 cursor-pointer items-center justify-center p-1.5' onClick={toggleFullScreen}>
  107. {fullScreen ? <RiCollapseDiagonalLine className='h-4 w-4 text-text-tertiary' /> : <RiExpandDiagonalLine className='h-4 w-4 text-text-tertiary' />}
  108. </div>
  109. <div className='flex h-8 w-8 cursor-pointer items-center justify-center p-1.5' onClick={onCancel}>
  110. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  111. </div>
  112. </div>
  113. </div>
  114. <div className={cn(
  115. 'flex grow',
  116. fullScreen ? 'w-full flex-row justify-center gap-x-8 px-6 pt-6' : 'flex-col gap-y-1 px-4 py-3',
  117. !isEditMode && 'overflow-hidden pb-0',
  118. )}>
  119. <div className={cn(isEditMode ? 'overflow-hidden whitespace-pre-line break-all' : 'overflow-y-auto', fullScreen ? 'w-1/2' : 'grow')}>
  120. <ChunkContent
  121. docForm={docForm}
  122. question={question}
  123. answer={answer}
  124. onQuestionChange={question => setQuestion(question)}
  125. onAnswerChange={answer => setAnswer(answer)}
  126. isEditMode={isEditMode}
  127. />
  128. </div>
  129. {isECOIndexing && <Keywords
  130. className={fullScreen ? 'w-1/5' : ''}
  131. actionType={isEditMode ? 'edit' : 'view'}
  132. segInfo={segInfo}
  133. keywords={keywords}
  134. isEditMode={isEditMode}
  135. onKeywordsChange={keywords => setKeywords(keywords)}
  136. />}
  137. </div>
  138. {isEditMode && !fullScreen && (
  139. <div className='flex items-center justify-end border-t-[1px] border-t-divider-subtle p-4 pt-3'>
  140. <ActionButtons
  141. handleCancel={handleCancel}
  142. handleRegeneration={handleRegeneration}
  143. handleSave={handleSave}
  144. loading={loading}
  145. />
  146. </div>
  147. )}
  148. {
  149. showRegenerationModal && (
  150. <RegenerationModal
  151. isShow={showRegenerationModal}
  152. onConfirm={onConfirmRegeneration}
  153. onCancel={onCancelRegeneration}
  154. onClose={onCancelRegeneration}
  155. />
  156. )
  157. }
  158. </div>
  159. )
  160. }
  161. export default React.memo(SegmentDetail)