omg finally working chat with chatHistory
This commit is contained in:
parent
5cdea21cea
commit
b365e6968b
2 changed files with 29 additions and 14 deletions
|
@ -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]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue