delete logs and fix hydration error

This commit is contained in:
YasinOnm08 2024-10-11 10:46:05 +02:00
parent 4ed2756b9a
commit 891dabcabf
10 changed files with 37 additions and 70 deletions

View file

@ -46,16 +46,11 @@ const setGlobalState = (newState: GlobalChatHistory): void => {
// Custom hook to manage chat history
export const useChatHistory = (): [
GlobalChatHistory, // Current state
(index: number) => void, // Function to set selected index
(newState: GlobalChatHistory) => void, // Function to set global state
(messageIndex: number, newContent: string) => void // Function to update a message
] => {
const [state, setState] = useState<GlobalChatHistory>(globalChatHistory); // Local state initialized with global state
useEffect(() => {
// Log the initial state for debugging
console.log("Current global chat history:", globalChatHistory);
// Listener to update local state when global state changes
const listener = (newState: GlobalChatHistory) => {
setState(newState);
@ -69,30 +64,31 @@ export const useChatHistory = (): [
};
}, []);
// Function to set the selected chat index
const setSelectedIndex = (index: number) => {
setGlobalState({ ...state, selectedIndex: index }); // Update the global state
}
// Return the current state and action functions
return [state, setGlobalState];
}
// Function to set the selected chat index
export const setSelectedIndex = (index: number) => {
setGlobalState({ ...globalChatHistory, selectedIndex: index }); // Update the global state
}
// Function to update a specific message in the current chat
const updateMessage = (messageIndex: number, newContent: string) => {
const updatedChats = [...state.chats]; // Make a copy of the current chats
const chatIndex = globalChatHistory.selectedIndex; // Get the currently selected chat index
// Function to update a specific message in the current chat
export const updateMessage = (messageIndex: number, newContent: string) => {
const updatedChats = [...globalChatHistory.chats]; // Make a copy of the current chats
const chatIndex = globalChatHistory.selectedIndex; // Get the currently selected chat index
// Check if the selected chat index is valid
if (chatIndex >= 0 && chatIndex < updatedChats.length) {
const updatedMessages = [...updatedChats[chatIndex].messages]; // Copy messages of the selected chat
// Check if the selected chat index is valid
if (chatIndex >= 0 && chatIndex < updatedChats.length) {
const updatedMessages = [...updatedChats[chatIndex].messages]; // Copy messages of the selected chat
// Check if the message index is valid
if (messageIndex >= 0 && messageIndex < updatedMessages.length) {
// Update the content of the specified message
updatedMessages[messageIndex] = { ...updatedMessages[messageIndex], content: newContent };
updatedChats[chatIndex] = { ...updatedChats[chatIndex], messages: updatedMessages }; // Update the chat with new messages
setGlobalState({ ...state, chats: updatedChats }); // Set the updated global state
}
// Check if the message index is valid
if (messageIndex >= 0 && messageIndex < updatedMessages.length) {
// Update the content of the specified message
updatedMessages[messageIndex] = { ...updatedMessages[messageIndex], content: newContent };
updatedChats[chatIndex] = { ...updatedChats[chatIndex], messages: updatedMessages }; // Update the chat with new messages
setGlobalState({ ...globalChatHistory, chats: updatedChats }); // Set the updated global state
}
}
// Return the current state and action functions
return [state, setSelectedIndex, setGlobalState, updateMessage];
}