new-child-segment.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { memo, useMemo, useRef, useState } from 'react'
  2. import type { FC } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import { useParams } from 'next/navigation'
  6. import { RiCloseLine, RiExpandDiagonalLine } from '@remixicon/react'
  7. import { useShallow } from 'zustand/react/shallow'
  8. import { useDocumentContext } from '../index'
  9. import { SegmentIndexTag } from './common/segment-index-tag'
  10. import ActionButtons from './common/action-buttons'
  11. import ChunkContent from './common/chunk-content'
  12. import AddAnother from './common/add-another'
  13. import Dot from './common/dot'
  14. import { useSegmentListContext } from './index'
  15. import { useStore as useAppStore } from '@/app/components/app/store'
  16. import { ToastContext } from '@/app/components/base/toast'
  17. import { type ChildChunkDetail, ChunkingMode, type SegmentUpdater } from '@/models/datasets'
  18. import classNames from '@/utils/classnames'
  19. import { formatNumber } from '@/utils/format'
  20. import Divider from '@/app/components/base/divider'
  21. import { useAddChildSegment } from '@/service/knowledge/use-segment'
  22. type NewChildSegmentModalProps = {
  23. chunkId: string
  24. onCancel: () => void
  25. onSave: (ChildChunk?: ChildChunkDetail) => void
  26. viewNewlyAddedChildChunk?: () => void
  27. }
  28. const NewChildSegmentModal: FC<NewChildSegmentModalProps> = ({
  29. chunkId,
  30. onCancel,
  31. onSave,
  32. viewNewlyAddedChildChunk,
  33. }) => {
  34. const { t } = useTranslation()
  35. const { notify } = useContext(ToastContext)
  36. const [content, setContent] = useState('')
  37. const { datasetId, documentId } = useParams<{ datasetId: string; documentId: string }>()
  38. const [loading, setLoading] = useState(false)
  39. const [addAnother, setAddAnother] = useState(true)
  40. const fullScreen = useSegmentListContext(s => s.fullScreen)
  41. const toggleFullScreen = useSegmentListContext(s => s.toggleFullScreen)
  42. const { appSidebarExpand } = useAppStore(useShallow(state => ({
  43. appSidebarExpand: state.appSidebarExpand,
  44. })))
  45. const parentMode = useDocumentContext(s => s.parentMode)
  46. const refreshTimer = useRef<any>(null)
  47. const isFullDocMode = useMemo(() => {
  48. return parentMode === 'full-doc'
  49. }, [parentMode])
  50. const CustomButton = <>
  51. <Divider type='vertical' className='h-3 mx-1 bg-divider-regular' />
  52. <button
  53. type='button'
  54. className='text-text-accent system-xs-semibold'
  55. onClick={() => {
  56. clearTimeout(refreshTimer.current)
  57. viewNewlyAddedChildChunk?.()
  58. }}>
  59. {t('common.operation.view')}
  60. </button>
  61. </>
  62. const handleCancel = (actionType: 'esc' | 'add' = 'esc') => {
  63. if (actionType === 'esc' || !addAnother)
  64. onCancel()
  65. }
  66. const { mutateAsync: addChildSegment } = useAddChildSegment()
  67. const handleSave = async () => {
  68. const params: SegmentUpdater = { content: '' }
  69. if (!content.trim())
  70. return notify({ type: 'error', message: t('datasetDocuments.segment.contentEmpty') })
  71. params.content = content
  72. setLoading(true)
  73. await addChildSegment({ datasetId, documentId, segmentId: chunkId, body: params }, {
  74. onSuccess(res) {
  75. notify({
  76. type: 'success',
  77. message: t('datasetDocuments.segment.childChunkAdded'),
  78. className: `!w-[296px] !bottom-0 ${appSidebarExpand === 'expand' ? '!left-[216px]' : '!left-14'}
  79. !top-auto !right-auto !mb-[52px] !ml-11`,
  80. customComponent: isFullDocMode && CustomButton,
  81. })
  82. handleCancel('add')
  83. if (isFullDocMode) {
  84. refreshTimer.current = setTimeout(() => {
  85. onSave()
  86. }, 3000)
  87. }
  88. else {
  89. onSave(res.data)
  90. }
  91. },
  92. onSettled() {
  93. setLoading(false)
  94. },
  95. })
  96. }
  97. const wordCountText = useMemo(() => {
  98. const count = content.length
  99. return `${formatNumber(count)} ${t('datasetDocuments.segment.characters', { count })}`
  100. // eslint-disable-next-line react-hooks/exhaustive-deps
  101. }, [content.length])
  102. return (
  103. <div className={'flex flex-col h-full'}>
  104. <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')}>
  105. <div className='flex flex-col'>
  106. <div className='text-text-primary system-xl-semibold'>{t('datasetDocuments.segment.addChildChunk')}</div>
  107. <div className='flex items-center gap-x-2'>
  108. <SegmentIndexTag label={t('datasetDocuments.segment.newChildChunk') as string} />
  109. <Dot />
  110. <span className='text-text-tertiary system-xs-medium'>{wordCountText}</span>
  111. </div>
  112. </div>
  113. <div className='flex items-center'>
  114. {fullScreen && (
  115. <>
  116. <AddAnother className='mr-3' isChecked={addAnother} onCheck={() => setAddAnother(!addAnother)} />
  117. <ActionButtons
  118. handleCancel={handleCancel.bind(null, 'esc')}
  119. handleSave={handleSave}
  120. loading={loading}
  121. actionType='add'
  122. isChildChunk={true}
  123. />
  124. <Divider type='vertical' className='h-3.5 bg-divider-regular ml-4 mr-2' />
  125. </>
  126. )}
  127. <div className='w-8 h-8 flex justify-center items-center p-1.5 cursor-pointer mr-1' onClick={toggleFullScreen}>
  128. <RiExpandDiagonalLine className='w-4 h-4 text-text-tertiary' />
  129. </div>
  130. <div className='w-8 h-8 flex justify-center items-center p-1.5 cursor-pointer' onClick={handleCancel.bind(null, 'esc')}>
  131. <RiCloseLine className='w-4 h-4 text-text-tertiary' />
  132. </div>
  133. </div>
  134. </div>
  135. <div className={classNames('flex grow w-full', fullScreen ? 'flex-row justify-center px-6 pt-6' : 'py-3 px-4')}>
  136. <div className={classNames('break-all overflow-hidden whitespace-pre-line h-full', fullScreen ? 'w-1/2' : 'w-full')}>
  137. <ChunkContent
  138. docForm={ChunkingMode.parentChild}
  139. question={content}
  140. onQuestionChange={content => setContent(content)}
  141. isEditMode={true}
  142. />
  143. </div>
  144. </div>
  145. {!fullScreen && (
  146. <div className='flex items-center justify-between p-4 pt-3 border-t-[1px] border-t-divider-subtle'>
  147. <AddAnother isChecked={addAnother} onCheck={() => setAddAnother(!addAnother)} />
  148. <ActionButtons
  149. handleCancel={handleCancel.bind(null, 'esc')}
  150. handleSave={handleSave}
  151. loading={loading}
  152. actionType='add'
  153. isChildChunk={true}
  154. />
  155. </div>
  156. )}
  157. </div>
  158. )
  159. }
  160. export default memo(NewChildSegmentModal)