feat: auto-add new session to active user view on creation

When a session is created via the + button while a user view is active,
auto-add the new session's key to that view's sessions list.

- In createNewSession(), after the successful POST call, check _activeView
- Skip auto-add for reserved views ('all', 'hidden')
- Build sessionKey correctly using device_id or remoteId
- PATCH /api/settings with updated views, catch errors gracefully
- Update local _serverSettings.views cache immediately

Test: test_create_new_session_references_active_view
This commit is contained in:
Brian Krabach
2026-04-15 17:17:11 -07:00
parent c3eb714977
commit ecca48a1ad
2 changed files with 43 additions and 0 deletions
+24
View File
@@ -2133,6 +2133,30 @@ async function createNewSession(name, remoteId) {
const res = await api('POST', endpoint, { name });
const data = await res.json();
const sessionName = data.name || name;
// Auto-add to active user view (not 'all' or 'hidden')
if (_activeView !== 'all' && _activeView !== 'hidden') {
var views = (_serverSettings && _serverSettings.views) || [];
var viewIdx = -1;
for (var vi = 0; vi < views.length; vi++) {
if (views[vi].name === _activeView) { viewIdx = vi; break; }
}
if (viewIdx >= 0) {
var newSessionKey = remoteId ? (remoteId + ':' + sessionName) : sessionName;
if (!remoteId && _serverSettings && _serverSettings.device_id) {
newSessionKey = _serverSettings.device_id + ':' + sessionName;
}
var updatedViews = JSON.parse(JSON.stringify(views));
if (!updatedViews[viewIdx].sessions.includes(newSessionKey)) {
updatedViews[viewIdx].sessions.push(newSessionKey);
api('PATCH', '/api/settings', { views: updatedViews }).catch(function(err) {
console.warn('[createNewSession] auto-add to view failed:', err);
});
if (_serverSettings) _serverSettings.views = updatedViews;
}
}
}
showToast('Creating session \'' + sessionName + '\'…');
// Inject a loading placeholder tile so the user sees feedback immediately
+19
View File
@@ -3093,3 +3093,22 @@ def test_set_active_view_helper_exported() -> None:
assert "_setActiveView" in exports, (
"module.exports must export _setActiveView test helper"
)
# ---------------------------------------------------------------------------
# Auto-add session to active user view on creation (task-3-auto-add-to-view)
# ---------------------------------------------------------------------------
def test_create_new_session_references_active_view() -> None:
"""createNewSession must reference _activeView to auto-add the new session to the active user view."""
match = re.search(
r"async function createNewSession\s*\([\w,\s]+\)\s*\{(.*?)(?=\nasync function |\nfunction |\n// )",
_JS,
re.DOTALL,
)
assert match, "createNewSession function not found"
body = match.group(1)
assert "_activeView" in body, (
"createNewSession must reference _activeView to auto-add new session to the active user view"
)