interstellar_ai/app/hooks/useChatHistory.tsx

99 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-10-11 10:06:32 +02:00
import { useEffect, useState } from "react";
2024-10-08 12:56:28 +02:00
2024-10-11 10:06:32 +02:00
// Define the structure of a Message
2024-10-08 15:33:21 +02:00
interface Message {
2024-10-11 10:06:32 +02:00
role: string; // The role of the message sender (e.g., system, user, assistant)
content: string; // The content of the message
2024-10-08 15:33:21 +02:00
}
2024-10-11 10:06:32 +02:00
// Define the structure for each chat session
2024-10-08 15:33:21 +02:00
interface ChatMessages {
2024-10-11 10:06:32 +02:00
name: string; // The name or title of the chat
messages: Message[]; // Array of messages in the chat
timestamp: number; // Timestamp for the chat session
2024-10-08 15:33:21 +02:00
}
2024-10-08 12:56:28 +02:00
2024-10-11 10:06:32 +02:00
// Define the structure for the global chat history
2024-10-08 15:33:21 +02:00
interface GlobalChatHistory {
2024-10-11 10:06:32 +02:00
chats: ChatMessages[]; // Array of chat sessions
selectedIndex: number; // Index of the currently selected chat
2024-10-08 15:33:21 +02:00
}
2024-10-11 10:06:32 +02:00
// Initial global chat history state
2024-10-08 15:33:21 +02:00
let globalChatHistory: GlobalChatHistory = {
chats: [
2024-10-11 10:06:32 +02:00
{
name: "Welcome!",
messages: [
{ role: "system", content: "you are a helpful assistant" },
{ role: "assistant", content: "Hello! How can I help you?" }
],
timestamp: 4
},
2024-10-08 15:33:21 +02:00
],
2024-10-11 10:06:32 +02:00
selectedIndex: 0
};
// Listeners for state changes
let listeners: ((state: GlobalChatHistory) => void)[] = [];
2024-10-08 12:56:28 +02:00
2024-10-11 10:06:32 +02:00
// Function to set a new global state and notify listeners
2024-10-08 15:33:21 +02:00
const setGlobalState = (newState: GlobalChatHistory): void => {
2024-10-11 10:06:32 +02:00
globalChatHistory = newState; // Update the global state
listeners.forEach((listener) => listener(globalChatHistory)); // Notify all listeners
2024-10-08 12:56:28 +02:00
}
2024-10-11 10:06:32 +02:00
// 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
2024-10-08 15:33:21 +02:00
useEffect(() => {
2024-10-11 10:06:32 +02:00
// Log the initial state for debugging
console.log("Current global chat history:", globalChatHistory);
2024-10-08 15:33:21 +02:00
2024-10-11 10:06:32 +02:00
// Listener to update local state when global state changes
2024-10-08 15:33:21 +02:00
const listener = (newState: GlobalChatHistory) => {
2024-10-11 10:06:32 +02:00
setState(newState);
};
2024-10-08 15:33:21 +02:00
2024-10-11 10:06:32 +02:00
listeners.push(listener); // Add the listener to the array
2024-10-08 15:33:21 +02:00
2024-10-11 10:06:32 +02:00
// Cleanup function to remove the listener when the component unmounts
2024-10-08 15:33:21 +02:00
return () => {
2024-10-11 10:06:32 +02:00
listeners = listeners.filter((l) => l !== listener);
};
}, []);
2024-10-08 15:33:21 +02:00
2024-10-11 10:06:32 +02:00
// Function to set the selected chat index
2024-10-08 15:33:21 +02:00
const setSelectedIndex = (index: number) => {
2024-10-11 10:06:32 +02:00
setGlobalState({ ...state, selectedIndex: index }); // Update the global state
2024-10-08 15:33:21 +02:00
}
2024-10-11 10:06:32 +02:00
// Function to update a specific message in the current chat
const updateMessage = (messageIndex: number, newContent: string) => {
2024-10-11 10:06:32 +02:00
const updatedChats = [...state.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) {
2024-10-11 10:06:32 +02:00
const updatedMessages = [...updatedChats[chatIndex].messages]; // Copy messages of the selected chat
// Check if the message index is valid
if (messageIndex >= 0 && messageIndex < updatedMessages.length) {
2024-10-11 10:06:32 +02:00
// 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
}
}
}
2024-10-11 10:06:32 +02:00
// Return the current state and action functions
return [state, setSelectedIndex, setGlobalState, updateMessage];
}