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