Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

App.tsx

221 lines8.3 KB

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";
20​
21import { RegistryContext } from "@effect/atom-react";
22import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages";
23import { ThreadArrangementHost } from "./features/threads/ThreadArrangementSheet";
24import * as Dialog from "./components/ui/Dialog";
25import { CloudAuthProvider } from "./features/cloud/CloudAuthProvider";
26import { prepareNativeShowcaseCapture } from "./features/showcase/nativeShowcaseScene";
27import {
28 presentPendingIncomingShare,
29 startIncomingShareInbox,
30} from "./features/sharing/IncomingShareProvider";
31import {
32 installAgentNotificationNavigation,
33 setAgentNotificationPathname,
34} from "./features/agent-awareness/notificationNavigation";
35import {
36 installAppShortcuts,
37 recordAppShortcutNavigationState,
38} from "./features/shortcuts/appShortcutsRuntime";
39import {
40 AppearancePreferencesProvider,
41 installAppearanceRuntime,
42 useAppearancePreferences,
43} from "./features/settings/appearance/AppearancePreferencesProvider";
44import { RootStack, workspaceLocationFromState } from "./Stack";
45import { appAtomRegistry } from "./state/atom-registry";
46import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
47import { environmentThreadShells } from "./state/threads";
48import { environmentShell } from "./state/shell";
49import { OverlayPortalHost } from "./components/ui/OverlayPortal";
50import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
51import { hasCloudPublicConfig } from "./features/cloud/publicConfig";
52import { takeScheduledTaskNewLink } from "./features/settings/scheduled-task-editor-state";
53import {
54 disposeOnFoundationReplace,
55 type FoundationHotModule,
56} from "./lib/foundation-fast-refresh";
57import { useMobileNavigationTheme } from "./lib/useMobileNavigationTheme";
58import { installUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarningGc";
59import { SubscriptionUsageCoordinator } from "./widgets/SubscriptionUsageCoordinator";
60import { installSubscriptionUsageRefresh } from "./widgets/useSubscriptionUsage";
61import { GlobalVoiceInputControl } from "./features/voice-input/GlobalVoiceInputControl";
62import { restoreSettingsLinkState } from "./features/settings/settings-navigation";
63import {
64 hydrateWorkspaceSelections,
65 launchWorkspaceURL,
66 recordWorkspaceSelections,
67 settingsLinkPage,
68} from "./features/workspace/workspace-selection-store";
69import { workspaceDetailLinkAction } from "./features/workspace/workspace-detail-selection";
70import { NativeLayoutMetricsProvider } from "./native/native-layout-metrics";
71import {
72 attachRootNavigation,
73 flushRootNavigationQueue,
74 linkToRootPath,
75 rootNavigationRef,
76} from "./lib/rootNavigation";
77import { pruneNewTaskBranchCheckouts } from "./features/threads/NewTaskDraftRouteScreen";
78​
79import "../global.css";
80​
81if (process.env.EXPO_PUBLIC_SHOWCASE === "1") {
82 prepareNativeShowcaseCapture();
84​
85void SplashScreen.preventAutoHideAsync().catch(() => {});
86void hydrateWorkspaceSelections();
87​
88const linkingConfig = {
89 initialRouteName: "Home",
90 screens: createPathConfigForStaticNavigation(RootStack, { initialRouteName: "Home" }) ?? {},
91} as const;
92​
93function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) {
94 return takeScheduledTaskNewLink(
95 restoreSettingsLinkState(
96 restoreDraftLinkState(getStateFromPath(path, options), path),
97 settingsLinkPage(),
98 ),
99 );
101​
102const appLinking = {
103 prefixes: [Linking.createURL("/"), "t3code://", "t3code-dev://", "t3code-preview://"],
104 config: { initialRouteName: "Home" },
105 filter: (url: string) => shouldHandleAppLink(url, hasCloudPublicConfig()),
106 getInitialURL: async () => (await Linking.getInitialURL()) ?? (await launchWorkspaceURL()),
107 getActionFromState: (
108 state: Parameters<typeof getActionFromState>[0],
109 options: Parameters<typeof getActionFromState>[1],
110 ): ReturnType<typeof getActionFromState> =>
111 // Our router handles the selection; React Navigation's type only lists its own actions.
112 (workspaceDetailLinkAction(state) as ReturnType<typeof getActionFromState>) ??
113 getActionFromState(state, options),
114 getStateFromPath: getAppStateFromPath,
115};
116​
117function appLinkAction(href: string): NavigationAction {
118 const state = getAppStateFromPath(href, linkingConfig);
119 if (!state) throw new Error(`Failed to parse the href to a navigation state (${href}).`);
120 return appLinking.getActionFromState(state, linkingConfig) ?? CommonActions.reset(state);
122​
123function linkToAppPath(href: string): void {
124 linkToRootPath(href, appLinkAction);
126​
127const Navigation = createStaticNavigation(RootStack);
128​
129function handleNavigationStateChange(state: NavigationState | undefined): void {
130 recordWorkspaceSelections(state);
131 presentPendingIncomingShare();
132 if (!state) return;
133 setAgentNotificationPathname(workspaceLocationFromState(state).pathname);
134 recordAppShortcutNavigationState(state);
135 pruneNewTaskBranchCheckouts(state);
137​
138function handleNavigationReady(): void {
139 handleNavigationStateChange(rootNavigationRef.getRootState());
140 flushRootNavigationQueue();
142​
143declare const module: { readonly hot?: FoundationHotModule } | undefined;
144​
145const appServiceDisposers = [
146 installUiRuntimeMemoryWarningGc(),
147 subscribeComposerDraftModelReconciliation({
148 registry: appAtomRegistry,
149 threadShellAtom: environmentThreadShells.threadShellAtom,
150 threadRefsAtom: environmentThreadShells.threadRefsAtom,
151 shellStateAtom: environmentShell.stateAtom,
152 }),
153 installAppearanceRuntime(),
154 installSubscriptionUsageRefresh(),
155 installPermissionUpdateNotice(),
156 installThreadOutboxDelivery(),
157 installComposerAttachmentUploads(),
158 startIncomingShareInbox(),
159 installAgentNotificationNavigation(linkToAppPath),
160 installAppShortcuts(linkToAppPath),
161];
162disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.hot, () => {
163 for (const dispose of appServiceDisposers) dispose();
164});
165​
166function SplashScreenCoordinator() {
167 const { isReady } = useAppearancePreferences();
168​
169 useEffect(() => {
170 if (isReady) void SplashScreen.hide();
171 }, [isReady]);
172​
173 return null;
175​
176export default function App() {
177 return (
178 <RegistryContext.Provider value={appAtomRegistry}>
179 <CloudAuthProvider>
180 <AppearancePreferencesProvider>
181 <AppContent />
182 </AppearancePreferencesProvider>
183 </CloudAuthProvider>
184 </RegistryContext.Provider>
185 );
187​
188function AppContent() {
189 const { themeAppearance } = useAppearancePreferences();
190 const navigationTheme = useMobileNavigationTheme();
191​
192 return (
193 <>
194 <SplashScreenCoordinator />
195 <SubscriptionUsageCoordinator />
196 <GestureHandlerRootView className="flex-1">
197 <KeyboardProvider statusBarTranslucent>
198 <SafeAreaProvider>
199 <StatusBar barStyle={themeAppearance === "dark" ? "light-content" : "dark-content"} />
200 {/* native-stack needs the theme's dark flag to select dark native headers. */}
201 <GlobalVoiceInputControl>
202 <NativeLayoutMetricsProvider>
203 <Navigation
204 ref={attachRootNavigation}
205 linking={appLinking}
206 onReady={handleNavigationReady}
207 theme={navigationTheme}
208 onStateChange={handleNavigationStateChange}
209 />
210 <Dialog.Host />
211 <ThreadArrangementHost />
212 <ProjectChipIconRasterizer />
213 </NativeLayoutMetricsProvider>
214 </GlobalVoiceInputControl>
215 <OverlayPortalHost />
216 </SafeAreaProvider>
217 </KeyboardProvider>
218 </GestureHandlerRootView>
219 </>
220 );