Skip to content

Commit d585c0c

Browse files
authored
fix(ui): keep newer Appearance choices after delayed theme imports [AI-assisted] (#117292)
* fix(ui): fence stale custom theme imports * test(ui): split custom theme ownership coverage * fix(ui): fence external theme import ownership * fix(ui): separate theme import activation ownership * fix(ui): bind theme imports to authoritative intent * refactor(ui): own custom theme import lifecycles * test(ui): refresh memory helper routing expectation * test(ui): emit custom theme race verdict
1 parent b721cff commit d585c0c

10 files changed

Lines changed: 1026 additions & 71 deletions

‎ui/src/app/app-host.server-prefs.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,36 @@ describe("OpenClaw shell locale preferences", () => {
105105
expect(loadSettings().locale).toBe("fr");
106106
expect(refreshTheme).toHaveBeenCalledOnce();
107107
});
108+
109+
it("publishes authored theme changes when the local mirror needs no patch", () => {
110+
const state = {
111+
configSnapshot: {
112+
config: { ui: { prefs: { theme: "custom" } } },
113+
hash: "theme-custom",
114+
},
115+
};
116+
const runtimeConfig = { state } as unknown as ApplicationContext["runtimeConfig"];
117+
const recordServerSelection = vi.fn();
118+
const context = {
119+
gateway: { connection: { gatewayUrl: "ws://theme.test" } },
120+
navigation: { update: vi.fn() },
121+
theme: { recordServerSelection, refresh: vi.fn(), serverSelection: null },
122+
runtimeConfig,
123+
} as unknown as ApplicationContext;
124+
const shell = document.createElement(
125+
"openclaw-app-shell",
126+
) as unknown as ShellServerPreferencesState;
127+
shell.runtime = { context };
128+
129+
shell.reconcileServerUiPrefs(runtimeConfig);
130+
expect(recordServerSelection).toHaveBeenLastCalledWith("custom", "ws://theme.test");
131+
132+
state.configSnapshot = {
133+
config: { ui: { prefs: { theme: "claw" } } },
134+
hash: "theme-claw",
135+
};
136+
shell.reconcileServerUiPrefs(runtimeConfig);
137+
expect(recordServerSelection).toHaveBeenLastCalledWith("claw", "ws://theme.test");
138+
expect(loadSettings().theme).toBe("claw");
139+
});
108140
});

‎ui/src/app/app-shell-gateway.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class ShellGatewayOwner {
9797
const scope = context.gateway.connection.gatewayUrl;
9898
applyServerUiPrefs(snapshot.config, {
9999
scope,
100+
onThemeChanged: (theme) => context.theme.recordServerSelection(theme, scope),
100101
onApplied: (patch) => {
101102
if (patch.sidebarEntries !== undefined) {
102103
context.navigation.update({ sidebarEntries: patch.sidebarEntries });

‎ui/src/app/bootstrap.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import type {
3434
ApplicationNavigationPreferencesSnapshot,
3535
ApplicationSkillWorkshopRevisionHandoff,
3636
ApplicationTheme,
37+
ApplicationThemeServerSelection,
3738
} from "./context.ts";
3839
import { syncCustomThemeStyleTag } from "./custom-theme.ts";
3940
import { createApplicationGateway } from "./gateway-store.ts";
@@ -78,6 +79,7 @@ function createApplicationTheme(
7879
initialSettings: UiSettings,
7980
): ApplicationTheme & { dispose: () => void } {
8081
let settings = initialSettings;
82+
let serverSelection: ApplicationThemeServerSelection | null = null;
8183
let systemThemeCleanup: (() => void) | undefined;
8284
const listeners = new Set<() => void>();
8385

@@ -119,6 +121,13 @@ function createApplicationTheme(
119121
get mode() {
120122
return settings.themeMode;
121123
},
124+
get serverSelection() {
125+
return serverSelection;
126+
},
127+
recordServerSelection(theme, scope) {
128+
serverSelection = { revision: (serverSelection?.revision ?? 0) + 1, scope, theme };
129+
publish();
130+
},
122131
setMode(mode: ThemeMode, element) {
123132
const currentSettings = loadSettings();
124133
const nextSettings = { ...currentSettings, themeMode: mode };

‎ui/src/app/context.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { ApplicationGateway } from "./gateway.ts";
1313
import type { NativeChatDrafts } from "./native-bridge.ts";
1414
import type { NativeNotificationsCapability } from "./native-notifications.ts";
1515
import type { ApplicationOverlays } from "./overlays.ts";
16-
import type { ThemeMode } from "./theme.ts";
16+
import type { ThemeMode, ThemeName } from "./theme.ts";
1717
import type { WebPushCapability } from "./web-push.ts";
1818

1919
export type {
@@ -23,8 +23,16 @@ export type {
2323
ApplicationGatewaySnapshot,
2424
} from "./gateway.ts";
2525

26+
export type ApplicationThemeServerSelection = {
27+
readonly revision: number;
28+
readonly scope: string;
29+
readonly theme: ThemeName | null;
30+
};
31+
2632
export type ApplicationTheme = {
2733
readonly mode: ThemeMode;
34+
readonly serverSelection: ApplicationThemeServerSelection | null;
35+
recordServerSelection: (theme: ThemeName | null, scope: string) => void;
2836
setMode: (mode: ThemeMode, element?: HTMLElement | null) => void;
2937
refresh: () => void;
3038
subscribe: (listener: () => void) => () => void;

‎ui/src/app/server-prefs.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,18 @@ describe("applyServerUiPrefs", () => {
290290

291291
it("ignores a server custom theme until this browser imported one", () => {
292292
const onApplied = vi.fn();
293-
expect(applyServerUiPrefs(configWithPrefs({ theme: "custom" }), { onApplied })).toBe(false);
293+
const onThemeChanged = vi.fn();
294+
expect(
295+
applyServerUiPrefs(configWithPrefs({ theme: "custom" }), { onApplied, onThemeChanged }),
296+
).toBe(false);
294297
expect(loadSettings().theme).toBe("claw");
298+
expect(onThemeChanged).toHaveBeenLastCalledWith("custom");
299+
300+
expect(
301+
applyServerUiPrefs(configWithPrefs({ theme: "claw" }), { onApplied, onThemeChanged }),
302+
).toBe(false);
303+
expect(onThemeChanged).toHaveBeenLastCalledWith("claw");
304+
expect(onApplied).not.toHaveBeenCalled();
295305
});
296306
});
297307

@@ -457,6 +467,37 @@ describe("pushServerUiPrefs", () => {
457467
JSON.parse(localStorage.getItem(pendingKey(scope)) ?? "{}") as Record<string, unknown>;
458468
const createClient = createServerPrefsWriter;
459469

470+
it("does not publish a server theme change shadowed by pending local intent", async () => {
471+
const scope = "ws://gw";
472+
const requestGate = deferred();
473+
const request = vi.fn<(method: string, params?: unknown) => Promise<unknown>>(
474+
() => requestGate.promise,
475+
);
476+
const client = createClient(request, scope);
477+
const onApplied = vi.fn();
478+
const onThemeChanged = vi.fn();
479+
applyServerUiPrefs(configWithPrefs({ theme: "claw" }), {
480+
scope,
481+
onApplied,
482+
onThemeChanged,
483+
});
484+
onThemeChanged.mockClear();
485+
486+
pushServerUiPrefs(client, { theme: "knot" });
487+
await vi.waitFor(() => expect(request).toHaveBeenCalledOnce());
488+
expect(
489+
applyServerUiPrefs(configWithPrefs({ theme: "knot" }), {
490+
scope,
491+
onApplied,
492+
onThemeChanged,
493+
}),
494+
).toBe(false);
495+
expect(onThemeChanged).not.toHaveBeenCalled();
496+
497+
requestGate.resolve({});
498+
await vi.waitFor(() => expect(localStorage.getItem(pendingKey(scope))).toBeNull());
499+
});
500+
460501
it("keeps a synced default reset as a pending offline null intent", () => {
461502
const scope = "ws://gw";
462503
const previous = loadSettings();

‎ui/src/app/server-prefs.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ export function applyServerUiPrefs(
415415
hooks: {
416416
scope?: string;
417417
onApplied: (patch: Partial<UiSettings>) => void;
418+
onThemeChanged?: (theme: ThemeName | null) => void;
418419
},
419420
): boolean {
420421
const scope = hooks.scope ?? "";
@@ -457,6 +458,9 @@ export function applyServerUiPrefs(
457458
}
458459
writeStorage(LAST_SEEN_KEY, scope, key);
459460
recordReconciledObject();
461+
if (Object.hasOwn(changed, "theme")) {
462+
hooks.onThemeChanged?.(changed.theme ?? null);
463+
}
460464
const patch = serverPrefsLocalPatch(changed, loadSettings());
461465
if (!patch) {
462466
return false;

0 commit comments

Comments
 (0)