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