Refine chat management: auto-titles, per-chat rename/delete, custom modal

- Auto-title a session from its first user message (chat_title) so chats are distinguishable in the sidebar instead of all 'Nuova chat'.

- Per-chat rename (✏️) and delete (🗑️) buttons with aria-labels; stopPropagation so they don't switch the conversation. deleteSessionById selects a fallback or creates a fresh session.

- Replace native prompt()/confirm() with a styled custom modal (showDialog): mobile-friendly and inspectable by automation (native dialogs hung the browser subagent).

Verified via direct HTTP (auto-title/rename/delete) + 108 tests; browser subagent avoided after repeated hangs.
This commit is contained in:
adm committed 2026-11-25 02:41:51 +01:00
1 parent 8979201139
commit 0123f41d99
5 files changed
+198 -10

No files matched your search

+83 -6
View File
@@ -117,7 +117,20 @@ function renderSessions() {
const list = $("#session-list");
list.textContent = "";
for (const s of state.sessions) {
const li = el("li", s.id === state.current ? "active" : null, s.title);
const li = el("li", s.id === state.current ? "active" : null);
li.appendChild(el("span", "session-title", s.title));
const actions = el("span", "session-actions");
const ren = el("button", "sess-btn", "\u{270F}\u{FE0F}");
ren.title = "Rinomina";
ren.setAttribute("aria-label", "Rinomina chat");
ren.addEventListener("click", (e) => { e.stopPropagation(); renameSessionById(s.id, s.title); });
const del = el("button", "sess-btn danger", "\u{1F5D1}\u{FE0F}");
del.title = "Elimina";
del.setAttribute("aria-label", "Elimina chat");
del.addEventListener("click", (e) => { e.stopPropagation(); deleteSessionById(s.id); });
actions.appendChild(ren);
actions.appendChild(del);
li.appendChild(actions);
li.addEventListener("click", () => { selectSession(s.id); closeDrawer(); });
list.appendChild(li);
}
@@ -187,11 +200,40 @@ async function newSession() {
}
async function deleteSession() {
if (!state.current) return;
if (!confirm("Eliminare questa conversazione?")) return;
await sendJSON(`/api/sessions/${state.current}`, "DELETE");
state.current = null;
await loadSessions();
if (state.current) await deleteSessionById(state.current);
}
async function renameSessionById(id, currentTitle) {
const input = await showDialog({ title: "Rinomina chat", input: currentTitle, okText: "Salva" });
if (input === null) return;
const title = input.trim();
if (!title) return;
await sendJSON(`/api/sessions/${id}`, "PATCH", { title });
state.sessions = await getJSON("/api/sessions");
renderSessions();
if (id === state.current) $("#session-title").textContent = title;
}
async function deleteSessionById(id) {
const ok = await showDialog({
title: "Elimina chat",
message: "Eliminare questa chat? L'operazione non è reversibile.",
okText: "Elimina",
});
if (!ok) return;
await sendJSON(`/api/sessions/${id}`, "DELETE");
const wasCurrent = id === state.current;
state.sessions = await getJSON("/api/sessions");
if (wasCurrent) {
state.current = null;
if (!state.sessions.length) {
const s = await sendJSON("/api/sessions", "POST", {});
state.sessions = [s];
}
await selectSession(state.sessions[0].id);
} else {
renderSessions();
}
}
async function sendMessage() {
@@ -321,6 +363,41 @@ async function showStats() {
$("#stats-modal").hidden = false;
}
// A custom modal dialog (replaces native prompt/confirm: styled, mobile-friendly,
// and inspectable by automation). Resolves with the input string (or null on
// cancel) when `input` is given, otherwise true/false.
function showDialog({ title, message = "", input = null, okText = "OK" }) {
return new Promise((resolve) => {
$("#dialog-title").textContent = title;
const msg = $("#dialog-msg");
msg.textContent = message;
msg.hidden = !message;
const inp = $("#dialog-input");
inp.hidden = input === null;
if (input !== null) inp.value = input;
$("#dialog-ok").textContent = okText;
const dlg = $("#dialog");
dlg.hidden = false;
if (input !== null) { inp.focus(); inp.select(); }
const finish = (val) => {
dlg.hidden = true;
$("#dialog-ok").removeEventListener("click", onOk);
$("#dialog-cancel").removeEventListener("click", onCancel);
inp.removeEventListener("keydown", onKey);
resolve(val);
};
const onOk = () => finish(input !== null ? inp.value : true);
const onCancel = () => finish(input !== null ? null : false);
const onKey = (e) => {
if (e.key === "Enter") { e.preventDefault(); onOk(); }
else if (e.key === "Escape") { e.preventDefault(); onCancel(); }
};
$("#dialog-ok").addEventListener("click", onOk);
$("#dialog-cancel").addEventListener("click", onCancel);
inp.addEventListener("keydown", onKey);
});
}
// ---- Drawer / input UX ----------------------------------------------------
function openDrawer() { $("#drawer").classList.add("open"); $("#scrim").hidden = false; }
function closeDrawer() { $("#drawer").classList.remove("open"); $("#scrim").hidden = true; }