From cf682093dbe3cf8f573874002982b08d03398247 Mon Sep 17 00:00:00 2001 From: dd3xp Date: Tue, 26 May 2026 02:22:16 +0800 Subject: [PATCH] fix(desktop): after deleting active session, select the topmost one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closeSession picked state.sessions.keys().next().value — the Map's first insertion-order entry, unrelated to the visible list, so it felt like jumping to the "bottom" session. Now it picks the first of the displayed order (sortedSessions()[0]): pinned first, then most-recently-active = the topmost. Extracted sortedSessions() shared by renderSessionList and closeSession. --- frontends/desktop/static/app.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/frontends/desktop/static/app.js b/frontends/desktop/static/app.js index 952b7791..6fb80c90 100644 --- a/frontends/desktop/static/app.js +++ b/frontends/desktop/static/app.js @@ -1285,15 +1285,18 @@ function setBusy(sess, busy) { /* ═══════════════ 会话 ═══════════════ */ function isUntitled(x) { return !x || /^(new chat|新对话|新会话)$/i.test(String(x).trim()); } +function sortedSessions() { + // display order: pinned first, then most-recently-active. [0] is the topmost. + return [...state.sessions.values()].sort((a, b) => { + if (a.pinned && !b.pinned) return -1; + if (!a.pinned && b.pinned) return 1; + return (b.lastActiveTs || 0) - (a.lastActiveTs || 0); + }); +} function renderSessionList() { convListEl.innerHTML = ''; const query = (searchInput ? searchInput.value : '').trim().toLowerCase(); - const all = [...state.sessions.values()] - .sort((a, b) => { - if (a.pinned && !b.pinned) return -1; - if (!a.pinned && b.pinned) return 1; - return (b.lastActiveTs || 0) - (a.lastActiveTs || 0); - }); + const all = sortedSessions(); const filtered = query ? all.filter(s => { const title = (s.title || '').toLowerCase(); @@ -1364,7 +1367,7 @@ async function closeSession(id) { } state.sessions.delete(id); state.runtime.delete(id); if (state.activeId === id) { - const next = state.sessions.keys().next().value || null; + const next = (sortedSessions()[0] || {}).id || null; // 切到列表最靠上的会话 if (next) setActiveSession(next); else { state.activeId = null; if (msgsEl) msgsEl.innerHTML = ''; refreshEmptyState(null); refreshStatusLabel(); } }