Compare commits
No commits in common. "7861353498208899d23ca82b17f0220d19ab00be" and "6eb1cba0aa952acbfec9a38ac365646ef91a54c9" have entirely different histories.
7861353498
...
6eb1cba0aa
2 changed files with 14 additions and 29 deletions
|
@ -15,7 +15,7 @@ const InputOutputBackend: React.FC = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define state variables for user preferences and messages
|
// Define state variables for user preferences and messages
|
||||||
const [chatHistory, setChatHistory, setSelectedIndex, updateMessage] = useChatHistory()
|
const [chatHistory, setChatHistory, setSelectedIndex] = useChatHistory()
|
||||||
const [preferredCurrency, setPreferredCurrency] = useState<string>("USD");
|
const [preferredCurrency, setPreferredCurrency] = useState<string>("USD");
|
||||||
const [preferredLanguage, setPreferredLanguage] = useState<string>("english");
|
const [preferredLanguage, setPreferredLanguage] = useState<string>("english");
|
||||||
const [timeFormat, setTimeFormat] = useState<string>("24-hour");
|
const [timeFormat, setTimeFormat] = useState<string>("24-hour");
|
||||||
|
@ -186,19 +186,17 @@ const InputOutputBackend: React.FC = () => {
|
||||||
if (newContent == "") {
|
if (newContent == "") {
|
||||||
newContent = "Generating answer..."
|
newContent = "Generating answer..."
|
||||||
}
|
}
|
||||||
const messageIndex = chatHistory.chats[chatHistory.selectedIndex].messages.length-1
|
setMessages((prevMessages) => {
|
||||||
updateMessage(messageIndex,newContent)
|
const updatedMessages = prevMessages.slice(); // Create a shallow copy of the current messages
|
||||||
// setMessages((prevMessages) => {
|
if (updatedMessages.length > 0) {
|
||||||
// const updatedMessages = prevMessages.slice(); // Create a shallow copy of the current messages
|
const lastMessage = updatedMessages[updatedMessages.length - 1];
|
||||||
// if (updatedMessages.length > 0) {
|
updatedMessages[updatedMessages.length - 1] = {
|
||||||
// const lastMessage = updatedMessages[updatedMessages.length - 1];
|
...lastMessage, // Keep the existing role and other properties
|
||||||
// updatedMessages[updatedMessages.length - 1] = {
|
content: newContent, // Update only the content
|
||||||
// ...lastMessage, // Keep the existing role and other properties
|
};
|
||||||
// content: newContent, // Update only the content
|
}
|
||||||
// };
|
return updatedMessages; // Return the updated array
|
||||||
// }
|
});
|
||||||
// return updatedMessages; // Return the updated array
|
|
||||||
// });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const addMessage = (role: string, content: string) => {
|
const addMessage = (role: string, content: string) => {
|
||||||
|
|
|
@ -31,7 +31,7 @@ const setGlobalState = (newState: GlobalChatHistory): void => {
|
||||||
listeners.forEach((listener) => listener(globalChatHistory))
|
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>(globalChatHistory)
|
const [state, setState] = useState<GlobalChatHistory>(globalChatHistory)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -52,18 +52,5 @@ export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistor
|
||||||
setGlobalState({...state,selectedIndex:index})
|
setGlobalState({...state,selectedIndex:index})
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateMessage = (messageIndex: number, newContent: string) => {
|
return [state, setGlobalState, setSelectedIndex]
|
||||||
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]
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue