import { ChatContext } from '@/app/chat-context'; import { apiInterceptors, newDialogue } from '@/client/api'; import { STORAGE_INIT_MESSAGE_KET } from '@/utils'; import { Button, Input } from 'antd'; import cls from 'classnames'; import { useRouter } from 'next/router'; import { useContext, useState } from 'react'; import { useTranslation } from 'react-i18next'; function ChatInput() { const { setCurrentDialogInfo } = useContext(ChatContext); const { t } = useTranslation(); const router = useRouter(); const [userInput, setUserInput] = useState(''); const [isFocus, setIsFocus] = useState(false); const [isZhInput, setIsZhInput] = useState(false); const onSubmit = async () => { const [, res] = await apiInterceptors(newDialogue({ chat_mode: 'chat_normal' })); if (res) { setCurrentDialogInfo?.({ chat_scene: res.chat_mode, app_code: res.chat_mode, }); localStorage.setItem( 'cur_dialog_info', JSON.stringify({ chat_scene: res.chat_mode, app_code: res.chat_mode, }), ); localStorage.setItem(STORAGE_INIT_MESSAGE_KET, JSON.stringify({ id: res.conv_uid, message: userInput })); router.push(`/chat/?scene=chat_normal&id=${res.conv_uid}`); } setUserInput(''); }; return (
{ if (e.key === 'Enter') { if (e.shiftKey) { return; } if (isZhInput) { return; } e.preventDefault(); if (!userInput.trim()) { return; } onSubmit(); } }} onChange={e => { setUserInput(e.target.value); }} onFocus={() => { setIsFocus(true); }} onBlur={() => setIsFocus(false)} onCompositionStart={() => setIsZhInput(true)} onCompositionEnd={() => setIsZhInput(false)} />
); } export default ChatInput;