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