Open History to see the last commit
1import { installPermissionUpdateNotice } from "./features/connection/permissionUpdateNotice";
2import { installComposerAttachmentUploads } from "./state/composer-attachment-uploads";
3import { installThreadOutboxDelivery } from "./state/threadOutboxDelivery";
4import * as Linking from "expo-linking";
5import * as SplashScreen from "expo-splash-screen";
6import { useEffect } from "react";
7import { StatusBar } from "react-native";
8import { GestureHandlerRootView } from "react-native-gesture-handler";
9import { KeyboardProvider } from "react-native-keyboard-controller";
10import { SafeAreaProvider } from "react-native-safe-area-context";
11import {
12 CommonActions,
13 createPathConfigForStaticNavigation,
14 createStaticNavigation,
15 getActionFromState,
16 getStateFromPath,
17 type NavigationAction,
18 type NavigationState,
19} from "@react-navigation/native";
20import { pipe } from "effect/Function";
21
22import { RegistryContext } from "@effect/atom-react";
23import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages";
24import { ThreadArrangementHost } from "./features/threads/ThreadArrangementSheet";
25import * as Dialog from "./components/ui/Dialog";
26import { CloudAuthProvider } from "./features/cloud/CloudAuthProvider";
27import { prepareNativeShowcaseCapture } from "./features/showcase/nativeShowcaseScene";
28import { installShowcaseCapture } from "./features/showcase/ShowcaseCaptureCoordinator";
29import {
30 presentPendingIncomingShare,
31 pruneIncomingShareImports,
32 startIncomingShareInbox,
33} from "./features/sharing/IncomingShareProvider";
34import {
35 installAgentNotificationNavigation,
36 setAgentNotificationPathname,
37} from "./features/agent-awareness/notificationNavigation";
38import {
39 installAppShortcuts,
40 recordAppShortcutNavigationState,
41} from "./features/shortcuts/appShortcutsRuntime";
42import {
43 AppearancePreferencesProvider,
44 installAppearanceRuntime,
45 useAppearancePreferences,
46} from "./features/settings/appearance/AppearancePreferencesProvider";
47import { RootStack, rootPathnameFromState, workspaceLocationFromState } from "./Stack";
48import { appAtomRegistry } from "./state/atom-registry";
49import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
50import { environmentThreadShells } from "./state/threads";
51import { environmentShell } from "./state/shell";
52import { OverlayPortalHost } from "./components/ui/OverlayPortal";
53import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
54import { hasCloudPublicConfig } from "./features/cloud/publicConfig";
55import { takeScheduledTaskNewLink } from "./features/settings/scheduled-task-editor-state";
56import {
57 pairingFormLinkAction,
58 startDevAutoConnect,
59 withoutPairingTarget,
60} from "./features/connection/devAutoConnect";
61import {
62 resolvePendingNewTaskPromptLink,
63 takeNewTaskPromptLink,
64} from "./features/prompt-links/newTaskPromptLink";
65import {
66 resolvePendingThreadPromptLink,
67 takeThreadPromptLink,
68} from "./features/prompt-links/threadPromptLink";
69import {
70 disposeOnFoundationReplace,
71 type FoundationHotModule,
72} from "./lib/foundation-fast-refresh";
73import { useMobileNavigationTheme } from "./lib/useMobileNavigationTheme";
74import { installUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarningGc";
75import { SubscriptionUsageCoordinator } from "./widgets/SubscriptionUsageCoordinator";
76import { installSubscriptionUsageRefresh } from "./widgets/useSubscriptionUsage";
77import { GlobalVoiceInputControl } from "./features/voice-input/GlobalVoiceInputControl";
78import { restoreSettingsLinkState } from "./features/settings/settings-navigation";
79import {
80 hydrateWorkspaceSelections,
81 launchWorkspaceURL,
82 recordWorkspaceSelections,
83 settingsLinkPage,
84} from "./features/workspace/workspace-selection-store";
85import { workspaceDetailLinkAction } from "./features/workspace/workspace-detail-selection";
86import { NativeLayoutMetricsProvider } from "./native/native-layout-metrics";
87import {
88 attachRootNavigation,
89 flushRootNavigationQueue,
90 linkToRootPath,
91 rootNavigationRef,
92} from "./lib/rootNavigation";
93import { pruneNewTaskFlowStates } from "./features/threads/new-task-flow-state";
94import { pruneNewTaskBranchCheckouts } from "./features/threads/NewTaskDraftRouteScreen";
95import { installAppUpdateChecks } from "./features/updates/app-updates";
96
97import "../global.css";
98
99if (process.env.EXPO_PUBLIC_SHOWCASE === "1") {
100 prepareNativeShowcaseCapture();
101}
102
103void SplashScreen.preventAutoHideAsync().catch(() => {});
104void hydrateWorkspaceSelections();
105
106const linkingConfig = {
107 initialRouteName: "Home",
108 screens: createPathConfigForStaticNavigation(RootStack, { initialRouteName: "Home" }) ?? {},
109} as const;
110
111function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) {
112 return pipe(
113 getStateFromPath(path, options),
114 (state) => restoreDraftLinkState(state, path),
115 (state) => restoreSettingsLinkState(state, settingsLinkPage()),
116 takeThreadPromptLink,
117 takeNewTaskPromptLink,
118 withoutPairingTarget,
119 startDevAutoConnect,
120 takeScheduledTaskNewLink,
121 );
122}
123
124const appLinking = {
125 prefixes: [Linking.createURL("/"), "t3code://", "t3code-dev://", "t3code-preview://"],
126 config: { initialRouteName: "Home" },
127 filter: (url: string) => shouldHandleAppLink(url, hasCloudPublicConfig()),
128 getInitialURL: async () => (await Linking.getInitialURL()) ?? (await launchWorkspaceURL()),
129 getActionFromState: (
130 state: Parameters<typeof getActionFromState>[0],
131 options: Parameters<typeof getActionFromState>[1],
132 ): ReturnType<typeof getActionFromState> =>
133 // Our router handles the selection; React Navigation's type only lists its own actions.
134 (workspaceDetailLinkAction(state) as ReturnType<typeof getActionFromState>) ??
135 pairingFormLinkAction(
136 state,
137 rootNavigationRef.isReady() ? rootNavigationRef.getRootState() : undefined,
138 ) ??
139 getActionFromState(state, options),
140 getStateFromPath: getAppStateFromPath,
141};
142
143function appLinkAction(href: string): NavigationAction {
144 const state = getAppStateFromPath(href, linkingConfig);
145 if (!state) throw new Error(`Failed to parse the href to a navigation state (${href}).`);
146 return appLinking.getActionFromState(state, linkingConfig) ?? CommonActions.reset(state);
147}
148
149function linkToAppPath(href: string): void {
150 linkToRootPath(href, appLinkAction);
151}
152
153const Navigation = createStaticNavigation(RootStack);
154
155function handleNavigationStateChange(state: NavigationState | undefined): void {
156 recordWorkspaceSelections(state);
157 presentPendingIncomingShare();
158 resolvePendingNewTaskPromptLink(state);
159 resolvePendingThreadPromptLink(state);
160 if (!state) return;
161 setAgentNotificationPathname(workspaceLocationFromState(state).pathname);
162 recordAppShortcutNavigationState(state);
163 pruneNewTaskFlowStates(state);
164 pruneNewTaskBranchCheckouts(state);
165 pruneIncomingShareImports(state);
166}
167
168function handleNavigationReady(): void {
169 handleNavigationStateChange(rootNavigationRef.getRootState());
170 flushRootNavigationQueue();
171}
172
173declare const module: { readonly hot?: FoundationHotModule } | undefined;
174
175const appServiceDisposers = [
176 installUiRuntimeMemoryWarningGc(),
177 subscribeComposerDraftModelReconciliation({
178 registry: appAtomRegistry,
179 threadShellAtom: environmentThreadShells.threadShellAtom,
180 threadRefsAtom: environmentThreadShells.threadRefsAtom,
181 shellStateAtom: environmentShell.stateAtom,
182 }),
183 installAppearanceRuntime(),
184 installSubscriptionUsageRefresh(),
185 installPermissionUpdateNotice(),
186 installThreadOutboxDelivery(),
187 installComposerAttachmentUploads(),
188 startIncomingShareInbox(),
189 installAgentNotificationNavigation(linkToAppPath),
190 installAppShortcuts(linkToAppPath),
191 installAppUpdateChecks(),
192 installShowcaseCapture(rootPathnameFromState),
193];
194disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.hot, () => {
195 for (const dispose of appServiceDisposers) dispose();
196});
197
198function SplashScreenCoordinator() {
199 const { isReady } = useAppearancePreferences();
200
201 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hides the native splash screen once appearance preferences have loaded
202 useEffect(() => {
203 if (isReady) void SplashScreen.hide();
204 }, [isReady]);
205
206 return null;
207}
208
209export default function App() {
210 return (
211 <RegistryContext.Provider value={appAtomRegistry}>
212 <CloudAuthProvider>
213 <AppearancePreferencesProvider>
214 <AppContent />
215 </AppearancePreferencesProvider>
216 </CloudAuthProvider>
217 </RegistryContext.Provider>
218 );
219}
220
221function AppContent() {
222 const { themeAppearance } = useAppearancePreferences();
223 const navigationTheme = useMobileNavigationTheme();
224
225 return (
226 <>
227 <SplashScreenCoordinator />
228 <SubscriptionUsageCoordinator />
229 <GestureHandlerRootView className="flex-1">
230 <KeyboardProvider statusBarTranslucent>
231 <SafeAreaProvider>
232 <StatusBar barStyle={themeAppearance === "dark" ? "light-content" : "dark-content"} />
233 {/* native-stack needs the theme's dark flag to select dark native headers. */}
234 <GlobalVoiceInputControl>
235 <NativeLayoutMetricsProvider>
236 <Navigation
237 ref={attachRootNavigation}
238 linking={appLinking}
239 onReady={handleNavigationReady}
240 theme={navigationTheme}
241 onStateChange={handleNavigationStateChange}
242 />
243 <Dialog.Host />
244 <ThreadArrangementHost />
245 <ProjectChipIconRasterizer />
246 </NativeLayoutMetricsProvider>
247 </GlobalVoiceInputControl>
248 <OverlayPortalHost />
249 </SafeAreaProvider>
250 </KeyboardProvider>
251 </GestureHandlerRootView>
252 </>
253 );
254}