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

appLinking.ts

131 lines4.9 KB

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 );
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/**
30 * A file path param spanning every remaining segment. React Navigation 7 has no `:param*`
31 * wildcard; it reads that as a one-segment param named `path*`.
32 */
33export const FILE_PATH_LINK_PARAM = ":path(.+)";
34​
35/** Keep the published draft URLs while using the root thread screen. */
36export function restoreDraftLinkState(
37 state: PartialState<NavigationState> | undefined,
38 path: string,
39) {
40 const pathname = path.split("?")[0]?.replace(/^\/+|\/+$/g, "");
41 if (!state || !pathname || !/^new\/draft(?:\/|$)/.test(pathname)) return state;
42 const target = state.routes.at(-1);
43 if (!target) return state;
44 // The draft screen checks out its branch on mount; the checkout comes from the draft, not the link.
45 const {
46 branch: _branch,
47 worktreePath: _worktreePath,
48 ...draft
49 } = (target.params ?? {}) as {
50 readonly [param: string]: unknown;
51 };
52 const routes = state.routes.slice(0, -1);
53 routes.push({ name: "Thread", params: { draft } });
54 if (pathname !== "new/draft") routes.push(target);
55 return { ...state, index: routes.length - 1, routes };
57​
58type LinkState = PartialState<NavigationState>;
59type LinkRoute = LinkState["routes"][number];
60​
61function isLinkState(value: unknown): value is LinkState {
62 return typeof value === "object" && value !== null && Array.isArray((value as LinkState).routes);
64​
65/** The first route named one of `names`, at any nesting depth of a parsed link. */
66export function findLinkedRoute(
67 state: LinkState | undefined,
68 names: ReadonlyArray<string>,
69): LinkRoute | undefined {
70 for (const route of state?.routes ?? []) {
71 if (names.includes(route.name)) return route;
72 const nested = isLinkState(route.state) ? findLinkedRoute(route.state, names) : undefined;
73 if (nested) return nested;
74 }
75 return undefined;
77​
78/**
79 * The last route named `name` (and accepted by `accept`, when given) at any depth of a live
80 * navigation state, with the key of the navigator holding it, so an action can be dispatched as
81 * that screen would. `focused` says whether it is that navigator's current route.
82 */
83export function findStackRoute(
84 state: NavigationState | undefined,
85 name: string,
86 accept?: (params: Readonly<Record<string, unknown>> | undefined) => boolean,
87):
88 | { readonly navigatorKey: string; readonly routeKey: string; readonly focused: boolean }
89 | undefined {
90 if (!state) return undefined;
91 for (let index = state.routes.length - 1; index >= 0; index -= 1) {
92 const route = state.routes[index]!;
93 if (
94 route.name === name &&
95 (accept === undefined ||
96 accept(route.params as Readonly<Record<string, unknown>> | undefined))
97 ) {
98 return { navigatorKey: state.key, routeKey: route.key, focused: index === state.index };
99 }
100 const nested = route.state;
101 // Mounted navigators always have keys; a partial state from a link has not been mounted yet.
102 if (typeof nested?.key === "string") {
103 const found = findStackRoute(nested as NavigationState, name, accept);
104 if (found) return found;
105 }
106 }
107 return undefined;
109​
110/** Rewrites the first route named one of `names`, keeping every untouched branch of the state. */
111export function updateLinkedRoute(
112 state: LinkState | undefined,
113 names: ReadonlyArray<string>,
114 update: (route: LinkRoute) => LinkRoute,
115): LinkState | undefined {
116 if (!state) return state;
117 for (const [index, route] of state.routes.entries()) {
118 let next: LinkRoute | undefined;
119 if (names.includes(route.name)) next = update(route);
120 else if (isLinkState(route.state)) {
121 const nested = updateLinkedRoute(route.state, names, update);
122 if (nested !== route.state) next = { ...route, state: nested };
123 }
124 if (next) {
125 const routes = state.routes.slice();
126 routes[index] = next;
127 return { ...state, routes };
128 }
129 }
130 return state;