new-segment-modal.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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> = memo(({
  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 handleCancel = () => {
  33. setQuestion('')
  34. setAnswer('')
  35. onCancel()
  36. setKeywords([])
  37. }
  38. const handleSave = async () => {
  39. const params: SegmentUpdator = { content: '' }
  40. if (docForm === 'qa_model') {
  41. if (!question.trim())
  42. return notify({ type: 'error', message: t('datasetDocuments.segment.questionEmpty') })
  43. if (!answer.trim())
  44. return notify({ type: 'error', message: t('datasetDocuments.segment.answerEmpty') })
  45. params.content = question
  46. params.answer = answer
  47. }
  48. else {
  49. if (!question.trim())
  50. return notify({ type: 'error', message: t('datasetDocuments.segment.contentEmpty') })
  51. params.content = question
  52. }
  53. if (keywords?.length)
  54. params.keywords = keywords
  55. await addSegment({ datasetId, documentId, body: params })
  56. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  57. handleCancel()
  58. onSave()
  59. }
  60. const renderContent = () => {
  61. if (docForm === 'qa_model') {
  62. return (
  63. <>
  64. <div className='mb-1 text-xs font-medium text-gray-500'>QUESTION</div>
  65. <AutoHeightTextarea
  66. outerClassName='mb-4'
  67. className='leading-6 text-md text-gray-800'
  68. value={question}
  69. placeholder={t('datasetDocuments.segment.questionPlaceholder') || ''}
  70. onChange={e => setQuestion(e.target.value)}
  71. autoFocus
  72. />
  73. <div className='mb-1 text-xs font-medium text-gray-500'>ANSWER</div>
  74. <AutoHeightTextarea
  75. outerClassName='mb-4'
  76. className='leading-6 text-md text-gray-800'
  77. value={answer}
  78. placeholder={t('datasetDocuments.segment.answerPlaceholder') || ''}
  79. onChange={e => setAnswer(e.target.value)}
  80. />
  81. </>
  82. )
  83. }
  84. return (
  85. <AutoHeightTextarea
  86. className='leading-6 text-md text-gray-800'
  87. value={question}
  88. placeholder={t('datasetDocuments.segment.contentPlaceholder') || ''}
  89. onChange={e => setQuestion(e.target.value)}
  90. autoFocus
  91. />
  92. )
  93. }
  94. return (
  95. <Modal isShow={isShow} onClose={() => {}} className='pt-8 px-8 pb-6 !max-w-[640px] !rounded-xl'>
  96. <div className={'flex flex-col relative'}>
  97. <div className='absolute right-0 -top-0.5 flex items-center h-6'>
  98. <div className='flex justify-center items-center w-6 h-6 cursor-pointer' onClick={handleCancel}>
  99. <XClose className='w-4 h-4 text-gray-500' />
  100. </div>
  101. </div>
  102. <div className='mb-[14px]'>
  103. <span className='inline-flex items-center px-1.5 h-5 border border-gray-200 rounded-md'>
  104. <Hash02 className='mr-0.5 w-3 h-3 text-gray-400' />
  105. <span className='text-[11px] font-medium text-gray-500 italic'>
  106. {
  107. docForm === 'qa_model'
  108. ? t('datasetDocuments.segment.newQaSegment')
  109. : t('datasetDocuments.segment.newTextSegment')
  110. }
  111. </span>
  112. </span>
  113. </div>
  114. <div className='mb-4 py-1.5 h-[420px] overflow-auto'>{renderContent()}</div>
  115. <div className='text-xs font-medium text-gray-500'>{t('datasetDocuments.segment.keywords')}</div>
  116. <div className='mb-8'>
  117. <TagInput items={keywords} onChange={newKeywords => setKeywords(newKeywords)} />
  118. </div>
  119. <div className='flex justify-end'>
  120. <Button
  121. className='mr-2 !h-9 !px-4 !py-2 text-sm font-medium text-gray-700 !rounded-lg'
  122. onClick={handleCancel}>
  123. {t('common.operation.cancel')}
  124. </Button>
  125. <Button
  126. type='primary'
  127. className='!h-9 !px-4 !py-2 text-sm font-medium !rounded-lg'
  128. onClick={handleSave}>
  129. {t('common.operation.save')}
  130. </Button>
  131. </div>
  132. </div>
  133. </Modal>
  134. )
  135. })
  136. export default NewSegmentModal