segment-detail.tsx 7.0 KB

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