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

@ -31,7 +31,7 @@ const setGlobalState = (newState: GlobalChatHistory): void => {
listeners.forEach((listener) => listener(globalChatHistory))
}
export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistory) => void, (index:number)=>void] => {
export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistory) => void, (index:number)=>void, (messageIndex: number, newContent:string)=> void] => {
const [state, setState] = useState<GlobalChatHistory>(globalChatHistory)
useEffect(() => {
@ -52,5 +52,18 @@ export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistor
setGlobalState({...state,selectedIndex:index})
}
return [state, setGlobalState, setSelectedIndex]
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]
}