Skip to content

Commit 3602a38

Browse files
refactor(web): document appearance and title are applied from app start
The root route mounted four null components (ContrastAppearanceSync, GlassAppearanceSync, FontAppearanceSync, DocumentTitleSync) whose only job was to copy client settings and the primary server version onto the document from seven useEffects. They re-rendered on every subscribed setting change, wrote the DOM a commit late, and were only mounted on some root branches. appInit.tsx now owns app-start work: startAppServices() runs the provider auth handoff, builds the router, sets the Electron document classes, and subscribes once to the client settings store and primaryServerConfigAtom, writing only the attribute groups that changed. main.tsx calls it. Effects removed: 7 in routes/__root.tsx (14 -> 7). Kept: BrowserChromeThemeSync (samples the painted surface per route, honestly post-paint), the theme sync, tracing, hosted-static bootstrap and EventRouter, which move in their own units. Behavior change: appearance settings (contrast, glass, fonts, chat width, diff colors, redesign flag) now also apply on /pair, /connect, /connect-agent, hosted prompt-link landings, the signed-out shell and the guest page, where those components were not mounted. The document title now also updates on the guest page. Both are set before the first render instead of after the first commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 725ada6 commit 3602a38

4 files changed

Lines changed: 111 additions & 124 deletions

File tree

‎apps/web/src/appInit.tsx‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import type { ClientSettings } from "@t3tools/contracts/settings";
2+
import { createBrowserHistory, createHashHistory } from "@tanstack/react-router";
3+
4+
import { applyAppearanceContrast } from "./appearanceContrast";
5+
import { applyAppearanceFontVariables } from "./appearanceFonts";
6+
import { APP_BASE_NAME, APP_DISPLAY_NAME, APP_STAGE_LABEL } from "./branding";
7+
import { resolveServerBackedAppDisplayName } from "./branding.logic";
8+
import { isElectron } from "./env";
9+
import { getClientSettings, subscribeClientSettings } from "./hooks/useSettings";
10+
import {
11+
syncDocumentElectronPlatformClasses,
12+
syncDocumentWindowControlsOverlayClass,
13+
} from "./lib/windowControlsOverlay";
14+
import { prepareProviderAuthDelivery } from "./providerAuthDelivery";
15+
import { getRouter, type AppRouter } from "./router";
16+
import { appAtomRegistry } from "./rpc/atomRegistry";
17+
import { primaryServerConfigAtom } from "./state/server";
18+
19+
/** Writes appearance settings onto the document, touching only the groups that changed. */
20+
function startClientAppearance(): void {
21+
const root = document.documentElement;
22+
let applied: ClientSettings | null = null;
23+
const apply = () => {
24+
const settings = getClientSettings();
25+
const previous = applied;
26+
applied = settings;
27+
if (previous?.diffColorScheme !== settings.diffColorScheme) {
28+
root.dataset.diffColorScheme = settings.diffColorScheme;
29+
}
30+
if (previous?.chatWidth !== settings.chatWidth) {
31+
root.dataset.chatWidth = settings.chatWidth;
32+
}
33+
if (previous?.redesignEnabled !== settings.redesignEnabled) {
34+
root.toggleAttribute("data-redesign", settings.redesignEnabled);
35+
}
36+
if (previous?.appearanceContrast !== settings.appearanceContrast) {
37+
applyAppearanceContrast(root, settings.appearanceContrast);
38+
}
39+
if (previous?.glassOpacity !== settings.glassOpacity) {
40+
root.style.setProperty("--glass-opacity", `${settings.glassOpacity}%`);
41+
if (settings.glassOpacity === 100) {
42+
root.style.setProperty("--glass-blur", "0px");
43+
} else {
44+
root.style.removeProperty("--glass-blur");
45+
}
46+
}
47+
if (
48+
previous?.fontFamilySans !== settings.fontFamilySans ||
49+
previous.fontFamilyCode !== settings.fontFamilyCode ||
50+
previous.fontFamilyComposer !== settings.fontFamilyComposer ||
51+
previous.fontSizeInterface !== settings.fontSizeInterface ||
52+
previous.fontSizePrompt !== settings.fontSizePrompt ||
53+
previous.fontSizeCode !== settings.fontSizeCode ||
54+
previous.fontSmoothing !== settings.fontSmoothing
55+
) {
56+
applyAppearanceFontVariables(root, {
57+
sans: settings.fontFamilySans,
58+
code: settings.fontFamilyCode,
59+
composer: settings.fontFamilyComposer,
60+
sizeInterface: settings.fontSizeInterface,
61+
sizePrompt: settings.fontSizePrompt,
62+
sizeCode: settings.fontSizeCode,
63+
smoothing: settings.fontSmoothing,
64+
});
65+
}
66+
};
67+
subscribeClientSettings(apply);
68+
apply();
69+
}
70+
71+
function startDocumentTitle(): void {
72+
appAtomRegistry.subscribe(
73+
primaryServerConfigAtom,
74+
(config) => {
75+
document.title = resolveServerBackedAppDisplayName({
76+
baseName: APP_BASE_NAME,
77+
fallbackDisplayName: APP_DISPLAY_NAME,
78+
fallbackStageLabel: APP_STAGE_LABEL,
79+
primaryServerVersion: config?.environment.serverVersion ?? null,
80+
});
81+
},
82+
{ immediate: true },
83+
);
84+
}
85+
86+
/**
87+
* Starts every app-lifetime service once, before the first render, and returns the router.
88+
* They run for the life of the page, so their disposers are dropped here.
89+
*/
90+
export function startAppServices(): AppRouter {
91+
// Ahead of the history: the provider auth code must leave the address before anything reads it.
92+
prepareProviderAuthDelivery();
93+
94+
// Electron loads the app from a file-backed shell, so hash history avoids path resolution issues.
95+
const router = getRouter(isElectron ? createHashHistory() : createBrowserHistory());
96+
97+
if (isElectron) {
98+
syncDocumentElectronPlatformClasses(navigator.platform);
99+
syncDocumentWindowControlsOverlayClass();
100+
}
101+
102+
startClientAppearance();
103+
startDocumentTitle();
104+
105+
return router;
106+
}

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function setClientSettingsHydrationStatus(nextStatus: ClientSettingsHydrationSta
104104
emitClientSettingsHydrationChange();
105105
}
106106

107-
function subscribeClientSettings(listener: () => void): () => void {
107+
export function subscribeClientSettings(listener: () => void): () => void {
108108
clientSettingsListeners.add(listener);
109109
void hydrateClientSettings().catch(() => undefined);
110110
return () => {

‎apps/web/src/main.tsx‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
import React from "react";
22
import ReactDOM from "react-dom/client";
3-
import { createHashHistory, createBrowserHistory } from "@tanstack/react-router";
43

54
import "@fontsource-variable/inter";
65
import "./index.css";
76

8-
import { prepareProviderAuthDelivery } from "./providerAuthDelivery";
9-
import { isElectron } from "./env";
107
import { hasCloudPublicConfig } from "./cloud/publicConfig";
11-
import { getRouter } from "./router";
12-
import {
13-
syncDocumentElectronPlatformClasses,
14-
syncDocumentWindowControlsOverlayClass,
15-
} from "./lib/windowControlsOverlay";
8+
import { isElectron } from "./env";
9+
import { startAppServices } from "./appInit";
1610
import { AppRoot } from "./AppRoot";
1711
import { clearChunkReloadGuard, reloadOnceForChunkLoadError } from "./lib/chunkReloadGuard";
1812

19-
prepareProviderAuthDelivery();
20-
21-
// Electron loads the app from a file-backed shell, so hash history avoids path resolution issues.
22-
const history = isElectron ? createHashHistory() : createBrowserHistory();
23-
24-
const router = getRouter(history);
25-
26-
if (isElectron) {
27-
syncDocumentElectronPlatformClasses(navigator.platform);
28-
syncDocumentWindowControlsOverlayClass();
29-
}
13+
const router = startAppServices();
3014

3115
const clerkPublishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined;
3216

‎apps/web/src/routes/__root.tsx‎

Lines changed: 1 addition & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
import { Check, Copy } from "lucide";
1515
import { useEffect, useEffectEvent, useRef, useState } from "react";
1616

17-
import { APP_BASE_NAME, APP_DISPLAY_NAME, APP_STAGE_LABEL, APP_VERSION } from "../branding";
18-
import { resolveServerBackedAppDisplayName } from "../branding.logic";
17+
import { APP_DISPLAY_NAME, APP_VERSION } from "../branding";
1918
import { AppSidebarLayout } from "../components/AppSidebarLayout";
2019
import { CommandPalette } from "../components/CommandPalette";
2120
import { CustomSnoozeDialogHost } from "../components/CustomSnoozeDialog";
@@ -56,8 +55,6 @@ import {
5655
} from "../components/ui/toast";
5756
import { isElectron } from "../env";
5857
import { cn } from "../lib/utils";
59-
import { applyAppearanceFontVariables } from "~/appearanceFonts";
60-
import { applyAppearanceContrast } from "~/appearanceContrast";
6158
import { useClientSettings } from "../hooks/useSettings";
6259
import {
6360
deriveLogicalProjectKeyFromSettings,
@@ -170,7 +167,6 @@ function RootRouteView() {
170167
if (pathname === "/pair" || pathname === "/connect" || pathname === "/connect-agent") {
171168
return (
172169
<>
173-
<DocumentTitleSync />
174170
<BrowserChromeThemeSync key={pathname} />
175171
<Outlet />
176172
</>
@@ -181,7 +177,6 @@ function RootRouteView() {
181177
if (hostedPromptLinkLanding !== null) {
182178
return (
183179
<>
184-
<DocumentTitleSync />
185180
<BrowserChromeThemeSync key={pathname} />
186181
{hostedPromptLinkLanding}
187182
</>
@@ -192,12 +187,8 @@ function RootRouteView() {
192187
return (
193188
<ToastProvider>
194189
<AnchoredToastProvider>
195-
<DocumentTitleSync />
196190
<BrowserChromeThemeSync key={pathname} />
197-
<ContrastAppearanceSync />
198191
<EnvironmentThemeSync />
199-
<GlassAppearanceSync />
200-
<FontAppearanceSync />
201192
<ProviderAuthCallbackCoordinator />
202193
<CustomSnoozeDialogHost />
203194
<GuestShareDialogHost />
@@ -214,7 +205,6 @@ function RootRouteView() {
214205
if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") {
215206
return (
216207
<>
217-
<DocumentTitleSync />
218208
<BrowserChromeThemeSync key={pathname} />
219209
<Outlet />
220210
</>
@@ -232,12 +222,8 @@ function RootRouteView() {
232222
return (
233223
<ToastProvider>
234224
<AnchoredToastProvider>
235-
<DocumentTitleSync />
236225
<BrowserChromeThemeSync key={pathname} />
237-
<ContrastAppearanceSync />
238226
<EnvironmentThemeSync />
239-
<GlassAppearanceSync />
240-
<FontAppearanceSync />
241227
<ProviderAuthCallbackCoordinator />
242228
<ChatGptWelcomeCoordinator />
243229
<FirstRunGate
@@ -285,78 +271,6 @@ function EnvironmentThemeSync() {
285271
return null;
286272
}
287273

288-
function ContrastAppearanceSync() {
289-
const appearanceContrast = useClientSettings((settings) => settings.appearanceContrast);
290-
const diffColorScheme = useClientSettings((settings) => settings.diffColorScheme);
291-
292-
useEffect(() => {
293-
document.documentElement.dataset.diffColorScheme = diffColorScheme;
294-
}, [diffColorScheme]);
295-
296-
const chatWidth = useClientSettings((settings) => settings.chatWidth);
297-
const redesignEnabled = useClientSettings((settings) => settings.redesignEnabled);
298-
useEffect(() => {
299-
document.documentElement.toggleAttribute("data-redesign", redesignEnabled);
300-
}, [redesignEnabled]);
301-
useEffect(() => {
302-
document.documentElement.dataset.chatWidth = chatWidth;
303-
}, [chatWidth]);
304-
305-
useEffect(() => {
306-
applyAppearanceContrast(document.documentElement, appearanceContrast);
307-
}, [appearanceContrast]);
308-
309-
return null;
310-
}
311-
312-
function GlassAppearanceSync() {
313-
const glassOpacity = useClientSettings((settings) => settings.glassOpacity);
314-
315-
useEffect(() => {
316-
const style = document.documentElement.style;
317-
style.setProperty("--glass-opacity", `${glassOpacity}%`);
318-
if (glassOpacity === 100) {
319-
style.setProperty("--glass-blur", "0px");
320-
} else {
321-
style.removeProperty("--glass-blur");
322-
}
323-
}, [glassOpacity]);
324-
325-
return null;
326-
}
327-
328-
function FontAppearanceSync() {
329-
const fontFamilySans = useClientSettings((settings) => settings.fontFamilySans);
330-
const fontFamilyCode = useClientSettings((settings) => settings.fontFamilyCode);
331-
const fontFamilyComposer = useClientSettings((settings) => settings.fontFamilyComposer);
332-
const fontSizeInterface = useClientSettings((settings) => settings.fontSizeInterface);
333-
const fontSizePrompt = useClientSettings((settings) => settings.fontSizePrompt);
334-
const fontSizeCode = useClientSettings((settings) => settings.fontSizeCode);
335-
const fontSmoothing = useClientSettings((settings) => settings.fontSmoothing);
336-
337-
useEffect(() => {
338-
applyAppearanceFontVariables(document.documentElement, {
339-
sans: fontFamilySans,
340-
code: fontFamilyCode,
341-
composer: fontFamilyComposer,
342-
sizeInterface: fontSizeInterface,
343-
sizePrompt: fontSizePrompt,
344-
sizeCode: fontSizeCode,
345-
smoothing: fontSmoothing,
346-
});
347-
}, [
348-
fontFamilyCode,
349-
fontFamilyComposer,
350-
fontFamilySans,
351-
fontSizeCode,
352-
fontSizeInterface,
353-
fontSizePrompt,
354-
fontSmoothing,
355-
]);
356-
357-
return null;
358-
}
359-
360274
// Each route paints its own surface for browser chrome to sample.
361275
function BrowserChromeThemeSync() {
362276
useEffect(() => {
@@ -370,23 +284,6 @@ function BrowserChromeThemeSync() {
370284
return null;
371285
}
372286

373-
function DocumentTitleSync() {
374-
const primaryServerVersion =
375-
useAtomValue(primaryServerConfigAtom)?.environment.serverVersion ?? null;
376-
const title = resolveServerBackedAppDisplayName({
377-
baseName: APP_BASE_NAME,
378-
fallbackDisplayName: APP_DISPLAY_NAME,
379-
fallbackStageLabel: APP_STAGE_LABEL,
380-
primaryServerVersion,
381-
});
382-
383-
useEffect(() => {
384-
document.title = title;
385-
}, [title]);
386-
387-
return null;
388-
}
389-
390287
function HostedStaticEnvironmentBootstrap() {
391288
const { environments } = useEnvironments();
392289
const activeEnvironmentId = useActiveEnvironmentId();

0 commit comments

Comments
 (0)