diff --git a/app/backend/InputOutputHandler.tsx b/app/backend/InputOutputHandler.tsx index 68e552a..7986f37 100644 --- a/app/backend/InputOutputHandler.tsx +++ b/app/backend/InputOutputHandler.tsx @@ -15,7 +15,7 @@ const InputOutputBackend: React.FC = () => { } // Define state variables for user preferences and messages - const [chatHistory, setChatHistory, setSelectedIndex, updateMessage] = useChatHistory() + const [chatHistory, setChatHistory, setSelectedIndex] = useChatHistory() const [preferredCurrency, setPreferredCurrency] = useState("USD"); const [preferredLanguage, setPreferredLanguage] = useState("english"); const [timeFormat, setTimeFormat] = useState("24-hour"); @@ -186,19 +186,17 @@ const InputOutputBackend: React.FC = () => { if (newContent == "") { newContent = "Generating answer..." } - const messageIndex = chatHistory.chats[chatHistory.selectedIndex].messages.length-1 - updateMessage(messageIndex,newContent) - // setMessages((prevMessages) => { - // const updatedMessages = prevMessages.slice(); // Create a shallow copy of the current messages - // if (updatedMessages.length > 0) { - // const lastMessage = updatedMessages[updatedMessages.length - 1]; - // updatedMessages[updatedMessages.length - 1] = { - // ...lastMessage, // Keep the existing role and other properties - // content: newContent, // Update only the content - // }; - // } - // return updatedMessages; // Return the updated array - // }); + setMessages((prevMessages) => { + const updatedMessages = prevMessages.slice(); // Create a shallow copy of the current messages + if (updatedMessages.length > 0) { + const lastMessage = updatedMessages[updatedMessages.length - 1]; + updatedMessages[updatedMessages.length - 1] = { + ...lastMessage, // Keep the existing role and other properties + content: newContent, // Update only the content + }; + } + return updatedMessages; // Return the updated array + }); }; const addMessage = (role: string, content: string) => { diff --git a/app/hooks/useChatHistory.tsx b/app/hooks/useChatHistory.tsx index b153eef..e3c550c 100644 --- a/app/hooks/useChatHistory.tsx +++ b/app/hooks/useChatHistory.tsx @@ -31,7 +31,7 @@ const setGlobalState = (newState: GlobalChatHistory): void => { listeners.forEach((listener) => listener(globalChatHistory)) } -export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistory) => void, (index:number)=>void, (messageIndex: number, newContent:string)=> void] => { +export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistory) => void, (index:number)=>void] => { const [state, setState] = useState(globalChatHistory) useEffect(() => { @@ -52,18 +52,5 @@ export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistor setGlobalState({...state,selectedIndex:index}) } - const updateMessage = (messageIndex: number, newContent: string) => { - const updatedChats = [...state.chats] - const chatIndex = globalChatHistory.selectedIndex - if (chatIndex >= 0 && chatIndex < updatedChats.length) { - const updatedMessages = [...updatedChats[chatIndex].messages] - if (messageIndex >= 0 && messageIndex < updatedMessages.length) { - updatedMessages[messageIndex] = { ...updatedMessages[messageIndex], content: newContent } - updatedChats[chatIndex] = { ...updatedChats[chatIndex], messages: updatedMessages } - setGlobalState({...state, chats: updatedChats}) - } - } - } - - return [state, setGlobalState, setSelectedIndex, updateMessage] + return [state, setGlobalState, setSelectedIndex] } \ No newline at end of file