come on mannnnnn

This commit is contained in:
YasinOnm08 2024-10-08 15:33:21 +02:00
parent 1f12a1d708
commit 9350a41197
4 changed files with 104 additions and 39 deletions

View file

@ -1,14 +1,55 @@
import { useState } from "react"
import { useEffect, useState } from "react"
const useChatHistory = () => {
type ChatMessages = {
name: string
messages: {}
timestamp: number
}
const [chathistory, setChatHistory] = useState<ChatMessages[]>()
interface Message {
role: string
content:string
}
export default useChatHistory
interface ChatMessages {
name: string
messages: Message[]
timestamp: number
}
interface GlobalChatHistory {
chats: ChatMessages[]
selectedIndex: number
}
let globalChatHistory: GlobalChatHistory = {
chats: [
{ name: "Chat 1", messages: [], timestamp: 4 }
],
selectedIndex:0
}
let listeners: ((state: GlobalChatHistory) => void)[] = []
const setGlobalState = (newState: GlobalChatHistory): void => {
globalChatHistory = newState;
listeners.forEach((listener) => listener(globalChatHistory))
}
export const useChatHistory = (): [GlobalChatHistory, (newState:GlobalChatHistory) => void, (index:number)=>void] => {
const [state, setState] = useState<GlobalChatHistory>(globalChatHistory)
useEffect(() => {
console.log("help", globalChatHistory);
const listener = (newState: GlobalChatHistory) => {
setState(newState)
}
listeners.push(listener)
return () => {
listeners = listeners.filter((l) => l!== listener)
}
}, [])
const setSelectedIndex = (index: number) => {
setGlobalState({...state,selectedIndex:index})
}
return [state, setGlobalState, setSelectedIndex]
}