segment-detail.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  57. const handleSave = () => {
  58. onUpdate(segInfo?.id || '', question, answer, keywords)
  59. }
  60. const handleRegeneration = () => {
  61. setShowRegenerationModal(true)
  62. }
  63. const onCancelRegeneration = () => {
  64. setShowRegenerationModal(false)
  65. }
  66. const onConfirmRegeneration = () => {
  67. onUpdate(segInfo?.id || '', question, answer, keywords, true)
  68. }
  69. const isParentChildMode = useMemo(() => {
  70. return mode === 'hierarchical'
  71. }, [mode])
  72. const isFullDocMode = useMemo(() => {
  73. return mode === 'hierarchical' && parentMode === 'full-doc'
  74. }, [mode, parentMode])
  75. const titleText = useMemo(() => {
  76. return isEditMode ? t('datasetDocuments.segment.editChunk') : t('datasetDocuments.segment.chunkDetail')
  77. // eslint-disable-next-line react-hooks/exhaustive-deps
  78. }, [isEditMode])
  79. const isQAModel = useMemo(() => {
  80. return docForm === ChunkingMode.qa
  81. }, [docForm])
  82. const wordCountText = useMemo(() => {
  83. const contentLength = isQAModel ? (question.length + answer.length) : question.length
  84. const total = formatNumber(isEditMode ? contentLength : segInfo!.word_count as number)
  85. const count = isEditMode ? contentLength : segInfo!.word_count as number
  86. return `${total} ${t('datasetDocuments.segment.characters', { count })}`
  87. // eslint-disable-next-line react-hooks/exhaustive-deps
  88. }, [isEditMode, question.length, answer.length, segInfo?.word_count, isQAModel])
  89. const labelPrefix = useMemo(() => {
  90. return isParentChildMode ? t('datasetDocuments.segment.parentChunk') : t('datasetDocuments.segment.chunk')
  91. // eslint-disable-next-line react-hooks/exhaustive-deps
  92. }, [isParentChildMode])
  93. return (
  94. <div className={'flex flex-col h-full'}>
  95. <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')}>
  96. <div className='flex flex-col'>
  97. <div className='text-text-primary system-xl-semibold'>{titleText}</div>
  98. <div className='flex items-center gap-x-2'>
  99. <SegmentIndexTag positionId={segInfo?.position || ''} label={isFullDocMode ? labelPrefix : ''} labelPrefix={labelPrefix} />
  100. <Dot />
  101. <span className='text-text-tertiary system-xs-medium'>{wordCountText}</span>
  102. </div>
  103. </div>
  104. <div className='flex items-center'>
  105. {isEditMode && fullScreen && (
  106. <>
  107. <ActionButtons
  108. handleCancel={handleCancel}
  109. handleRegeneration={handleRegeneration}
  110. handleSave={handleSave}
  111. loading={loading}
  112. />
  113. <Divider type='vertical' className='h-3.5 bg-divider-regular ml-4 mr-2' />
  114. </>
  115. )}
  116. <div className='w-8 h-8 flex justify-center items-center p-1.5 cursor-pointer mr-1' onClick={toggleFullScreen}>
  117. <RiExpandDiagonalLine className='w-4 h-4 text-text-tertiary' />
  118. </div>
  119. <div className='w-8 h-8 flex justify-center items-center p-1.5 cursor-pointer' onClick={onCancel}>
  120. <RiCloseLine className='w-4 h-4 text-text-tertiary' />
  121. </div>
  122. </div>
  123. </div>
  124. <div className={classNames(
  125. 'flex grow',
  126. fullScreen ? 'w-full flex-row justify-center px-6 pt-6 gap-x-8' : 'flex-col gap-y-1 py-3 px-4',
  127. !isEditMode && 'pb-0 overflow-hidden',
  128. )}>
  129. <div className={classNames(isEditMode ? 'break-all whitespace-pre-line overflow-hidden' : 'overflow-y-auto', fullScreen ? 'w-1/2' : 'grow')}>
  130. <ChunkContent
  131. docForm={docForm}
  132. question={question}
  133. answer={answer}
  134. onQuestionChange={question => setQuestion(question)}
  135. onAnswerChange={answer => setAnswer(answer)}
  136. isEditMode={isEditMode}
  137. />
  138. </div>
  139. {mode === 'custom' && <Keywords
  140. className={fullScreen ? 'w-1/5' : ''}
  141. actionType={isEditMode ? 'edit' : 'view'}
  142. segInfo={segInfo}
  143. keywords={keywords}
  144. isEditMode={isEditMode}
  145. onKeywordsChange={keywords => setKeywords(keywords)}
  146. />}
  147. </div>
  148. {isEditMode && !fullScreen && (
  149. <div className='flex items-center justify-end p-4 pt-3 border-t-[1px] border-t-divider-subtle'>
  150. <ActionButtons
  151. handleCancel={handleCancel}
  152. handleRegeneration={handleRegeneration}
  153. handleSave={handleSave}
  154. loading={loading}
  155. />
  156. </div>
  157. )}
  158. {
  159. showRegenerationModal && (
  160. <RegenerationModal
  161. isShow={showRegenerationModal}
  162. onConfirm={onConfirmRegeneration}
  163. onCancel={onCancelRegeneration}
  164. onClose={onCancelRegeneration}
  165. />
  166. )
  167. }
  168. </div>
  169. )
  170. }
  171. export default React.memo(SegmentDetail)