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