Repository navigation
Expand file tree
/
Copy pathappInit.tsx
More file actions
226 lines (214 loc) · 9.82 KB
/
Copy pathappInit.tsx
File metadata and controls
226 lines (214 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
import type { ClientSettings } from "@t3tools/contracts/settings";
import { createBrowserHistory, createHashHistory } from "@tanstack/react-router";
import * as Schema from "effect/Schema";
import { applyAppearanceContrast } from "./appearanceContrast";
import { applyAppearanceFontVariables, TYPOGRAPHY_ADVANCED_STORAGE_KEY } from "./appearanceFonts";
import { startElectronBrowserHost } from "./browser/ElectronBrowserHost";
import { startBrowserProfileReporting } from "./browser/BrowserProfileReporter";
import { startPreviewTitleRecording } from "./browserHistoryStore";
import { retainThreadOverlaysForThread } from "./components/chat/threadOverlayRetention";
import { startBackgroundThreadAnnouncements } from "./components/BackgroundThreadAnnouncer";
import { startSshPasswordPromptQueue } from "./components/desktop/SshPasswordPromptDialog";
import { startHostFreezeToasts } from "./components/HostFreezeToastCoordinator";
import { startKeybindingsUpdateToasts } from "./components/KeybindingsUpdateToast";
import { startLegacyThreadMigrationToast } from "./components/LegacyThreadMigrationToast";
import { startPermissionUpdateNotices } from "./components/PermissionUpdateNotice";
import { startAgentOpenedPreviews } from "./components/preview/agentOpenedPreviews";
import { startMainAppLocationTracking } from "./components/sidebar/mainAppLocation";
import { startDesktopUpdateLaunchNotice } from "./components/sidebar/updateEverythingRun";
import { startSlowRpcRequestToasts } from "./components/SlowRpcRequestToastCoordinator";
import { startThreadNotifications } from "./components/ThreadNotificationCoordinator";
import { startGuideRunNotifications } from "./components/pullRequest/guide/PullRequestGuideRunCoordinator";
import { APP_BASE_NAME, APP_DISPLAY_NAME, APP_STAGE_LABEL } from "./branding";
import { resolveServerBackedAppDisplayName } from "./branding.logic";
import { isElectron } from "./env";
import {
subscribeComposerDraftModelReconciliation,
subscribeDraftThreadPromotion,
useComposerDraftStore,
} from "./composerDraftStore";
import { startDefaultThemeAdoption } from "./hooks/useDefaultTheme";
import { startEnvironmentThemeRepaint } from "./hooks/useEnvironmentTheme";
import { readLocalStorageValue, subscribeLocalStorageKey } from "./hooks/useLocalStorage";
import { startHostEnvironmentLookup } from "./state/primaryEnvironment";
import {
getClientSettings,
startModelPreferencesMigration,
subscribeClientSettings,
} from "./hooks/useSettings";
import { syncBrowserChromeTheme } from "./hooks/useTheme";
import { startThreadVisitedMigration } from "./hooks/useThreadVisitedMigration";
import { installDesktopPasteAsText } from "./lib/desktopPasteAsText";
import {
syncDocumentElectronPlatformClasses,
syncDocumentWindowControlsOverlayClass,
} from "./lib/windowControlsOverlay";
import { startProviderAuthDelivery } from "./providerAuthDelivery";
import { getRouter, type AppRouter } from "./router";
import { appAtomRegistry } from "./rpc/atomRegistry";
import { primaryServerConfigAtom } from "./state/server";
import { environmentShell } from "./state/shell";
import { installChatRouteCommands } from "./shortcuts/chatRouteCommands";
import { startWorkspaceFocus } from "./lib/workspaceFocus";
import { installCommandPaletteCommands } from "./shortcuts/commandPaletteCommands";
import { registerOpenFavoriteEditorCommand } from "./components/chat/OpenInPickerShortcut";
import { environmentThreadShells, threadEnvironment } from "./state/threads";
import { resolveThreadRouteTarget } from "./threadRoutes";
/** Writes appearance settings onto the document, touching only the groups that changed. */
function startClientAppearance(): void {
const root = document.documentElement;
let applied: ClientSettings | null = null;
let appliedSeparateFonts: boolean | null = null;
const apply = () => {
const settings = getClientSettings();
const previous = applied;
applied = settings;
const separateFonts = readLocalStorageValue(
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
false,
Schema.Boolean,
);
const previousSeparateFonts = appliedSeparateFonts;
appliedSeparateFonts = separateFonts;
if (previous?.diffColorScheme !== settings.diffColorScheme) {
root.dataset.diffColorScheme = settings.diffColorScheme;
}
if (previous?.chatWidth !== settings.chatWidth) {
root.dataset.chatWidth = settings.chatWidth;
}
if (previous?.appearanceContrast !== settings.appearanceContrast) {
applyAppearanceContrast(root, settings.appearanceContrast);
}
if (previous?.glassOpacity !== settings.glassOpacity) {
root.style.setProperty("--glass-opacity", `${settings.glassOpacity}%`);
if (settings.glassOpacity === 100) {
root.style.setProperty("--glass-blur", "0px");
} else {
root.style.removeProperty("--glass-blur");
}
}
if (
previous?.fontFamilySans !== settings.fontFamilySans ||
previous.fontFamilyCode !== settings.fontFamilyCode ||
previous.fontFamilyComposer !== settings.fontFamilyComposer ||
previous.fontSizeInterface !== settings.fontSizeInterface ||
previous.fontSizePrompt !== settings.fontSizePrompt ||
previous.fontSizeCode !== settings.fontSizeCode ||
previous.fontSmoothing !== settings.fontSmoothing ||
previousSeparateFonts !== separateFonts
) {
applyAppearanceFontVariables(root, {
sans: settings.fontFamilySans,
code: settings.fontFamilyCode,
composer: settings.fontFamilyComposer,
sizeInterface: settings.fontSizeInterface,
sizePrompt: settings.fontSizePrompt,
sizeCode: settings.fontSizeCode,
smoothing: settings.fontSmoothing,
separate: separateFonts,
});
}
};
subscribeClientSettings(apply);
subscribeLocalStorageKey(TYPOGRAPHY_ADVANCED_STORAGE_KEY, apply);
apply();
}
function startDocumentTitle(): void {
appAtomRegistry.subscribe(
primaryServerConfigAtom,
(config) => {
document.title = resolveServerBackedAppDisplayName({
baseName: APP_BASE_NAME,
fallbackDisplayName: APP_DISPLAY_NAME,
fallbackStageLabel: APP_STAGE_LABEL,
primaryServerVersion: config?.environment.serverVersion ?? null,
});
},
{ immediate: true },
);
}
/**
* Each route paints its own surface, which the browser chrome samples once it is on screen.
* onRendered only fires for a new href; onResolved also covers `router.invalidate()`, which
* can swap the root between the bare outlet and the shell at the same address.
*/
function startBrowserChromeThemeSync(router: AppRouter): void {
const resync = () => window.requestAnimationFrame(syncBrowserChromeTheme);
router.subscribe("onRendered", resync);
router.subscribe("onResolved", resync);
}
/** Chat overlays belong to the thread they were opened in, so leaving its route drops them. */
function startThreadOverlayRetention(router: AppRouter): void {
router.subscribe("onResolved", () => {
const target = resolveThreadRouteTarget(router.state.matches.at(-1)?.params ?? {});
const draftSession =
target?.kind === "draft"
? useComposerDraftStore.getState().getDraftSession(target.draftId)
: null;
const threadRef =
target?.kind === "server"
? target.threadRef
: draftSession
? scopeThreadRef(draftSession.environmentId, draftSession.threadId)
: null;
retainThreadOverlaysForThread(threadRef ? scopedThreadKey(threadRef) : null);
});
}
/**
* Starts every app-lifetime service once, before the first render, and returns the router.
* They run for the life of the page, so their disposers are dropped here.
*/
export function startAppServices(): AppRouter {
// Ahead of the history: the provider auth code must leave the address before anything reads it.
startProviderAuthDelivery();
// Electron loads the app from a file-backed shell, so hash history avoids path resolution issues.
const router = getRouter(isElectron ? createHashHistory() : createBrowserHistory());
if (isElectron) {
syncDocumentElectronPlatformClasses(navigator.platform);
syncDocumentWindowControlsOverlayClass();
}
installDesktopPasteAsText(window.desktopBridge, window);
startClientAppearance();
startDocumentTitle();
startHostEnvironmentLookup();
startEnvironmentThemeRepaint();
startBrowserChromeThemeSync(router);
startDefaultThemeAdoption();
startModelPreferencesMigration();
startAgentOpenedPreviews();
if (isElectron) startElectronBrowserHost();
startPreviewTitleRecording();
startBrowserProfileReporting();
installChatRouteCommands(router);
installCommandPaletteCommands(router);
registerOpenFavoriteEditorCommand();
startThreadOverlayRetention(router);
// Drafts promote as their server threads arrive, wherever the user is.
subscribeDraftThreadPromotion({
registry: appAtomRegistry,
threadShellAtom: environmentThreadShells.quietThreadShellAtom,
threadRefsAtom: environmentThreadShells.threadRefsAtom,
});
subscribeComposerDraftModelReconciliation({
registry: appAtomRegistry,
threadShellAtom: environmentThreadShells.threadShellAtom,
threadRefsAtom: environmentThreadShells.threadRefsAtom,
shellStateAtom: environmentShell.stateAtom,
pendingThreadIdsAtom: threadEnvironment.pendingThreadIdsAtom,
});
startMainAppLocationTracking(router);
startWorkspaceFocus(router);
startBackgroundThreadAnnouncements(appAtomRegistry, router);
startThreadVisitedMigration(appAtomRegistry);
startSshPasswordPromptQueue();
startKeybindingsUpdateToasts();
startSlowRpcRequestToasts();
startHostFreezeToasts();
startLegacyThreadMigrationToast();
startPermissionUpdateNotices(router);
startThreadNotifications(appAtomRegistry, router);
startGuideRunNotifications(appAtomRegistry, router);
if (isElectron) startDesktopUpdateLaunchNotice();
return router;
}