segment-detail.tsx 6.9 KB

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