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

appLinking.ts

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