start of chatHistory

This commit is contained in:
YasinOnm08 2024-10-08 12:56:28 +02:00
parent dc44bd22d7
commit dbf30fa8a7
3 changed files with 38 additions and 27 deletions

View file

@ -1,30 +1,28 @@
type Message = {
role: string;
content:string
}
// type Message = {
// role: string;
// content:string
// }
type Chat = {
name: string;
messages: Message[];
timestamp: number;
};
// type Chat = {
// name: string;
// messages: Message[];
// timestamp: number;
// };
export let chatHistory: Chat[] = [];
// export function addMessageToHistory(index: number, chat: Chat): void {
// if (index >= 0 && index < chatHistory.length) {
// chatHistory[index] = chat;
// chatHistory.sort((a, b) => b.timestamp - a.timestamp)
// }
// }
export function addMessageToHistory(index: number, chat: Chat): void {
if (index >= 0 && index < chatHistory.length) {
chatHistory[index] = chat;
chatHistory.sort((a, b) => b.timestamp - a.timestamp)
}
}
export function removeMessageFromHistory(timestamp: number): void {
const index = chatHistory.findIndex((msg) => msg.timestamp === timestamp);
if (index > -1) {
chatHistory.splice(index, 1);
console.log(`Removed message with timestamp: ${timestamp}`);
} else {
console.log(`Message not found with timestamp: ${timestamp}`);
}
}
// export function removeMessageFromHistory(timestamp: number): void {
// const index = chatHistory.findIndex((msg) => msg.timestamp === timestamp);
// if (index > -1) {
// chatHistory.splice(index, 1);
// console.log(`Removed message with timestamp: ${timestamp}`);
// } else {
// console.log(`Message not found with timestamp: ${timestamp}`);
// }
// }

View file

@ -5,7 +5,6 @@ import InputFrontend from "../components/InputFrontend";
import { sendToVoiceRecognition } from "./voice_backend"
import axios from "axios";
import { changeHistory, checkCredentials, getHistory } from './database';
import { addMessageToHistory, removeMessageFromHistory } from "./ChatHistory";
interface InputOutputHandlerProps {
selectedIndex: number;

View file

@ -0,0 +1,14 @@
import { useState } from "react"
const useChatHistory = () => {
type ChatMessages = {
name: string
messages: {}
timestamp: number
}
const [chathistory, setChatHistory] = useState<ChatMessages[]>()
}
export default useChatHistory