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