@devries-cameron20 / t3code Mirror · read-only / commits / 9803d1adb5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(web): paint the selected environment theme from the first frame on launch (#36042)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Aviddaram committed Nov 14, 2026 at 14:40 UTC 9803d1adb5352a59aa78624e6465fd4ae9968c93
8 files changed +120 -24
apps/web/index.html
+11 -5
@@ -15,6 +15,7 @@
15 const LIGHT_BACKGROUND = "#ffffff";
16 const DARK_BACKGROUND = "#000000";
17 const CUSTOM_THEMES_STORAGE_KEY = "t3code:themes:v1";
18 + const ENVIRONMENT_THEMES_STORAGE_KEY = "t3code:environment-themes:v1";
19 const THEME_FOLLOW_SYSTEM_STORAGE_KEY = "t3code:theme-follow-system";
20 const THEME_APPEARANCE_MODE_STORAGE_KEY = "t3code:theme-appearance-mode";
21 const THEME_HALVES_STORAGE_KEY = "t3code:theme-halves:v1";
@@ -197,12 +198,9 @@
198 (appearance === value.appearance || isRecord(colors)),
199 );
200 };
200 - const findCustomTheme = (themeId) => {
201 - if (RESERVED_THEME_IDS.includes(themeId)) return null;
201 + const findStoredTheme = (storageKey, themeId) => {
202 try {
203 - const parsed = JSON.parse(
204 - window.localStorage.getItem(CUSTOM_THEMES_STORAGE_KEY) ?? "null",
205 - );
203 + const parsed = JSON.parse(window.localStorage.getItem(storageKey) ?? "null");
204 if (!Array.isArray(parsed)) return null;
205 return (
206 parsed.find((value) => isStoredCustomTheme(value) && value.id === themeId) ?? null
@@ -211,6 +209,14 @@
209 return null;
210 }
211 };
212 + // Same precedence as getThemeDefinition: saved custom themes, then published ones.
213 + const findCustomTheme = (themeId) => {
214 + if (RESERVED_THEME_IDS.includes(themeId)) return null;
215 + return (
216 + findStoredTheme(CUSTOM_THEMES_STORAGE_KEY, themeId) ??
217 + findStoredTheme(ENVIRONMENT_THEMES_STORAGE_KEY, themeId)
218 + );
219 + };
220
221 try {
222 const storedTheme = window.localStorage.getItem("t3code:theme");
apps/web/src/hooks/useEnvironmentTheme.test.ts
+26 -4
@@ -2,13 +2,11 @@ import type { EnvironmentTheme } from "@t3tools/contracts";
2 import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3
4 const publishedThemes = vi.hoisted(() => ({
5 - atom: null as unknown as import("effect/reactivity").Atom.Writable<
6 - ReadonlyArray<EnvironmentTheme>
7 - >,
5 + atom: null as unknown as import("effect/reactivity").Atom.Writable<ReadonlyArray<EnvironmentTheme> | null>,
6 }));
7 vi.mock(import("../state/server"), async () => {
8 const { Atom } = await import("effect/reactivity");
11 - publishedThemes.atom = Atom.make<ReadonlyArray<EnvironmentTheme>>([]);
9 + publishedThemes.atom = Atom.make<ReadonlyArray<EnvironmentTheme> | null>([]);
10 return { themeServerEnvironmentThemesAtom: publishedThemes.atom } as never;
11 });
12 const refreshTheme = vi.hoisted(() => vi.fn());
@@ -21,6 +19,7 @@ import {
19 invalidateCustomThemes,
20 publishedThemeDefinitions,
21 } from "../themePalette";
22 +import { startEnvironmentThemeRepaint } from "./useEnvironmentTheme";
23
24 const NIGHTFALL_THEME = {
25 id: "nightfall",
@@ -122,6 +121,29 @@ describe("environment themes", () => {
121 expect(getThemeDefinition("nightfall")?.label).toBe("Nightfall");
122 });
123
124 + it("keeps the last published palette while the server has not answered", () => {
125 + const store = new Map<string, string>();
126 + vi.stubGlobal("window", {
127 + localStorage: {
128 + getItem: (key: string) => store.get(key) ?? null,
129 + setItem: (key: string, value: string) => store.set(key, value),
130 + },
131 + });
132 + const stop = startEnvironmentThemeRepaint();
133 + try {
134 + appAtomRegistry.set(publishedThemes.atom, [NIGHTFALL_THEME]);
135 + appAtomRegistry.set(publishedThemes.atom, null);
136 + expect(getThemeDefinition("nightfall")?.label).toBe("Nightfall");
137 +
138 + appAtomRegistry.set(publishedThemes.atom, []);
139 + appAtomRegistry.set(publishedThemes.atom, null);
140 + expect(getThemeDefinition("nightfall")).toBeNull();
141 + } finally {
142 + stop();
143 + vi.unstubAllGlobals();
144 + }
145 + });
146 +
147 it("repaints when the environment publishes themes nothing has read yet", async () => {
148 vi.resetModules();
149 const { appAtomRegistry: registry } = await import("../rpc/atomRegistry");
apps/web/src/hooks/useEnvironmentTheme.ts
+7 -2
@@ -1,7 +1,11 @@
1 import { useAtomValue } from "@effect/atom-react";
2
3 import { subscribeAtomChanges } from "../rpc/atomRegistry";
4 -import { environmentThemeDefinitionsAtom, type ThemeDefinition } from "../themePalette";
4 +import {
5 + environmentThemeDefinitionsAtom,
6 + storePublishedEnvironmentThemes,
7 + type ThemeDefinition,
8 +} from "../themePalette";
9 import { refreshTheme } from "./useTheme";
10
11 export function useEnvironmentThemeDefinitions(): ReadonlyArray<ThemeDefinition> {
@@ -13,7 +17,8 @@ export function useEnvironmentThemeDefinitions(): ReadonlyArray<ThemeDefinition>
17 * only land if the active theme is re-applied.
18 */
19 export function startEnvironmentThemeRepaint(): () => void {
16 - return subscribeAtomChanges(environmentThemeDefinitionsAtom, () => {
20 + return subscribeAtomChanges(environmentThemeDefinitionsAtom, (definitions) => {
21 + storePublishedEnvironmentThemes(definitions);
22 refreshTheme({ preservePreview: true });
23 });
24 }
apps/web/src/state/server.ts
+13 -2
@@ -171,9 +171,20 @@ const themeServerConfigAtom = Atom.make((get): ServerConfig | null => {
171 return environmentId === null ? null : get(serverEnvironment.configValueAtom(environmentId));
172 }).pipe(Atom.withLabel("web-theme-server-config"));
173
174 +/**
175 + * Null until the theme environment says what it publishes. Cached configs and
176 + * snapshots never carry themes, so a capable server has none until its theme
177 + * event lands.
178 + */
179 export const themeServerEnvironmentThemesAtom = Atom.make(
175 - (get): ReadonlyArray<EnvironmentTheme> =>
176 - get(themeServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
180 + (get): ReadonlyArray<EnvironmentTheme> | null => {
181 + const config = get(themeServerConfigAtom);
182 + if (config === null) return null;
183 + if (config.environmentThemes !== undefined) return config.environmentThemes;
184 + return config.environment.capabilities.environmentThemes === true
185 + ? null
186 + : EMPTY_ENVIRONMENT_THEMES;
187 + },
188 ).pipe(Atom.withLabel("web-theme-server-environment-themes"));
189
190 export const themeServerSettingsAtom = Atom.make(
apps/web/src/themeBoot.test.ts
+26
@@ -3,10 +3,12 @@ import { describe, expect, it, vi } from "vite-plus/test";
3 import indexHtml from "../index.html?raw";
4 import {
5 CUSTOM_THEMES_STORAGE_KEY,
6 + ENVIRONMENT_THEMES_STORAGE_KEY,
7 getDefaultThemeColors,
8 getThemeColorsForMode,
9 invalidateCustomThemes,
10 isKnownThemePreference,
11 + publishedThemeDefinitions,
12 resolveThemeAppearance,
13 T3_CHAT_THEME,
14 EMBER_THEME,
@@ -283,6 +285,30 @@ describe("index.html boot script", () => {
285 expect(boot.metaContent).toBe(colors.chrome);
286 });
287
288 + it("paints a published theme from its stored copy before the server connects", () => {
289 + const [published] = publishedThemeDefinitions([
290 + {
291 + id: "nightfall",
292 + name: "Nightfall",
293 + appearance: "dark",
294 + canvas: "#1a1b26",
295 + accent: "#7aa2f7",
296 + },
297 + ]);
298 + if (!published) throw new Error("expected the published theme");
299 + const boot = runBootScript({
300 + storage: {
301 + [THEME_STORAGE_KEY]: "nightfall",
302 + [ENVIRONMENT_THEMES_STORAGE_KEY]: JSON.stringify([published]),
303 + },
304 + prefersDark: true,
305 + });
306 +
307 + expect(boot.themeId).toBe("nightfall");
308 + expect(boot.bootVariables["--boot-background"]).toBe(published.colors.canvas);
309 + expect(boot.backgroundColor).toBe(published.colors.chrome);
310 + });
311 +
312 it("keeps every built-in boot splash in sync with the real palettes", () => {
313 for (const theme of [T3_CHAT_THEME, GROVE_THEME, OCEAN_THEME, EMBER_THEME, IRIS_THEME]) {
314 expect(theme.appearance).toBe("light");
apps/web/src/themePalette.ts
+33 -8
@@ -46,6 +46,7 @@ export const CUSTOM_THEMES_STORAGE_KEY = "t3code:themes:v1";
46 export const THEME_FOLLOW_SYSTEM_STORAGE_KEY = "t3code:theme-follow-system";
47 export const THEME_APPEARANCE_MODE_STORAGE_KEY = "t3code:theme-appearance-mode";
48 export const THEME_HALVES_STORAGE_KEY = "t3code:theme-halves:v1";
49 +export const ENVIRONMENT_THEMES_STORAGE_KEY = "t3code:environment-themes:v1";
50
51 const LEGACY_T3_CHAT_DARK_THEME_ID = "t3-chat-dark";
52
@@ -322,20 +323,44 @@ export function publishedThemeDefinitions(
323
324 // Every reconnect snapshot delivers a fresh but usually identical array; keeping the
325 // previous one skips regenerating palettes and repainting the document for it.
325 -const publishedThemesAtom = Atom.make((get): ReadonlyArray<EnvironmentTheme> => {
326 +const publishedThemesAtom = Atom.make((get): ReadonlyArray<EnvironmentTheme> | null => {
327 const next = get(themeServerEnvironmentThemesAtom);
327 - const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentTheme>>());
328 + const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentTheme> | null>());
329 return previous !== null && Equal.equals(previous, next) ? previous : next;
330 }).pipe(Atom.withLabel("web-published-environment-themes"));
331
332 +function readStoredEnvironmentThemes(): ReadonlyArray<ThemeDefinition> {
333 + try {
334 + const parsed: unknown = JSON.parse(
335 + window.localStorage.getItem(ENVIRONMENT_THEMES_STORAGE_KEY) ?? "null",
336 + );
337 + return Array.isArray(parsed) ? parseStoredThemes(parsed) : [];
338 + } catch {
339 + return [];
340 + }
341 +}
342 +
343 /**
332 - * The environment's palettes are not saved: they are republished by the server
333 - * on every change and would go stale the moment the machine's theme moved on.
334 - * Kept alive because theme lookups read it outside React on every repaint.
344 + * Falls back to the last published set while the server connects, so boot and the
345 + * first render paint the selected palette. Kept alive because theme lookups read it
346 + * outside React on every repaint.
347 */
336 -export const environmentThemeDefinitionsAtom = Atom.make((get) =>
337 - publishedThemeDefinitions(get(publishedThemesAtom)),
338 -).pipe(Atom.keepAlive, Atom.withLabel("web-environment-theme-definitions"));
348 +export const environmentThemeDefinitionsAtom = Atom.make((get) => {
349 + const published = get(publishedThemesAtom);
350 + const definitions =
351 + published === null ? readStoredEnvironmentThemes() : publishedThemeDefinitions(published);
352 + const previous = Option.getOrNull(get.self<ReadonlyArray<ThemeDefinition>>());
353 + return previous !== null && Equal.equals(previous, definitions) ? previous : definitions;
354 +}).pipe(Atom.keepAlive, Atom.withLabel("web-environment-theme-definitions"));
355 +
356 +export function storePublishedEnvironmentThemes(definitions: ReadonlyArray<ThemeDefinition>) {
357 + if (appAtomRegistry.get(publishedThemesAtom) === null) return;
358 + try {
359 + window.localStorage.setItem(ENVIRONMENT_THEMES_STORAGE_KEY, JSON.stringify(definitions));
360 + } catch {
361 + // Without the stored copy the next launch paints stock colors until connect.
362 + }
363 +}
364
365 function getEnvironmentThemes(): ReadonlyArray<ThemeDefinition> {
366 return appAtomRegistry.get(environmentThemeDefinitionsAtom);
packages/client-runtime/src/rpc/session.test.ts
+2 -2
@@ -664,7 +664,7 @@ describe("RpcSessionFactory", () => {
664 for (const event of replay) {
665 projection = applyServerConfigProjection(projection, event);
666 }
667 - expect(Option.getOrThrow(projection).config.environmentThemes).toBeUndefined();
667 + expect(Option.getOrThrow(projection).config.environmentThemes).toEqual([]);
668 }),
669 );
670
@@ -791,7 +791,7 @@ describe("RpcSessionFactory", () => {
791 for (const event of recovered.slice(1)) {
792 projection = applyServerConfigProjection(projection, event);
793 }
794 - expect(Option.getOrThrow(projection).config.environmentThemes).toBeUndefined();
794 + expect(Option.getOrThrow(projection).config.environmentThemes).toEqual([]);
795 expect(Option.getOrThrow(projection).config.usageLimitSources).toBeUndefined();
796 }),
797 );
packages/client-runtime/src/state/serverConfigProjection.ts
+2 -1
@@ -166,7 +166,8 @@ export function applyServerConfigProjection(
166 return Option.map(current, (projection) => ({
167 config: {
168 ...projection.config,
169 - environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
169 + // Kept when empty: undefined means the theme event has not arrived yet.
170 + environmentThemes: event.payload.themes,
171 },
172 latestEvent: event,
173 source: "live",