omg finally working chat with chatHistory

This commit is contained in:
YasinOnm08 2024-10-09 13:32:41 +02:00
parent 5cdea21cea
commit b365e6968b
2 changed files with 29 additions and 14 deletions

View file

@ -15,7 +15,7 @@ const InputOutputBackend: React.FC = () => {
}
// Define state variables for user preferences and messages
const [chatHistory, setChatHistory, setSelectedIndex] = useChatHistory()
const [chatHistory, setChatHistory, setSelectedIndex, updateMessage] = useChatHistory()
const [preferredCurrency, setPreferredCurrency] = useState<string>("USD");
const [preferredLanguage, setPreferredLanguage] = useState<string>("english");
const [timeFormat, setTimeFormat] = useState<string>("24-hour");
@ -186,17 +186,19 @@ const InputOutputBackend: React.FC = () => {
if (newContent == "") {
newContent = "Generating answer..."
}
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 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
// });
};
const addMessage = (role: string, content: string) => {