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