'use client' import React, { useCallback, useState } from 'react' import { RiCloseLine } from '@remixicon/react' import Modal from '@/app/components/base/modal' import Button from '@/app/components/base/button' import { delCorpusQuestion, fetchIntent, fetchIntentType } from '@/service/common' import 'react-multi-email/dist/style.css' import Input from '@/app/components/base/input' import { SimpleSelect } from '@/app/components/base/select' import useSWR from 'swr' import { v4 as uuid4 } from 'uuid' import Checkbox from '@/app/components/base/checkbox' import cn from '@/utils/classnames' import { useContext } from 'use-context-selector' import { ToastContext } from '@/app/components/base/toast' import Confirm from '@/app/components/base/confirm' const DetailModal = ({ transfer, onCancel, onSend, }: any) => { const { notify } = useContext(ToastContext) const [questionRelation, setQuestionRelation] = useState('') const [questionFilter, setQuestionFilter] = useState('') // the input value const [question, setQuestion] = useState(transfer.row?.question || '') // the input value const [intentType, setIntentType] = useState(transfer.row?.intentType || '') // the input value const { data: dataOptionsIntentType }: any = useSWR( { url: '/xxx', params: { page: 1, limit: 1000, }, }, fetchIntentType, ) const optionsIntentType: any = dataOptionsIntentType?.data.map((v: any) => ({ name: v.name, value: v.id })) || [] const [intentName, setIntentName] = useState(transfer.row?.intentName || '') // the input value const { data: dataOptionsIntentName }: any = useSWR( { url: '/xxx', params: { page: 1, limit: 1000, intentType, }, }, fetchIntent, ) const optionsIntentName: any = dataOptionsIntentName?.data.map((v: any) => ({ name: v.name, value: v.id })) || [] const [questionList, setQuestionList] = useState([{ id: uuid4(), name: '啊啊啊啊啊啊啊啊啊啊' }, { id: uuid4(), name: '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊' }]) const handleAddQuestion = () => { if (!questionRelation) return if (questionList.some((v: any) => v.name === questionRelation)) { notify({ type: 'warning', message: '请勿新增重复数据!' }) return } setQuestionList([...questionList, { id: uuid4(), name: questionRelation }]) setQuestionRelation('') } const [questionSelectMap, setQuestionSelectMap] = useState(new Map()) const addQuestionSelectMap = (key: any, value: any) => { setQuestionSelectMap((prevMap: any) => { const newMap = new Map(prevMap) newMap.set(key, value) return newMap }) } const delQuestionSelectMap = (key: any) => { setQuestionSelectMap((prevMap: any) => { const newMap = new Map(prevMap) newMap.delete(key) return newMap }) } const [showConfirmDelete, setShowConfirmDelete] = useState(false) const [row, setRow] = useState({}) const handleDelQuestion = async () => { try { await delCorpusQuestion({ url: `/xxx/${row.id}`, body: {}, }) setShowConfirmDelete(false) // mutate() } catch (e) { } } const [showQuestionEdit, setShowQuestionEdit] = useState(false) const [editQuestion, setEditQuestion] = useState('') const [corpusRow, setCorpusRow] = useState({}) const [corpusRowConfig, setCorpusRowConfig] = useState('') const handleSave = useCallback(async () => { // try { // let res // if (transfer.mode === 'add') { // res = await addCorpus({ // url: '/xxx', // body: { name, type: 'knowledge_category' }, // }) // } // else { // res = await editCorpus({ // url: '/xxx', // body: { name }, // }) // } // const { id }: any = res // if (id) { // onCancel() // onSend() // } // } // catch (e) { } }, [name, onCancel, onSend, transfer]) const handleSaveQuestion = useCallback(async () => { // try { // let res // if (transfer.mode === 'add') { // res = await addCorpus({ // url: '/xxx', // body: { name, type: 'knowledge_category' }, // }) // } // else { // res = await editCorpus({ // url: '/xxx', // body: { name }, // }) // } // const { id }: any = res // if (id) { // onCancel() // onSend() // } // } // catch (e) { } }, [name, onCancel, onSend, transfer]) return (
{ }} className="p-[24px 32px] w-[800px] max-w-[800px]">
{transfer.mode === 'add' ? '新增' : '编辑'}意图
意图类型
{ setIntentType(i.value) setIntentName('') }} items={optionsIntentType} allowSearch={false} placeholder="请选择意图类型" />
意图名称
setQuestion(e.target.value)} onClear={() => setQuestion('')} />
{ transfer.mode === 'edit' && (
关键词
setQuestionRelation(e.target.value)} onClear={() => setQuestionRelation('')} placeholder='输入后Enter以添加' onEnter={handleAddQuestion} />
) }
{ transfer.mode === 'edit' && (
e.stopPropagation()}> questionSelectMap.has(v.id))} onCheck={() => { questionList.every((v: any) => questionSelectMap.has(v.id)) ? setQuestionSelectMap(new Map()) : questionList.forEach((v: any) => addQuestionSelectMap(v.id, v)) }} disabled={questionList.length === 0} /> 全选
setQuestionFilter(e.target.value)} onClear={() => setQuestionFilter('')} placeholder='请输入相似问题名称进行过滤' />
{ questionList.filter((v: any) => !questionFilter || v.name.includes(questionFilter)).map((item: any) => (
{ questionSelectMap.has(item.id) ? delQuestionSelectMap(item.id) : addQuestionSelectMap(item.id, item) }} disabled={questionList.length === 0} />
{item.name}
)) }
共{questionList.length}条
已选择{questionSelectMap.size}条
) }
{ showQuestionEdit && ( { }} className="p-[24px 32px] w-[400px]">
编辑关键词
setShowQuestionEdit(false)} />
关键词
setEditQuestion(e.target.value)} className='h-9' placeholder='请输入关键词' />
) } {showConfirmDelete && ( setShowConfirmDelete(false)} /> )}
) } export default DetailModal