Skip to content

Commit c8c2a31

Browse files
fix(ui): persist the sidebar session sort selection
The sessions sidebar held its sort mode in component state only, so a reload silently reverted the user's Created/Last updated/People choice back to Created while every sibling sidebar preference persisted. Store the mode under openclaw:sidebar:sessions:sort-mode alongside the existing sidebar preference keys and read it back at init. Resolution of a gateway-denied People sort now lives in one helper shared by the render path and the setter, keeping the element net-neutral in lines.
1 parent 8f807ae commit c8c2a31

4 files changed

Lines changed: 85 additions & 6 deletions

File tree

‎ui/src/components/app-sidebar-session-navigation.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ import { SessionPullRequestIndicatorsController } from "./app-sidebar-session-pr
5353
import { projectSessionTree } from "./app-sidebar-session-tree.ts";
5454
import { loadStoredHiddenSessionCatalogIds } from "./app-sidebar-session-types.ts";
5555
import {
56+
loadStoredSidebarSessionSortMode,
5657
loadStoredSidebarSessionStatusFilter,
5758
loadStoredSidebarSessionsGrouping,
5859
loadStoredSidebarSessionsShowCron,
5960
loadStoredSidebarSessionsShowSystem,
61+
resolveSidebarSessionSortMode,
62+
storeSidebarSessionSortMode,
6063
type SidebarRecentSession,
6164
type SidebarSessionSortMode,
6265
type SidebarSessionStatusFilter,
@@ -69,7 +72,7 @@ import type { SidebarMenusController } from "./sidebar-menus-controller.ts";
6972

7073
/** Session-row projection, selection, sorting, and agent scope navigation. */
7174
export class AppSidebarSessionNavigationElement extends AppSidebarBase {
72-
@state() sessionSortMode: SidebarSessionSortMode = "created";
75+
@state() sessionSortMode: SidebarSessionSortMode = loadStoredSidebarSessionSortMode();
7376

7477
readonly sessionData = new SessionDataController(this);
7578
private readonly sessionPullRequestIndicators = new SessionPullRequestIndicatorsController(this, {
@@ -109,14 +112,11 @@ export class AppSidebarSessionNavigationElement extends AppSidebarBase {
109112
effectiveSessionSortMode(): SidebarSessionSortMode {
110113
// A reconnect can temporarily hide the capability. Render Created without
111114
// discarding People until an authoritative single-identity hello arrives.
112-
return this.sessionSortMode === "people" && !this.sessionPeopleSortAvailable()
113-
? "created"
114-
: this.sessionSortMode;
115+
return resolveSidebarSessionSortMode(this.sessionSortMode, this.sessionPeopleSortAvailable());
115116
}
116117

117118
setSessionSortMode(mode: SidebarSessionSortMode) {
118-
this.sessionSortMode =
119-
mode === "people" && this.sessionPeopleSortCapability() === false ? "created" : mode;
119+
this.sessionSortMode = storeSidebarSessionSortMode(mode, this.sessionPeopleSortCapability());
120120
}
121121

122122
@state() sessionCreatorFilterId: string | null = null;

‎ui/src/components/app-sidebar-session-types.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import { afterEach, beforeEach, describe, expect, it } from "vitest";
44
import {
55
loadStoredHiddenSessionCatalogIds,
6+
loadStoredSidebarSessionSortMode,
67
loadStoredSidebarSessionStatusFilter,
78
setStoredSessionCatalogHidden,
9+
storeSidebarSessionSortMode,
810
storeSidebarSessionStatusFilter,
911
} from "./app-sidebar-session-types.ts";
1012

@@ -57,6 +59,26 @@ describe("sidebar session status preference", () => {
5759
});
5860
});
5961

62+
describe("sidebar session sort preference", () => {
63+
it("defaults absent and unknown stored values to created", () => {
64+
expect(loadStoredSidebarSessionSortMode()).toBe("created");
65+
localStorage.setItem("openclaw:sidebar:sessions:sort-mode", "unexpected");
66+
expect(loadStoredSidebarSessionSortMode()).toBe("created");
67+
});
68+
69+
it("round-trips updated and people modes", () => {
70+
expect(storeSidebarSessionSortMode("updated", undefined)).toBe("updated");
71+
expect(loadStoredSidebarSessionSortMode()).toBe("updated");
72+
expect(storeSidebarSessionSortMode("people", true)).toBe("people");
73+
expect(loadStoredSidebarSessionSortMode()).toBe("people");
74+
});
75+
76+
it("stores created instead of a people sort the gateway denied", () => {
77+
expect(storeSidebarSessionSortMode("people", false)).toBe("created");
78+
expect(loadStoredSidebarSessionSortMode()).toBe("created");
79+
});
80+
});
81+
6082
describe("hidden session catalog preference", () => {
6183
it("round-trips catalog ids and reverses one hide at a time", () => {
6284
setStoredSessionCatalogHidden("codex", true);

‎ui/src/components/app-sidebar-session-types.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ const SIDEBAR_SESSION_CATALOG_GROUPING_STORAGE_KEY = "openclaw:sidebar:sessions:
213213
const SIDEBAR_SESSION_SHOW_CRON_STORAGE_KEY = "openclaw:sidebar:sessions:show-cron";
214214
const SIDEBAR_SESSION_SHOW_SYSTEM_STORAGE_KEY = "openclaw:sidebar:sessions:show-system";
215215
const SIDEBAR_SESSION_STATUS_FILTER_STORAGE_KEY = "openclaw:sidebar:sessions:status-filter";
216+
const SIDEBAR_SESSION_SORT_MODE_STORAGE_KEY = "openclaw:sidebar:sessions:sort-mode";
216217
const SIDEBAR_SESSION_COLLAPSED_SECTIONS_STORAGE_KEY =
217218
"openclaw:sidebar:sessions:collapsed-sections";
218219
const SIDEBAR_HIDDEN_SESSION_CATALOGS_STORAGE_KEY = "openclaw:sidebar:sessions:hidden-catalogs";
@@ -261,6 +262,13 @@ export function loadStoredSidebarSessionStatusFilter(): SidebarSessionStatusFilt
261262
return stored === "archived" || stored === "all" ? stored : "active";
262263
}
263264

265+
export function loadStoredSidebarSessionSortMode(): SidebarSessionSortMode {
266+
const stored = getSafeLocalStorage()?.getItem(SIDEBAR_SESSION_SORT_MODE_STORAGE_KEY);
267+
// "people" stays readable here even when the gateway later hides the
268+
// capability; effectiveSessionSortMode() downgrades it at render time.
269+
return stored === "updated" || stored === "people" ? stored : "created";
270+
}
271+
264272
export function loadStoredCollapsedSessionSections(): ReadonlySet<string> {
265273
try {
266274
const raw = getSafeLocalStorage()?.getItem(SIDEBAR_SESSION_COLLAPSED_SECTIONS_STORAGE_KEY);
@@ -315,6 +323,30 @@ export function storeSidebarSessionStatusFilter(value: SidebarSessionStatusFilte
315323
getSafeLocalStorage()?.setItem(SIDEBAR_SESSION_STATUS_FILTER_STORAGE_KEY, value);
316324
}
317325

326+
/** People collapses to Created only where the gateway has authoritatively
327+
* denied the capability; an undefined capability (reconnect) keeps the mode. */
328+
export function resolveSidebarSessionSortMode(
329+
mode: SidebarSessionSortMode,
330+
keepPeople: boolean,
331+
): SidebarSessionSortMode {
332+
return mode === "people" && !keepPeople ? "created" : mode;
333+
}
334+
335+
/** Persists the resolved mode, never the rejected request, so a reload cannot
336+
* restore a People sort the gateway already refused. Returns what was stored. */
337+
export function storeSidebarSessionSortMode(
338+
mode: SidebarSessionSortMode,
339+
peopleCapability: boolean | undefined,
340+
): SidebarSessionSortMode {
341+
const resolved = resolveSidebarSessionSortMode(mode, peopleCapability !== false);
342+
try {
343+
getSafeLocalStorage()?.setItem(SIDEBAR_SESSION_SORT_MODE_STORAGE_KEY, resolved);
344+
} catch {
345+
// Keep the in-memory preference when storage is unavailable.
346+
}
347+
return resolved;
348+
}
349+
318350
export function storeCollapsedSessionSections(sections: ReadonlySet<string>) {
319351
getSafeLocalStorage()?.setItem(
320352
SIDEBAR_SESSION_COLLAPSED_SECTIONS_STORAGE_KEY,

‎ui/src/test-helpers/app-sidebar-cases/sessions.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it, vi } from "vitest";
22
import type { GatewayBrowserClient } from "../../api/gateway.ts";
3+
import type { SidebarSessionSortMode } from "../../components/app-sidebar-session-types.ts";
34
import {
45
createContext,
56
createGateway,
@@ -15,6 +16,30 @@ import {
1516
import "./session-pagination.ts";
1617
import "./session-navigation.ts";
1718

19+
type SidebarSortModeHost = {
20+
sessionSortMode: SidebarSessionSortMode;
21+
setSessionSortMode: (mode: SidebarSessionSortMode) => void;
22+
};
23+
24+
describe("AppSidebar session sort persistence", () => {
25+
it("restores the selected sort mode on a later mount", async () => {
26+
const gateway = createGateway({} as GatewayBrowserClient);
27+
const first = await mountSidebar(gateway, createSessions("main", ["agent:main:session-a"]));
28+
const firstSidebar = first.sidebar as unknown as SidebarSortModeHost;
29+
expect(firstSidebar.sessionSortMode).toBe("created");
30+
31+
firstSidebar.setSessionSortMode("updated");
32+
expect(localStorage.getItem("openclaw:sidebar:sessions:sort-mode")).toBe("updated");
33+
34+
// A remount is what a reload does to this element; the preference must
35+
// survive it like every other stored sidebar choice.
36+
document.body.replaceChildren();
37+
const second = await mountSidebar(gateway, createSessions("main", ["agent:main:session-a"]));
38+
39+
expect((second.sidebar as unknown as SidebarSortModeHost).sessionSortMode).toBe("updated");
40+
});
41+
});
42+
1843
describe("AppSidebar session pagination", () => {
1944
it("does not show pagination controls at the ten-session boundary", async () => {
2045
const keys = [

0 commit comments

Comments
 (0)