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