new-child-segment.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. setContent('')
  66. }
  67. const { mutateAsync: addChildSegment } = useAddChildSegment()
  68. const handleSave = async () => {
  69. const params: SegmentUpdater = { content: '' }
  70. if (!content.trim())
  71. return notify({ type: 'error', message: t('datasetDocuments.segment.contentEmpty') })
  72. params.content = content
  73. setLoading(true)
  74. await addChildSegment({ datasetId, documentId, segmentId: chunkId, body: params }, {
  75. onSuccess(res) {
  76. notify({
  77. type: 'success',
  78. message: t('datasetDocuments.segment.childChunkAdded'),
  79. className: `!w-[296px] !bottom-0 ${appSidebarExpand === 'expand' ? '!left-[216px]' : '!left-14'}
  80. !top-auto !right-auto !mb-[52px] !ml-11`,
  81. customComponent: isFullDocMode && CustomButton,
  82. })
  83. handleCancel('add')
  84. if (isFullDocMode) {
  85. refreshTimer.current = setTimeout(() => {
  86. onSave()
  87. }, 3000)
  88. }
  89. else {
  90. onSave(res.data)
  91. }
  92. },
  93. onSettled() {
  94. setLoading(false)
  95. },
  96. })
  97. }
  98. const wordCountText = useMemo(() => {
  99. const count = content.length
  100. return `${formatNumber(count)} ${t('datasetDocuments.segment.characters', { count })}`
  101. // eslint-disable-next-line react-hooks/exhaustive-deps
  102. }, [content.length])
  103. return (
  104. <div className={'flex flex-col h-full'}>
  105. <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')}>
  106. <div className='flex flex-col'>
  107. <div className='text-text-primary system-xl-semibold'>{t('datasetDocuments.segment.addChildChunk')}</div>
  108. <div className='flex items-center gap-x-2'>
  109. <SegmentIndexTag label={t('datasetDocuments.segment.newChildChunk') as string} />
  110. <Dot />
  111. <span className='text-text-tertiary system-xs-medium'>{wordCountText}</span>
  112. </div>
  113. </div>
  114. <div className='flex items-center'>
  115. {fullScreen && (
  116. <>
  117. <AddAnother className='mr-3' isChecked={addAnother} onCheck={() => setAddAnother(!addAnother)} />
  118. <ActionButtons
  119. handleCancel={handleCancel.bind(null, 'esc')}
  120. handleSave={handleSave}
  121. loading={loading}
  122. actionType='add'
  123. isChildChunk={true}
  124. />
  125. <Divider type='vertical' className='h-3.5 bg-divider-regular ml-4 mr-2' />
  126. </>
  127. )}
  128. <div className='w-8 h-8 flex justify-center items-center p-1.5 cursor-pointer mr-1' onClick={toggleFullScreen}>
  129. <RiExpandDiagonalLine className='w-4 h-4 text-text-tertiary' />
  130. </div>
  131. <div className='w-8 h-8 flex justify-center items-center p-1.5 cursor-pointer' onClick={handleCancel.bind(null, 'esc')}>
  132. <RiCloseLine className='w-4 h-4 text-text-tertiary' />
  133. </div>
  134. </div>
  135. </div>
  136. <div className={classNames('flex grow w-full', fullScreen ? 'flex-row justify-center px-6 pt-6' : 'py-3 px-4')}>
  137. <div className={classNames('break-all overflow-hidden whitespace-pre-line h-full', fullScreen ? 'w-1/2' : 'w-full')}>
  138. <ChunkContent
  139. docForm={ChunkingMode.parentChild}
  140. question={content}
  141. onQuestionChange={content => setContent(content)}
  142. isEditMode={true}
  143. />
  144. </div>
  145. </div>
  146. {!fullScreen && (
  147. <div className='flex items-center justify-between p-4 pt-3 border-t-[1px] border-t-divider-subtle'>
  148. <AddAnother isChecked={addAnother} onCheck={() => setAddAnother(!addAnother)} />
  149. <ActionButtons
  150. handleCancel={handleCancel.bind(null, 'esc')}
  151. handleSave={handleSave}
  152. loading={loading}
  153. actionType='add'
  154. isChildChunk={true}
  155. />
  156. </div>
  157. )}
  158. </div>
  159. )
  160. }
  161. export default memo(NewChildSegmentModal)