prevent submission while generating

This commit is contained in:
Jing Hua 2023-03-04 09:43:51 +08:00
parent 063316407d
commit fb21fb0f62
2 changed files with 30 additions and 9 deletions

View file

@ -15,23 +15,34 @@ import RefreshIcon from '@icon/RefreshIcon';
import { MessageInterface } from '@type/chat'; import { MessageInterface } from '@type/chat';
const ChatContent = () => { const ChatContent = () => {
const [messages, inputRole, apiFree, apiKey, setMessages] = useStore( const [
(state) => [ messages,
inputRole,
apiFree,
apiKey,
setMessages,
setGenerating,
generating,
] = useStore((state) => [
state.messages, state.messages,
state.inputRole, state.inputRole,
state.apiFree, state.apiFree,
state.apiKey, state.apiKey,
state.setMessages, state.setMessages,
] state.setGenerating,
); state.generating,
]);
const [error, setError] = useState<string>(''); const [error, setError] = useState<string>('');
const handleSubmit = async () => { const handleSubmit = async () => {
if (generating) return;
const updatedMessages: MessageInterface[] = JSON.parse( const updatedMessages: MessageInterface[] = JSON.parse(
JSON.stringify(messages) JSON.stringify(messages)
); );
updatedMessages.push({ role: 'assistant', content: '' }); updatedMessages.push({ role: 'assistant', content: '' });
setMessages(updatedMessages); setMessages(updatedMessages);
setGenerating(true);
let stream; let stream;
try { try {
@ -77,6 +88,7 @@ const ChatContent = () => {
setError(''), 10000; setError(''), 10000;
}); });
} }
setGenerating(false);
}; };
return ( return (

View file

@ -5,14 +5,17 @@ export interface ChatSlice {
messages: MessageInterface[]; messages: MessageInterface[];
chats?: ChatInterface[]; chats?: ChatInterface[];
currentChatIndex: number; currentChatIndex: number;
generating: boolean;
setMessages: (messages: MessageInterface[]) => void; setMessages: (messages: MessageInterface[]) => void;
setChats: (chats: ChatInterface[]) => void; setChats: (chats: ChatInterface[]) => void;
setCurrentChatIndex: (currentChatIndex: number) => void; setCurrentChatIndex: (currentChatIndex: number) => void;
setGenerating: (generating: boolean) => void;
} }
export const createChatSlice: StoreSlice<ChatSlice> = (set, get) => ({ export const createChatSlice: StoreSlice<ChatSlice> = (set, get) => ({
messages: [], messages: [],
currentChatIndex: -1, currentChatIndex: -1,
generating: false,
setMessages: (messages: MessageInterface[]) => { setMessages: (messages: MessageInterface[]) => {
set((prev: ChatSlice) => ({ set((prev: ChatSlice) => ({
...prev, ...prev,
@ -31,4 +34,10 @@ export const createChatSlice: StoreSlice<ChatSlice> = (set, get) => ({
currentChatIndex: currentChatIndex, currentChatIndex: currentChatIndex,
})); }));
}, },
setGenerating: (generating: boolean) => {
set((prev: ChatSlice) => ({
...prev,
generating: generating,
}));
},
}); });