checkpoint chat
This commit is contained in:
parent
1102404c9d
commit
a17f7b2a1b
@ -1,4 +1,5 @@
|
||||
let chatCount = 0; // Zähler für Chat-Sessions
|
||||
let currentChatId = null; // Aktuell angezeigte Chat-ID
|
||||
|
||||
document.getElementById('newChatButton').addEventListener('click', () => {
|
||||
createNewChat();
|
||||
@ -9,18 +10,18 @@ document.getElementById('sendButton').addEventListener('click', () => {
|
||||
const chatInput = document.getElementById('chatInput');
|
||||
const messageText = chatInput.value.trim();
|
||||
|
||||
if (messageText !== '') {
|
||||
if (messageText !== '' && currentChatId !== null) { // Stelle sicher, dass eine Chat-ID vorhanden ist
|
||||
const messageElement = document.createElement('div');
|
||||
messageElement.classList.add('chat-message', 'user');
|
||||
messageElement.innerText = messageText;
|
||||
chatBox.prepend(messageElement);
|
||||
|
||||
// Nachricht speichern
|
||||
saveMessage(chatCount, messageText);
|
||||
saveMessage(currentChatId, messageText); // Verwende die aktuelle Chat-ID
|
||||
|
||||
// Sidebar-Button für den Chat-Verlauf erstellen, falls noch nicht vorhanden
|
||||
if (document.getElementById(`chatSession-${chatCount}`) === null) {
|
||||
createChatSessionButton(chatCount);
|
||||
if (document.getElementById(`chatSession-${currentChatId}`) === null) {
|
||||
createChatSessionButton(currentChatId);
|
||||
}
|
||||
|
||||
chatInput.value = ''; // Input-Feld leeren
|
||||
@ -33,12 +34,26 @@ function createNewChat() {
|
||||
localStorage.setItem(`chatHistory-${newChatId}`, JSON.stringify([])); // Leeren Chat speichern
|
||||
createChatSessionButton(newChatId); // Button für den neuen Chat erstellen
|
||||
loadChatHistory(newChatId); // Lade den neuen Chat
|
||||
currentChatId = newChatId; // Setze die aktuelle Chat-ID
|
||||
}
|
||||
|
||||
function saveMessage(chatId, message) {
|
||||
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
||||
chatHistory.push({ user: "user", text: message });
|
||||
localStorage.setItem(`chatHistory-${chatId}`, JSON.stringify(chatHistory));
|
||||
|
||||
// Aktualisiere den Button-Text in der Sidebar
|
||||
updateChatSessionButton(chatId, message);
|
||||
}
|
||||
|
||||
function updateChatSessionButton(chatId, message) {
|
||||
const button = document.getElementById(`chatSession-${chatId}`);
|
||||
if (button) {
|
||||
const chatHistory = JSON.parse(localStorage.getItem(`chatHistory-${chatId}`)) || [];
|
||||
// Hole die erste Nachricht oder lasse es leer, wenn keine vorhanden ist
|
||||
const chatName = chatHistory.length > 0 ? chatHistory[0].text.slice(0, 10) : ''; // Nimm die ersten 10 Zeichen der ersten Nachricht
|
||||
button.innerText = chatName + "..." || `Chat ${chatId + 1}`; // Falls leer, setze auf "Chat x"
|
||||
}
|
||||
}
|
||||
|
||||
function createChatSessionButton(chatId) {
|
||||
@ -52,8 +67,11 @@ function createChatSessionButton(chatId) {
|
||||
const button = document.createElement('button');
|
||||
button.classList.add('chat-session-button');
|
||||
button.id = `chatSession-${chatId}`;
|
||||
button.innerText = `Chat ${chatId + 1}`;
|
||||
button.addEventListener('click', () => loadChatHistory(chatId));
|
||||
button.innerText = `Chat ${chatId + 1}`; // Vorläufiger Name
|
||||
button.addEventListener('click', () => {
|
||||
loadChatHistory(chatId); // Lade den Chat-Verlauf und setze die aktuelle Chat-ID
|
||||
currentChatId = chatId; // Setze die aktuelle Chat-ID auf die ausgewählte Chat-ID
|
||||
});
|
||||
|
||||
// Löschen-Button
|
||||
const deleteButton = document.createElement('button');
|
||||
@ -81,6 +99,8 @@ function loadChatHistory(chatId) {
|
||||
messageElement.innerText = entry.text;
|
||||
chatBox.prepend(messageElement);
|
||||
});
|
||||
|
||||
currentChatId = chatId; // Setze die aktuelle Chat-ID
|
||||
}
|
||||
|
||||
function deleteChat(chatId, buttonContainer) {
|
||||
|
Loading…
Reference in New Issue
Block a user