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