Open History to see the last commit
1import {
2 createPathConfigForStaticNavigation,
3 getPathFromState,
4 NavigationState,
5 StackActions,
6 useNavigation,
7} from "@react-navigation/native";
8import {
9 createNativeStackNavigator,
10 createNativeStackScreen,
11 type NativeStackNavigationOptions,
12} from "@react-navigation/native-stack";
13import { useEffect, useRef } from "react";
14import { DynamicColorIOS, Platform, Pressable, ScrollView, StyleSheet } from "react-native";
15import { useResolveClassNames } from "uniwind";
16
17import { AppText as Text } from "./components/AppText";
18import { ArchivedThreadsRouteScreen } from "./features/archive/ArchivedThreadsRouteScreen";
19import { useAgentNotificationNavigation } from "./features/agent-awareness/notificationNavigation";
20import { ClerkSettingsSheetDetentProvider } from "./features/cloud/ClerkSettingsSheetDetent";
21import { ConnectOnboardingRouteScreen } from "./features/cloud/ConnectOnboardingRouteScreen";
22import { useConnectOnboardingNavigation } from "./features/cloud/connectOnboardingNavigation";
23import { ThreadFilesTreeScreen, ThreadFileScreen } from "./features/files/ThreadFilesRouteScreen";
24import { AdaptiveWorkspaceLayout } from "./features/layout/AdaptiveWorkspaceLayout";
25import { HardwareKeyboardCommandProvider } from "./features/keyboard/HardwareKeyboardCommandProvider";
26import { ReviewCommentComposerSheet } from "./features/review/ReviewCommentComposerSheet";
27import { ReviewSheet } from "./features/review/ReviewSheet";
28import { ThreadTerminalRouteScreen } from "./features/terminal/ThreadTerminalRouteScreen";
29import { GitBranchesSheet } from "./features/threads/git/GitBranchesSheet";
30import { GitCommitSheet } from "./features/threads/git/GitCommitSheet";
31import { GitConfirmSheet } from "./features/threads/git/GitConfirmSheet";
32import { GitOverviewSheet } from "./features/threads/git/GitOverviewSheet";
33import { ThreadRouteScreen } from "./features/threads/ThreadRouteScreen";
34import { ConnectionsRouteScreen } from "./features/connection/ConnectionsRouteScreen";
35import { ConnectionsNewRouteScreen } from "./features/connection/ConnectionsNewRouteScreen";
36import { HomeRouteScreen } from "./features/home/HomeRouteScreen";
37import { AddProjectDestinationRoute } from "./features/projects/AddProjectDestinationRoute";
38import { AddProjectLocalRoute } from "./features/projects/AddProjectLocalRoute";
39import { AddProjectRepositoryRoute } from "./features/projects/AddProjectRepositoryRoute";
40import { AddProjectSourceRoute } from "./features/projects/AddProjectSourceRoute";
41import { NewTaskDraftRouteScreen } from "./features/threads/NewTaskDraftRouteScreen";
42import { NewTaskFlowProvider } from "./features/threads/new-task-flow-provider";
43import { NewTaskRouteScreen } from "./features/threads/NewTaskRouteScreen";
44import { SettingsAppearanceRouteScreen } from "./features/settings/SettingsAppearanceRouteScreen";
45import { SettingsClientStorageRouteScreen } from "./features/settings/SettingsClientStorageRouteScreen";
46import { SettingsAuthRouteScreen } from "./features/settings/SettingsAuthRouteScreen";
47import { SettingsEnvironmentsRouteScreen } from "./features/settings/SettingsEnvironmentsRouteScreen";
48import { SettingsLegalRouteScreen } from "./features/settings/SettingsLegalRouteScreen";
49import { SettingsRouteScreen } from "./features/settings/SettingsRouteScreen";
50import { SettingsWaitlistRouteScreen } from "./features/settings/SettingsWaitlistRouteScreen";
51import { ShowcaseCaptureCoordinator } from "./features/showcase/ShowcaseCaptureCoordinator";
52import {
53 SettingsLegalDocumentCloseHeaderButton,
54 SettingsLegalDocumentExternalHeaderButton,
55} from "./features/settings/components/SettingsLegalDocumentRouteScreen";
56import { useAppShortcuts } from "./features/shortcuts/useAppShortcuts";
57import { useIncomingShare } from "./features/sharing/IncomingShareProvider";
58import {
59 EMPTY_INCOMING_SHARE_PRESENTATION_STATE,
60 transitionIncomingSharePresentation,
61} from "./features/sharing/incoming-share-presentation";
62import { nativeHeaderScrollEdgeEffects } from "./native/StackHeader";
63import { useThreadOutboxDrain } from "./state/use-thread-outbox-drain";
64
65const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
66
67// Matches --color-sheet in global.css (light/dark). DynamicColorIOS lets the header
68// background stay STATIC config while still adapting to appearance changes.
69const SHEET_BACKGROUND_COLOR =
70 Platform.OS === "ios"
71 ? DynamicColorIOS({ light: "rgba(242, 242, 247, 0.98)", dark: "rgba(14, 14, 14, 0.98)" })
72 : undefined;
73
74type AppScreenOptions = NativeStackNavigationOptions & {
75 readonly unstable_navigationItemStyle?: "editor";
76};
77
78// Shared header presets. Screens only override genuinely dynamic values (titles,
79// subtitles, toolbar items, search callbacks) via NativeStackScreenOptions.
80//
81// GLASS: transparent header over the screen's primary scroll view, with the iOS 26
82// scroll-edge blur sampling the content (Home, Thread, Files tree, settings sheet).
83const GLASS_HEADER_OPTIONS: AppScreenOptions = {
84 headerBackButtonDisplayMode: "minimal",
85 headerBackTitle: "",
86 headerLargeTitle: false,
87 headerShadowVisible: false,
88 headerShown: true,
89 headerStyle: Platform.OS === "ios" ? { backgroundColor: "transparent" } : undefined,
90 headerTitleStyle: { fontSize: 18, fontWeight: "800" },
91 headerTransparent: Platform.OS === "ios",
92 scrollEdgeEffects: Platform.OS === "ios" ? HEADER_SCROLL_EDGE_EFFECTS : undefined,
93 unstable_navigationItemStyle: Platform.OS === "ios" ? "editor" : undefined,
94};
95
96// SOLID: opaque sheet-colored header for surfaces whose content scrolls internally
97// (file viewer, terminal, review) — there is nothing for glass to sample there.
98const SOLID_HEADER_OPTIONS: AppScreenOptions = {
99 headerBackButtonDisplayMode: "minimal",
100 headerBackTitle: "",
101 headerLargeTitle: false,
102 headerShadowVisible: false,
103 headerShown: true,
104 headerStyle:
105 SHEET_BACKGROUND_COLOR !== undefined
106 ? // native-stack types this as `string`, but the native side accepts any
107 // ColorValue including DynamicColorIOS.
108 { backgroundColor: SHEET_BACKGROUND_COLOR as unknown as string }
109 : undefined,
110 headerTitleStyle: { fontSize: 18, fontWeight: "800" },
111 headerTransparent: false,
112 unstable_navigationItemStyle: Platform.OS === "ios" ? "editor" : undefined,
113};
114
115// Solid header variant for screens inside sheets (centered title, no editor style).
116const SHEET_SOLID_HEADER_OPTIONS: AppScreenOptions = {
117 ...SOLID_HEADER_OPTIONS,
118 unstable_navigationItemStyle: undefined,
119};
120
121const LEGAL_DOCUMENT_HEADER_OPTIONS: AppScreenOptions = {
122 ...SHEET_SOLID_HEADER_OPTIONS,
123 headerBackVisible: false,
124 headerLeft: SettingsLegalDocumentCloseHeaderButton,
125 headerRight: () => <SettingsLegalDocumentExternalHeaderButton />,
126 presentation: "fullScreenModal",
127};
128
129const SettingsSheetStack = createNativeStackNavigator({
130 initialRouteName: "Settings",
131 screenOptions: {
132 ...GLASS_HEADER_OPTIONS,
133 // Sheets read better with the iOS-default centered title (no editor style).
134 unstable_navigationItemStyle: undefined,
135 },
136 screens: {
137 Settings: createNativeStackScreen({
138 screen: SettingsRouteScreen,
139 linking: "",
140 options: {
141 title: "Settings",
142 },
143 }),
144 SettingsEnvironments: createNativeStackScreen({
145 screen: SettingsEnvironmentsRouteScreen,
146 linking: "environments",
147 options: {
148 title: "Environments",
149 },
150 }),
151 SettingsEnvironmentNew: createNativeStackScreen({
152 screen: ConnectionsNewRouteScreen,
153 linking: "environment-new",
154 options: {
155 title: "Add Environment",
156 },
157 }),
158 SettingsArchive: createNativeStackScreen({
159 screen: ArchivedThreadsRouteScreen,
160 linking: "archive",
161 options: {
162 title: "Archived Threads",
163 },
164 }),
165 SettingsAppearance: createNativeStackScreen({
166 screen: SettingsAppearanceRouteScreen,
167 linking: "appearance",
168 options: {
169 title: "Appearance",
170 },
171 }),
172 SettingsClientStorage: createNativeStackScreen({
173 screen: SettingsClientStorageRouteScreen,
174 linking: "client-storage",
175 options: {
176 title: "Client Storage",
177 },
178 }),
179 SettingsAuth: createNativeStackScreen({
180 screen: SettingsAuthRouteScreen,
181 linking: "auth",
182 options: {
183 title: "Sign in",
184 },
185 }),
186 SettingsWaitlist: createNativeStackScreen({
187 screen: SettingsWaitlistRouteScreen,
188 linking: "waitlist",
189 options: {
190 title: "Join the waitlist",
191 },
192 }),
193 },
194});
195
196// Thread routes live FLAT in the root stack (not in a nested navigator). A nested
197// stack means a second UINavigationController with its own UINavigationBar, which
198// breaks iOS 26's shared-header morphing between Home and Thread (each pair inside
199// one bar morphs; across two bars the whole screen slides). Flat linking paths keep
200// the same deep-link URLs the nested config produced.
201const THREAD_LINKING_PREFIX = "threads/:environmentId/:threadId";
202
203// New-task / add-project flow: nested navigator inside the formSheet (Settings-sheet
204// pattern — a plain formSheet screen cannot render a stack header; the header and
205// in-sheet pushes come from this nested stack).
206const NewTaskSheetStack = createNativeStackNavigator({
207 initialRouteName: "NewTask",
208 screenOptions: {
209 ...GLASS_HEADER_OPTIONS,
210 // Sheets read better with the iOS-default centered title (no editor style).
211 unstable_navigationItemStyle: undefined,
212 },
213 screens: {
214 NewTask: createNativeStackScreen({
215 screen: NewTaskRouteScreen,
216 linking: "",
217 options: {
218 title: "Choose project",
219 },
220 }),
221 NewTaskDraft: createNativeStackScreen({
222 screen: NewTaskDraftRouteScreen,
223 linking: "draft",
224 // The draft composer has no scroll view for glass to sample; a solid
225 // header also lays the content out below the bar (no manual inset).
226 options: SHEET_SOLID_HEADER_OPTIONS,
227 }),
228 AddProject: createNativeStackScreen({
229 screen: AddProjectSourceRoute,
230 linking: "add-project",
231 options: {
232 title: "Add Project",
233 },
234 }),
235 AddProjectRepository: createNativeStackScreen({
236 screen: AddProjectRepositoryRoute,
237 linking: "add-project/repository",
238 }),
239 AddProjectDestination: createNativeStackScreen({
240 screen: AddProjectDestinationRoute,
241 linking: "add-project/destination",
242 }),
243 AddProjectLocal: createNativeStackScreen({
244 screen: AddProjectLocalRoute,
245 linking: "add-project/local",
246 }),
247 },
248});
249
250// Routes presented as sheets/overlays ON TOP of the workspace. They must not
251// influence the adaptive workspace layout: opening Settings over Home should
252// not flip the sidebar in or change the active thread.
253const WORKSPACE_OVERLAY_ROUTES = new Set([
254 "ConnectOnboarding",
255 "Connections",
256 "ConnectionsNew",
257 "GitBranches",
258 "GitCommit",
259 "GitConfirm",
260 "GitOverview",
261 "NewTaskSheet",
262 "SettingsLegal",
263 "SettingsSheet",
264 "ThreadReviewComment",
265]);
266
267/**
268 * Pathname of the topmost NON-overlay route — the screen the workspace is
269 * actually "on", regardless of any sheets floating above it.
270 */
271function workspacePathFromState(state: NavigationState): string {
272 const routes = state.routes.filter((route) => !WORKSPACE_OVERLAY_ROUTES.has(route.name));
273 const effectiveState =
274 routes.length > 0 && routes.length !== state.routes.length
275 ? ({ ...state, routes, index: routes.length - 1 } as NavigationState)
276 : state;
277 const path = getPathFromState(effectiveState, navigationPathConfig);
278 return path.startsWith("/") ? path : `/${path}`;
279}
280
281function RootStackLayout(props: {
282 readonly children: React.ReactNode;
283 readonly state: NavigationState;
284}) {
285 const navigation = useNavigation();
286 const { pendingShare } = useIncomingShare();
287 const sharePresentationRef = useRef(EMPTY_INCOMING_SHARE_PRESENTATION_STATE);
288 useAgentNotificationNavigation();
289 useThreadOutboxDrain();
290 // Presents the T3 Connect onboarding sheet after an in-session sign-in.
291 useConnectOnboardingNavigation();
292 // Launcher app shortcuts: routes shortcut taps and tracks opened threads.
293 useAppShortcuts(props.state);
294 useEffect(() => {
295 const topRouteName = props.state.routes[props.state.index]?.name;
296 const transition = transitionIncomingSharePresentation(sharePresentationRef.current, {
297 isShareSheetPresented: topRouteName === "NewTaskSheet",
298 pendingShareId: pendingShare?.id ?? null,
299 });
300 sharePresentationRef.current = transition.state;
301 if (!transition.shareIdToPresent) {
302 return;
303 }
304 navigation.navigate("NewTaskSheet", {
305 screen: "NewTask",
306 params: { incomingShareId: transition.shareIdToPresent },
307 });
308 }, [navigation, pendingShare, props.state]);
309 // Full pathname (sheets included) for keyboard-command scoping; the
310 // workspace layout only reacts to the underlying non-overlay route.
311 const path = getPathFromState(props.state, navigationPathConfig);
312 const pathname = path.startsWith("/") ? path : `/${path}`;
313 const workspacePathname = workspacePathFromState(props.state);
314
315 return (
316 <HardwareKeyboardCommandProvider pathname={pathname}>
317 <ShowcaseCaptureCoordinator pathname={pathname} />
318 <ClerkSettingsSheetDetentProvider initiallyExpanded={false}>
319 <AdaptiveWorkspaceLayout pathname={workspacePathname}>
320 {props.children}
321 </AdaptiveWorkspaceLayout>
322 </ClerkSettingsSheetDetentProvider>
323 </HardwareKeyboardCommandProvider>
324 );
325}
326
327function NotFoundScreen() {
328 const navigation = useNavigation();
329 const screenBgStyle = StyleSheet.flatten(useResolveClassNames("bg-screen"));
330 const primaryBgStyle = StyleSheet.flatten(useResolveClassNames("bg-primary"));
331 const returnHomeButtonStyle = StyleSheet.flatten([
332 {
333 borderRadius: 999,
334 paddingHorizontal: 20,
335 paddingVertical: 14,
336 },
337 primaryBgStyle,
338 ]);
339
340 return (
341 <ScrollView
342 contentInsetAdjustmentBehavior="automatic"
343 contentContainerStyle={{
344 flexGrow: 1,
345 alignItems: "center",
346 justifyContent: "center",
347 gap: 16,
348 paddingHorizontal: 24,
349 paddingVertical: 32,
350 }}
351 style={[{ flex: 1 }, screenBgStyle]}
352 >
353 <Text className="text-3xl font-t3-bold text-foreground" selectable>
354 Route not found
355 </Text>
356 <Pressable
357 style={returnHomeButtonStyle}
358 onPress={() => navigation.dispatch(StackActions.replace("Home"))}
359 >
360 <Text className="text-base font-t3-bold text-primary-foreground">Return home</Text>
361 </Pressable>
362 </ScrollView>
363 );
364}
365
366export const RootStack = createNativeStackNavigator({
367 initialRouteName: "Home",
368 layout: RootStackLayout,
369 screenOptions: {
370 headerShown: false,
371 },
372 screens: {
373 Home: createNativeStackScreen({
374 screen: HomeRouteScreen,
375 linking: "",
376 options: {
377 ...GLASS_HEADER_OPTIONS,
378 contentStyle: { backgroundColor: "transparent" },
379 headerBackVisible: false,
380 title: "Threads",
381 },
382 }),
383 Thread: createNativeStackScreen({
384 screen: ThreadRouteScreen,
385 linking: THREAD_LINKING_PREFIX,
386 options: GLASS_HEADER_OPTIONS,
387 }),
388 ThreadTerminal: createNativeStackScreen({
389 screen: ThreadTerminalRouteScreen,
390 linking: `${THREAD_LINKING_PREFIX}/terminal`,
391 options: SOLID_HEADER_OPTIONS,
392 }),
393 ThreadReview: createNativeStackScreen({
394 screen: ReviewSheet,
395 linking: `${THREAD_LINKING_PREFIX}/review`,
396 options: SOLID_HEADER_OPTIONS,
397 }),
398 ThreadReviewComment: createNativeStackScreen({
399 screen: ReviewCommentComposerSheet,
400 linking: `${THREAD_LINKING_PREFIX}/review-comment`,
401 options: {
402 // Android cannot host the keyboard-driven comment composer inside a
403 // formSheet; use a full-screen modal there instead.
404 presentation: Platform.OS === "android" ? "fullScreenModal" : "formSheet",
405 sheetAllowedDetents: Platform.OS === "android" ? undefined : [0.55, 0.92],
406 sheetGrabberVisible: Platform.OS !== "android",
407 },
408 }),
409 ThreadFiles: createNativeStackScreen({
410 screen: ThreadFilesTreeScreen,
411 linking: `${THREAD_LINKING_PREFIX}/files`,
412 options: {
413 ...GLASS_HEADER_OPTIONS,
414 contentStyle:
415 SHEET_BACKGROUND_COLOR !== undefined
416 ? { backgroundColor: SHEET_BACKGROUND_COLOR }
417 : undefined,
418 title: "Files",
419 },
420 }),
421 ThreadFile: createNativeStackScreen({
422 screen: ThreadFileScreen,
423 linking: `${THREAD_LINKING_PREFIX}/files/:path*`,
424 options: SOLID_HEADER_OPTIONS,
425 }),
426 GitOverview: createNativeStackScreen({
427 screen: GitOverviewSheet,
428 linking: `${THREAD_LINKING_PREFIX}/git`,
429 options: {
430 presentation: "formSheet",
431 sheetAllowedDetents: [0.55, 0.92],
432 sheetGrabberVisible: true,
433 },
434 }),
435 GitCommit: createNativeStackScreen({
436 screen: GitCommitSheet,
437 linking: `${THREAD_LINKING_PREFIX}/git/commit`,
438 options: {
439 presentation: "formSheet",
440 sheetAllowedDetents: [0.55, 0.92],
441 sheetGrabberVisible: true,
442 },
443 }),
444 GitBranches: createNativeStackScreen({
445 screen: GitBranchesSheet,
446 linking: `${THREAD_LINKING_PREFIX}/git/branches`,
447 options: {
448 presentation: "formSheet",
449 sheetAllowedDetents: [0.55, 0.92],
450 sheetGrabberVisible: true,
451 },
452 }),
453 GitConfirm: createNativeStackScreen({
454 screen: GitConfirmSheet,
455 linking: `${THREAD_LINKING_PREFIX}/git-confirm`,
456 options: {
457 presentation: "formSheet",
458 sheetAllowedDetents: [0.45, 0.7],
459 sheetGrabberVisible: true,
460 },
461 }),
462 SettingsSheet: createNativeStackScreen({
463 screen: SettingsSheetStack,
464 linking: "settings",
465 options: {
466 gestureEnabled: true,
467 headerShown: false,
468 // Android pushes settings as a regular full page with an in-screen
469 // back header; iOS keeps the detented form sheet.
470 ...(Platform.OS === "android"
471 ? { presentation: "card" as const }
472 : {
473 presentation: "formSheet" as const,
474 sheetAllowedDetents: [0.7, 0.92],
475 sheetGrabberVisible: true,
476 }),
477 },
478 }),
479 SettingsLegal: createNativeStackScreen({
480 screen: SettingsLegalRouteScreen,
481 linking: "settings/legal",
482 options: {
483 ...LEGAL_DOCUMENT_HEADER_OPTIONS,
484 title: "Legal",
485 },
486 }),
487 ConnectOnboarding: createNativeStackScreen({
488 screen: ConnectOnboardingRouteScreen,
489 linking: "connect-onboarding",
490 options: {
491 // A root-level Android formSheet does not host the native stack bar;
492 // the route renders an embedded AndroidSheetHeader instead.
493 ...(Platform.OS === "android" ? { headerShown: false } : SHEET_SOLID_HEADER_OPTIONS),
494 title: "Set up T3 Connect",
495 gestureEnabled: true,
496 presentation: "formSheet",
497 sheetAllowedDetents: [0.6, 0.95],
498 sheetGrabberVisible: true,
499 },
500 }),
501 Connections: createNativeStackScreen({
502 screen: ConnectionsRouteScreen,
503 linking: "connections",
504 options: {
505 title: "Environments",
506 // Android: full page; the screen renders its own AndroidScreenHeader,
507 // so the native bar stays hidden. iOS keeps the sheet.
508 ...(Platform.OS === "android"
509 ? { presentation: "card" as const, headerShown: false }
510 : {
511 presentation: "formSheet" as const,
512 sheetAllowedDetents: [0.55, 0.7],
513 sheetGrabberVisible: true,
514 }),
515 },
516 }),
517 ConnectionsNew: createNativeStackScreen({
518 screen: ConnectionsNewRouteScreen,
519 linking: "connections/new",
520 options: {
521 presentation: "formSheet",
522 sheetAllowedDetents: [0.55, 0.7],
523 sheetGrabberVisible: true,
524 },
525 }),
526 NewTaskSheet: createNativeStackScreen({
527 screen: NewTaskSheetStack,
528 linking: "new",
529 // The whole new-task flow (choose project → draft → add project) shares
530 // draft state via NewTaskFlowProvider. The expo-router era mounted it in
531 // app/new/_layout.tsx; this layout wrapper is the native-stack equivalent.
532 layout: ({ children }) => <NewTaskFlowProvider>{children}</NewTaskFlowProvider>,
533 options: {
534 gestureEnabled: true,
535 headerShown: false,
536 // Android pushes the flow as a regular full page — the draft should
537 // read like a thread that just doesn't exist yet; iOS keeps the sheet.
538 ...(Platform.OS === "android"
539 ? { presentation: "card" as const }
540 : {
541 presentation: "formSheet" as const,
542 sheetAllowedDetents: [0.92],
543 sheetGrabberVisible: true,
544 }),
545 },
546 }),
547 NotFound: createNativeStackScreen({
548 screen: NotFoundScreen,
549 linking: "*",
550 }),
551 },
552});
553type RootStackType = typeof RootStack;
554
555const navigationPathConfig = {
556 screens: createPathConfigForStaticNavigation(RootStack) ?? {},
557};
558
559declare module "@react-navigation/native" {
560 interface RootNavigator extends RootStackType {}
561}