new-segment-modal.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { memo, 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 Modal from '@/app/components/base/modal'
  7. import Button from '@/app/components/base/button'
  8. import AutoHeightTextarea from '@/app/components/base/auto-height-textarea/common'
  9. import { Hash02, XClose } from '@/app/components/base/icons/src/vender/line/general'
  10. import { ToastContext } from '@/app/components/base/toast'
  11. import type { SegmentUpdator } from '@/models/datasets'
  12. import { addSegment } from '@/service/datasets'
  13. import TagInput from '@/app/components/base/tag-input'
  14. type NewSegmentModalProps = {
  15. isShow: boolean
  16. onCancel: () => void
  17. docForm: string
  18. onSave: () => void
  19. }
  20. const NewSegmentModal: FC<NewSegmentModalProps> = ({
  21. isShow,
  22. onCancel,
  23. docForm,
  24. onSave,
  25. }) => {
  26. const { t } = useTranslation()
  27. const { notify } = useContext(ToastContext)
  28. const [question, setQuestion] = useState('')
  29. const [answer, setAnswer] = useState('')
  30. const { datasetId, documentId } = useParams()
  31. const [keywords, setKeywords] = useState<string[]>([])
  32. const [loading, setLoading] = useState(false)
  33. const handleCancel = () => {
  34. setQuestion('')
  35. setAnswer('')
  36. onCancel()
  37. setKeywords([])
  38. }
  39. const handleSave = async () => {
  40. const params: SegmentUpdator = { content: '' }
  41. if (docForm === 'qa_model') {
  42. if (!question.trim())
  43. return notify({ type: 'error', message: t('datasetDocuments.segment.questionEmpty') })
  44. if (!answer.trim())
  45. return notify({ type: 'error', message: t('datasetDocuments.segment.answerEmpty') })
  46. params.content = question
  47. params.answer = answer
  48. }
  49. else {
  50. if (!question.trim())
  51. return notify({ type: 'error', message: t('datasetDocuments.segment.contentEmpty') })
  52. params.content = question
  53. }
  54. if (keywords?.length)
  55. params.keywords = keywords
  56. setLoading(true)
  57. try {
  58. await addSegment({ datasetId, documentId, body: params })
  59. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  60. handleCancel()
  61. onSave()
  62. }
  63. finally {
  64. setLoading(false)
  65. }
  66. }
  67. const renderContent = () => {
  68. if (docForm === 'qa_model') {
  69. return (
  70. <>
  71. <div className='mb-1 text-xs font-medium text-gray-500'>QUESTION</div>
  72. <AutoHeightTextarea
  73. outerClassName='mb-4'
  74. className='leading-6 text-md text-gray-800'
  75. value={question}
  76. placeholder={t('datasetDocuments.segment.questionPlaceholder') || ''}
  77. onChange={e => setQuestion(e.target.value)}
  78. autoFocus
  79. />
  80. <div className='mb-1 text-xs font-medium text-gray-500'>ANSWER</div>
  81. <AutoHeightTextarea
  82. outerClassName='mb-4'
  83. className='leading-6 text-md text-gray-800'
  84. value={answer}
  85. placeholder={t('datasetDocuments.segment.answerPlaceholder') || ''}
  86. onChange={e => setAnswer(e.target.value)}
  87. />
  88. </>
  89. )
  90. }
  91. return (
  92. <AutoHeightTextarea
  93. className='leading-6 text-md text-gray-800'
  94. value={question}
  95. placeholder={t('datasetDocuments.segment.contentPlaceholder') || ''}
  96. onChange={e => setQuestion(e.target.value)}
  97. autoFocus
  98. />
  99. )
  100. }
  101. return (
  102. <Modal isShow={isShow} onClose={() => {}} className='pt-8 px-8 pb-6 !max-w-[640px] !rounded-xl'>
  103. <div className={'flex flex-col relative'}>
  104. <div className='absolute right-0 -top-0.5 flex items-center h-6'>
  105. <div className='flex justify-center items-center w-6 h-6 cursor-pointer' onClick={handleCancel}>
  106. <XClose className='w-4 h-4 text-gray-500' />
  107. </div>
  108. </div>
  109. <div className='mb-[14px]'>
  110. <span className='inline-flex items-center px-1.5 h-5 border border-gray-200 rounded-md'>
  111. <Hash02 className='mr-0.5 w-3 h-3 text-gray-400' />
  112. <span className='text-[11px] font-medium text-gray-500 italic'>
  113. {
  114. docForm === 'qa_model'
  115. ? t('datasetDocuments.segment.newQaSegment')
  116. : t('datasetDocuments.segment.newTextSegment')
  117. }
  118. </span>
  119. </span>
  120. </div>
  121. <div className='mb-4 py-1.5 h-[420px] overflow-auto'>{renderContent()}</div>
  122. <div className='text-xs font-medium text-gray-500'>{t('datasetDocuments.segment.keywords')}</div>
  123. <div className='mb-8'>
  124. <TagInput items={keywords} onChange={newKeywords => setKeywords(newKeywords)} />
  125. </div>
  126. <div className='flex justify-end'>
  127. <Button
  128. className='mr-2 !h-9 !px-4 !py-2 text-sm font-medium text-gray-700 !rounded-lg'
  129. onClick={handleCancel}>
  130. {t('common.operation.cancel')}
  131. </Button>
  132. <Button
  133. type='primary'
  134. className='!h-9 !px-4 !py-2 text-sm font-medium !rounded-lg'
  135. onClick={handleSave}
  136. disabled={loading}
  137. >
  138. {t('common.operation.save')}
  139. </Button>
  140. </div>
  141. </div>
  142. </Modal>
  143. )
  144. }
  145. export default memo(NewSegmentModal)