import { memo, useState } from 'react' import type { FC } from 'react' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import { useParams } from 'next/navigation' import { RiCloseLine } from '@remixicon/react' import Modal from '@/app/components/base/modal' import Button from '@/app/components/base/button' import AutoHeightTextarea from '@/app/components/base/auto-height-textarea/common' import { Hash02 } from '@/app/components/base/icons/src/vender/line/general' import { ToastContext } from '@/app/components/base/toast' import type { SegmentUpdater } from '@/models/datasets' import { addSegment } from '@/service/datasets' import TagInput from '@/app/components/base/tag-input' type NewSegmentModalProps = { isShow: boolean onCancel: () => void docForm: string onSave: () => void } const NewSegmentModal: FC = ({ isShow, onCancel, docForm, onSave, }) => { const { t } = useTranslation() const { notify } = useContext(ToastContext) const [question, setQuestion] = useState('') const [answer, setAnswer] = useState('') const { datasetId, documentId } = useParams() const [keywords, setKeywords] = useState([]) const [loading, setLoading] = useState(false) const handleCancel = () => { setQuestion('') setAnswer('') onCancel() setKeywords([]) } const handleSave = async () => { const params: SegmentUpdater = { content: '' } if (docForm === 'qa_model') { if (!question.trim()) return notify({ type: 'error', message: t('datasetDocuments.segment.questionEmpty') }) if (!answer.trim()) return notify({ type: 'error', message: t('datasetDocuments.segment.answerEmpty') }) params.content = question params.answer = answer } else { if (!question.trim()) return notify({ type: 'error', message: t('datasetDocuments.segment.contentEmpty') }) params.content = question } if (keywords?.length) params.keywords = keywords setLoading(true) try { await addSegment({ datasetId, documentId, body: params }) notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) handleCancel() onSave() } finally { setLoading(false) } } const renderContent = () => { if (docForm === 'qa_model') { return ( <>
QUESTION
setQuestion(e.target.value)} autoFocus />
ANSWER
setAnswer(e.target.value)} /> ) } return ( setQuestion(e.target.value)} autoFocus /> ) } return ( { }} className='pt-8 px-8 pb-6 !max-w-[640px] !rounded-xl'>
{ docForm === 'qa_model' ? t('datasetDocuments.segment.newQaSegment') : t('datasetDocuments.segment.newTextSegment') }
{renderContent()}
{t('datasetDocuments.segment.keywords')}
setKeywords(newKeywords)} />
) } export default memo(NewSegmentModal)