From fb21fb0f622aff728c666da1ec082e8a770663e1 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sat, 4 Mar 2023 09:43:51 +0800 Subject: [PATCH] prevent submission while generating --- .../Chat/ChatContent/ChatContent.tsx | 30 +++++++++++++------ src/store/chat-slice.ts | 9 ++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/components/Chat/ChatContent/ChatContent.tsx b/src/components/Chat/ChatContent/ChatContent.tsx index ca27620..a72b5c3 100644 --- a/src/components/Chat/ChatContent/ChatContent.tsx +++ b/src/components/Chat/ChatContent/ChatContent.tsx @@ -15,23 +15,34 @@ import RefreshIcon from '@icon/RefreshIcon'; import { MessageInterface } from '@type/chat'; const ChatContent = () => { - const [messages, inputRole, apiFree, apiKey, setMessages] = useStore( - (state) => [ - state.messages, - state.inputRole, - state.apiFree, - state.apiKey, - state.setMessages, - ] - ); + const [ + messages, + inputRole, + apiFree, + apiKey, + setMessages, + setGenerating, + generating, + ] = useStore((state) => [ + state.messages, + state.inputRole, + state.apiFree, + state.apiKey, + state.setMessages, + state.setGenerating, + state.generating, + ]); const [error, setError] = useState(''); const handleSubmit = async () => { + if (generating) return; + const updatedMessages: MessageInterface[] = JSON.parse( JSON.stringify(messages) ); updatedMessages.push({ role: 'assistant', content: '' }); setMessages(updatedMessages); + setGenerating(true); let stream; try { @@ -77,6 +88,7 @@ const ChatContent = () => { setError(''), 10000; }); } + setGenerating(false); }; return ( diff --git a/src/store/chat-slice.ts b/src/store/chat-slice.ts index 3610a4b..be21a6b 100644 --- a/src/store/chat-slice.ts +++ b/src/store/chat-slice.ts @@ -5,14 +5,17 @@ export interface ChatSlice { messages: MessageInterface[]; chats?: ChatInterface[]; currentChatIndex: number; + generating: boolean; setMessages: (messages: MessageInterface[]) => void; setChats: (chats: ChatInterface[]) => void; setCurrentChatIndex: (currentChatIndex: number) => void; + setGenerating: (generating: boolean) => void; } export const createChatSlice: StoreSlice = (set, get) => ({ messages: [], currentChatIndex: -1, + generating: false, setMessages: (messages: MessageInterface[]) => { set((prev: ChatSlice) => ({ ...prev, @@ -31,4 +34,10 @@ export const createChatSlice: StoreSlice = (set, get) => ({ currentChatIndex: currentChatIndex, })); }, + setGenerating: (generating: boolean) => { + set((prev: ChatSlice) => ({ + ...prev, + generating: generating, + })); + }, });