Open History to see the last commit
1import type { NavigationState, PartialState } from "@react-navigation/native";
2
3/**
4 * The Expo dev client launches the app via
5 * <scheme>://expo-development-client/?url=<packager> — that URL addresses
6 * the launcher, not app navigation. Without this filter it falls through
7 * to the NotFound wildcard route on every dev launch.
8 */
9export function shouldHandleAppLink(url: string, cloudConfigured: boolean): boolean {
10 // Only the route: a thread link's ?prompt= may mention either lifecycle URL.
11 const route = url.split(/[?#]/)[0] ?? "";
12 return (
13 !route.includes("expo-development-client") &&
14 !route.includes("://expo-sharing") &&
15 !/^t3code(-dev|-preview)?:\/*$/.test(url) &&
16 (cloudConfigured || !CLOUD_ONLY_LINK_PATH.test(route))
17 );
18}
19
20/**
21 * Screens that only exist with T3 Connect configured. A build without it has
22 * no entry points to them, so a link must not open one empty. Anchored to the
23 * app-relative path (after `exp://host/--/` in Expo Go) so a file route whose
24 * path merely ends in one of these names still opens.
25 */
26const CLOUD_ONLY_LINK_PATH =
27 /^[a-z][\w+.-]*:\/*(?:[^/]*\/--\/)?(?:connect-onboarding|settings\/(?:auth|waitlist))\/*$/i;
28
29/** Keep the published draft URLs while using the root thread screen. */
30export function restoreDraftLinkState(
31 state: PartialState<NavigationState> | undefined,
32 path: string,
33) {
34 const pathname = path.split("?")[0]?.replace(/^\/+|\/+$/g, "");
35 if (!state || !pathname || !/^new\/draft(?:\/|$)/.test(pathname)) return state;
36 const target = state.routes.at(-1);
37 if (!target) return state;
38 // The draft screen checks out its branch on mount; the checkout comes from the draft, not the link.
39 const {
40 branch: _branch,
41 worktreePath: _worktreePath,
42 ...draft
43 } = (target.params ?? {}) as {
44 readonly [param: string]: unknown;
45 };
46 const routes = state.routes.slice(0, -1);
47 routes.push({ name: "Thread", params: { draft } });
48 if (pathname !== "new/draft") routes.push(target);
49 return { ...state, index: routes.length - 1, routes };
50}
51
52type LinkState = PartialState<NavigationState>;
53type LinkRoute = LinkState["routes"][number];
54
55function isLinkState(value: unknown): value is LinkState {
56 return typeof value === "object" && value !== null && Array.isArray((value as LinkState).routes);
57}
58
59/** The first route named one of `names`, at any nesting depth of a parsed link. */
60export function findLinkedRoute(
61 state: LinkState | undefined,
62 names: ReadonlyArray<string>,
63): LinkRoute | undefined {
64 for (const route of state?.routes ?? []) {
65 if (names.includes(route.name)) return route;
66 const nested = isLinkState(route.state) ? findLinkedRoute(route.state, names) : undefined;
67 if (nested) return nested;
68 }
69 return undefined;
70}
71
72/**
73 * The last route named `name` (and accepted by `accept`, when given) at any depth of a live
74 * navigation state, with the key of the navigator holding it, so an action can be dispatched as
75 * that screen would. `focused` says whether it is that navigator's current route.
76 */
77export function findStackRoute(
78 state: NavigationState | undefined,
79 name: string,
80 accept?: (params: Readonly<Record<string, unknown>> | undefined) => boolean,
81):
82 | { readonly navigatorKey: string; readonly routeKey: string; readonly focused: boolean }
83 | undefined {
84 if (!state) return undefined;
85 for (let index = state.routes.length - 1; index >= 0; index -= 1) {
86 const route = state.routes[index]!;
87 if (
88 route.name === name &&
89 (accept === undefined ||
90 accept(route.params as Readonly<Record<string, unknown>> | undefined))
91 ) {
92 return { navigatorKey: state.key, routeKey: route.key, focused: index === state.index };
93 }
94 const nested = route.state;
95 // Mounted navigators always have keys; a partial state from a link has not been mounted yet.
96 if (typeof nested?.key === "string") {
97 const found = findStackRoute(nested as NavigationState, name, accept);
98 if (found) return found;
99 }
100 }
101 return undefined;
102}
103
104/** Rewrites the first route named one of `names`, keeping every untouched branch of the state. */
105export function updateLinkedRoute(
106 state: LinkState | undefined,
107 names: ReadonlyArray<string>,
108 update: (route: LinkRoute) => LinkRoute,
109): LinkState | undefined {
110 if (!state) return state;
111 for (const [index, route] of state.routes.entries()) {
112 let next: LinkRoute | undefined;
113 if (names.includes(route.name)) next = update(route);
114 else if (isLinkState(route.state)) {
115 const nested = updateLinkedRoute(route.state, names, update);
116 if (nested !== route.state) next = { ...route, state: nested };
117 }
118 if (next) {
119 const routes = state.routes.slice();
120 routes[index] = next;
121 return { ...state, routes };
122 }
123 }
124 return state;
125}