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