Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Stack.tsx

567 lines21.1 KB

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 { SettingsWaitlistRouteScreen } from "./features/settings/SettingsWaitlistRouteScreen";
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 screen: SettingsWaitlistRouteScreen,
194 linking: "waitlist",
195 options: {
196 title: "Join the waitlist",
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}`;
286​
287function RootStackLayout(props: {
288 readonly children: React.ReactNode;
289 readonly state: NavigationState;
290}) {
291 const navigation = useNavigation();
292 const { pendingShare } = useIncomingShare();
293 const sharePresentationRef = useRef(EMPTY_INCOMING_SHARE_PRESENTATION_STATE);
294 useAgentNotificationNavigation();
295 useThreadOutboxDrain();
296 // Presents the T3 Connect onboarding sheet after an in-session sign-in.
297 useConnectOnboardingNavigation();
298 // Launcher app shortcuts: routes shortcut taps and tracks opened threads.
299 useAppShortcuts(props.state);
300 useEffect(() => {
301 const topRouteName = props.state.routes[props.state.index]?.name;
302 const transition = transitionIncomingSharePresentation(sharePresentationRef.current, {
303 isShareSheetPresented: topRouteName === "NewTaskSheet",
304 pendingShareId: pendingShare?.id ?? null,
305 });
306 sharePresentationRef.current = transition.state;
307 if (!transition.shareIdToPresent) {
308 return;
309 }
310 navigation.navigate("NewTaskSheet", {
311 screen: "NewTask",
312 params: { incomingShareId: transition.shareIdToPresent },
313 });
314 }, [navigation, pendingShare, props.state]);
315 // Full pathname (sheets included) for keyboard-command scoping; the
316 // workspace layout only reacts to the underlying non-overlay route.
317 const path = getPathFromState(props.state, navigationPathConfig);
318 const pathname = path.startsWith("/") ? path : `/${path}`;
319 const workspacePathname = workspacePathFromState(props.state);
320​
321 return (
322 <HardwareKeyboardCommandProvider pathname={pathname}>
323 <ShowcaseCaptureCoordinator pathname={pathname} />
324 <ClerkSettingsSheetDetentProvider initiallyExpanded={false}>
325 <AdaptiveWorkspaceLayout pathname={workspacePathname}>
326 {props.children}
327 </AdaptiveWorkspaceLayout>
328 </ClerkSettingsSheetDetentProvider>
329 </HardwareKeyboardCommandProvider>
330 );
332​
333function NotFoundScreen() {
334 const navigation = useNavigation();
335 const screenBgStyle = StyleSheet.flatten(useResolveClassNames("bg-screen"));
336 const primaryBgStyle = StyleSheet.flatten(useResolveClassNames("bg-primary"));
337 const returnHomeButtonStyle = StyleSheet.flatten([
338 {
339 borderRadius: 999,
340 paddingHorizontal: 20,
341 paddingVertical: 14,
342 },
343 primaryBgStyle,
344 ]);
345​
346 return (
347 <ScrollView
348 contentInsetAdjustmentBehavior="automatic"
349 contentContainerStyle={{
350 flexGrow: 1,
351 alignItems: "center",
352 justifyContent: "center",
353 gap: 16,
354 paddingHorizontal: 24,
355 paddingVertical: 32,
356 }}
357 style={[{ flex: 1 }, screenBgStyle]}
358 >
359 <Text className="text-3xl font-t3-bold text-foreground" selectable>
360 Route not found
361 </Text>
362 <Pressable
363 style={returnHomeButtonStyle}
364 onPress={() => navigation.dispatch(StackActions.replace("Home"))}
365 >
366 <Text className="text-base font-t3-bold text-primary-foreground">Return home</Text>
367 </Pressable>
368 </ScrollView>
369 );
371​
372export const RootStack = createNativeStackNavigator({
373 initialRouteName: "Home",
374 layout: RootStackLayout,
375 screenOptions: {
376 headerShown: false,
377 },
378 screens: {
379 Home: createNativeStackScreen({
380 screen: HomeRouteScreen,
381 linking: "",
382 options: {
383 ...GLASS_HEADER_OPTIONS,
384 contentStyle: { backgroundColor: "transparent" },
385 headerBackVisible: false,
386 title: "Threads",
387 },
388 }),
389 Thread: createNativeStackScreen({
390 screen: ThreadRouteScreen,
391 linking: THREAD_LINKING_PREFIX,
392 options: GLASS_HEADER_OPTIONS,
393 }),
394 ThreadTerminal: createNativeStackScreen({
395 screen: ThreadTerminalRouteScreen,
396 linking: `${THREAD_LINKING_PREFIX}/terminal`,
397 options: SOLID_HEADER_OPTIONS,
398 }),
399 ThreadReview: createNativeStackScreen({
400 screen: ReviewSheet,
401 linking: `${THREAD_LINKING_PREFIX}/review`,
402 options: SOLID_HEADER_OPTIONS,
403 }),
404 ThreadReviewComment: createNativeStackScreen({
405 screen: ReviewCommentComposerSheet,
406 linking: `${THREAD_LINKING_PREFIX}/review-comment`,
407 options: {
408 // Android cannot host the keyboard-driven comment composer inside a
409 // formSheet; use a full-screen modal there instead.
410 presentation: Platform.OS === "android" ? "fullScreenModal" : "formSheet",
411 sheetAllowedDetents: Platform.OS === "android" ? undefined : [0.55, 0.92],
412 sheetGrabberVisible: Platform.OS !== "android",
413 },
414 }),
415 ThreadFiles: createNativeStackScreen({
416 screen: ThreadFilesTreeScreen,
417 linking: `${THREAD_LINKING_PREFIX}/files`,
418 options: {
419 ...GLASS_HEADER_OPTIONS,
420 contentStyle:
421 SHEET_BACKGROUND_COLOR !== undefined
422 ? { backgroundColor: SHEET_BACKGROUND_COLOR }
423 : undefined,
424 title: "Files",
425 },
426 }),
427 ThreadFile: createNativeStackScreen({
428 screen: ThreadFileScreen,
429 linking: `${THREAD_LINKING_PREFIX}/files/:path*`,
430 options: SOLID_HEADER_OPTIONS,
431 }),
432 GitOverview: createNativeStackScreen({
433 screen: GitOverviewSheet,
434 linking: `${THREAD_LINKING_PREFIX}/git`,
435 options: {
436 presentation: "formSheet",
437 sheetAllowedDetents: [0.55, 0.92],
438 sheetGrabberVisible: true,
439 },
440 }),
441 GitCommit: createNativeStackScreen({
442 screen: GitCommitSheet,
443 linking: `${THREAD_LINKING_PREFIX}/git/commit`,
444 options: {
445 presentation: "formSheet",
446 sheetAllowedDetents: [0.55, 0.92],
447 sheetGrabberVisible: true,
448 },
449 }),
450 GitBranches: createNativeStackScreen({
451 screen: GitBranchesSheet,
452 linking: `${THREAD_LINKING_PREFIX}/git/branches`,
453 options: {
454 presentation: "formSheet",
455 sheetAllowedDetents: [0.55, 0.92],
456 sheetGrabberVisible: true,
457 },
458 }),
459 GitConfirm: createNativeStackScreen({
460 screen: GitConfirmSheet,
461 linking: `${THREAD_LINKING_PREFIX}/git-confirm`,
462 options: {
463 presentation: "formSheet",
464 sheetAllowedDetents: [0.45, 0.7],
465 sheetGrabberVisible: true,
466 },
467 }),
468 SettingsSheet: createNativeStackScreen({
469 screen: SettingsSheetStack,
470 linking: "settings",
471 options: {
472 gestureEnabled: true,
473 headerShown: false,
474 // Android pushes settings as a regular full page with an in-screen
475 // back header; iOS keeps the detented form sheet.
476 ...(Platform.OS === "android"
477 ? { presentation: "card" as const }
478 : {
479 presentation: "formSheet" as const,
480 sheetAllowedDetents: [0.7, 0.92],
481 sheetGrabberVisible: true,
482 }),
483 },
484 }),
485 SettingsLegal: createNativeStackScreen({
486 screen: SettingsLegalRouteScreen,
487 linking: "settings/legal",
488 options: {
489 ...LEGAL_DOCUMENT_HEADER_OPTIONS,
490 title: "Legal",
491 },
492 }),
493 ConnectOnboarding: createNativeStackScreen({
494 screen: ConnectOnboardingRouteScreen,
495 linking: "connect-onboarding",
496 options: {
497 // A root-level Android formSheet does not host the native stack bar;
498 // the route renders an embedded AndroidSheetHeader instead.
499 ...(Platform.OS === "android" ? { headerShown: false } : SHEET_SOLID_HEADER_OPTIONS),
500 title: "Set up T3 Connect",
501 gestureEnabled: true,
502 presentation: "formSheet",
503 sheetAllowedDetents: [0.6, 0.95],
504 sheetGrabberVisible: true,
505 },
506 }),
507 Connections: createNativeStackScreen({
508 screen: ConnectionsRouteScreen,
509 linking: "connections",
510 options: {
511 title: "Environments",
512 // Android: full page; the screen renders its own AndroidScreenHeader,
513 // so the native bar stays hidden. iOS keeps the sheet.
514 ...(Platform.OS === "android"
515 ? { presentation: "card" as const, headerShown: false }
516 : {
517 presentation: "formSheet" as const,
518 sheetAllowedDetents: [0.55, 0.7],
519 sheetGrabberVisible: true,
520 }),
521 },
522 }),
523 ConnectionsNew: createNativeStackScreen({
524 screen: ConnectionsNewRouteScreen,
525 linking: "connections/new",
526 options: {
527 presentation: "formSheet",
528 sheetAllowedDetents: [0.55, 0.7],
529 sheetGrabberVisible: true,
530 },
531 }),
532 NewTaskSheet: createNativeStackScreen({
533 screen: NewTaskSheetStack,
534 linking: "new",
535 // The whole new-task flow (choose project → draft → add project) shares
536 // draft state via NewTaskFlowProvider. The expo-router era mounted it in
537 // app/new/_layout.tsx; this layout wrapper is the native-stack equivalent.
538 layout: ({ children }) => <NewTaskFlowProvider>{children}</NewTaskFlowProvider>,
539 options: {
540 gestureEnabled: true,
541 headerShown: false,
542 // Android pushes the flow as a regular full page — the draft should
543 // read like a thread that just doesn't exist yet; iOS keeps the sheet.
544 ...(Platform.OS === "android"
545 ? { presentation: "card" as const }
546 : {
547 presentation: "formSheet" as const,
548 sheetAllowedDetents: [0.92],
549 sheetGrabberVisible: true,
550 }),
551 },
552 }),
553 NotFound: createNativeStackScreen({
554 screen: NotFoundScreen,
555 linking: "*",
556 }),
557 },
558});
559type RootStackType = typeof RootStack;
560​
561const navigationPathConfig = {
562 screens: createPathConfigForStaticNavigation(RootStack) ?? {},
563};
564​
565declare module "@react-navigation/native" {
566 interface RootNavigator extends RootStackType {}