From 1fe45ef5e0cbc1a013113912e764eb3758ff6c16 Mon Sep 17 00:00:00 2001 From: Rankin Zheng Date: Fri, 18 Aug 2023 11:48:19 +0800 Subject: [PATCH] Refactor InputMessage component and ChatStore - Simplified the handleSendClick function in InputMessage/index.tsx to directly use input.value. - Made 'contexts' an optional field in the Message model in ChatStore.ts. - Updated the newMessage and updateLastMessage actions in ChatStore.ts to accept an IMessage object. --- src/views/components/InputMessage/index.tsx | 9 ++------- src/views/stores/ChatStore.ts | 14 +++++++------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/views/components/InputMessage/index.tsx b/src/views/components/InputMessage/index.tsx index 0df3523..c7bbe1b 100644 --- a/src/views/components/InputMessage/index.tsx +++ b/src/views/components/InputMessage/index.tsx @@ -37,15 +37,10 @@ const InputMessage = observer((props: any) => { }; const handleSendClick = (event: React.MouseEvent) => { - if (input) { - // Process and send the message to the extension - const contextInfo = input.contexts.map((item: any, index: number) => { - const { file, context } = item; - return { file, context }; - }); + if (input.value) { const text = input.value; // Add the user's message to the chat UI - chat.newMessage({ type: 'user', message: input, contexts: contexts ? [...contexts].map((item) => ({ ...item })) : undefined }); + chat.newMessage({ type: 'user', message: input.value, contexts: contexts ? [...contexts].map((item) => ({ ...item })) : undefined }); // start generating chat.startGenerating(text); // Clear the input field diff --git a/src/views/stores/ChatStore.ts b/src/views/stores/ChatStore.ts index 8f4f8d1..d4d5c64 100644 --- a/src/views/stores/ChatStore.ts +++ b/src/views/stores/ChatStore.ts @@ -60,7 +60,7 @@ export const Message = types.model({ hash: types.maybe(types.string), type: types.enumeration(['user', 'bot', 'system']), message: types.string, - contexts: types.array(ChatContext), + contexts: types.maybe(types.array(ChatContext)), }); export const ChatStore = types.model('Chat', { @@ -135,13 +135,13 @@ export const ChatStore = types.model('Chat', { self.responsed = true; self.currentMessage = action.payload; }, - newMessage: (action) => { - self.messages.push(action.payload); - self.lastMessage = action.payload; + newMessage: (message: IMessage) => { + self.messages.push(message); + self.lastMessage = message; }, - updateLastMessage: (action) => { - self.messages[self.messages.length - 1] = action.payload; - self.lastMessage = action.payload; + updateLastMessage: (message: IMessage) => { + self.messages[self.messages.length - 1] = message; + self.lastMessage = message; }, shiftMessage: () => { self.messages.splice(0, 1);