From 61b67e08431046219208d1542552c68eb7d17b7e Mon Sep 17 00:00:00 2001 From: Thomas Hackner Date: Tue, 7 Jul 2026 14:42:29 +0000 Subject: [PATCH] =?UTF-8?q?KI-Chat:=20Verlauf=20wird=20beim=20Laden=20ange?= =?UTF-8?q?zeigt=20+=20einzelne=20Chats=20l=C3=B6schbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - initialMessages-JSON wurde mit <%= %> HTML-escaped, wodurch JSON.parse fehlschlug und die Nachrichtenfläche beim Öffnen eines Verlaufs leer blieb. Jetzt <%- %> mit < -> < escapet, JSON bleibt parsebar. - Pro Verlauf ein Trash-Button in der Sidebar (DELETE /chat/api/threads/:id). Löscht den Verlauf, entfernt die Zeile, setzt bei aktivem Verlauf auf neuen Chat zurück. Auch für client-seitig neu angelegte Threads. Co-Authored-By: Claude --- public/js/chat.js | 50 +++++++++++++++++++++++++++++++++++++++++++---- views/chat.ejs | 25 ++++++++++++++++-------- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/public/js/chat.js b/public/js/chat.js index 1a8acce..f36dfc7 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -137,10 +137,21 @@ const empty = threadList.querySelector('li.text-center'); if (empty) empty.remove(); const li = document.createElement('li'); - li.innerHTML = '' + - '
' + escapeHtml(titel || '(neuer Chat)') + '
' + - '
gerade
'; + li.setAttribute('data-thread-li', id); + li.innerHTML = + '
' + + '' + + '
' + escapeHtml(titel || '(neuer Chat)') + '
' + + '
gerade
' + + '
' + + '' + + '
'; threadList.insertBefore(li, threadList.firstChild); } @@ -284,6 +295,37 @@ newThreadBtn.addEventListener('click', resetToNewChat); } + // Delete a thread from the sidebar. Event delegation: works for server-rendered + // rows and ones added client-side (prependThread). + if (threadList) { + threadList.addEventListener('click', async (e) => { + const btn = e.target.closest('[data-delete-thread]'); + if (!btn) return; + e.preventDefault(); + e.stopPropagation(); + const id = Number(btn.getAttribute('data-delete-thread')); + if (!id || !confirm('Diesen Chat-Verlauf wirklich löschen?')) return; + try { + const res = await fetch('/chat/api/threads/' + id, { method: 'DELETE' }); + if (!res.ok) throw new Error('Löschen fehlgeschlagen (' + res.status + ')'); + } catch (err) { + alert(err.message); + return; + } + const li = threadList.querySelector('li[data-thread-li="' + id + '"]'); + if (li) li.remove(); + // If the deleted thread was active, start a fresh chat. + if (activeId === id) resetToNewChat(); + // If no threads remain, show the empty placeholder. + if (!threadList.querySelector('li[data-thread-li]')) { + const empty = document.createElement('li'); + empty.className = 'px-3 py-4 text-sm text-gray-400 text-center'; + empty.textContent = 'Noch keine Verläufe.'; + threadList.appendChild(empty); + } + }); + } + // Render any pre-loaded messages (existing thread) with the same renderer. function renderInitial() { const blob = document.getElementById('initialMessages'); diff --git a/views/chat.ejs b/views/chat.ejs index 8345d2d..10d792b 100644 --- a/views/chat.ejs +++ b/views/chat.ejs @@ -43,13 +43,22 @@
Verläufe