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 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 );
16}
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 };
41}
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);
48}
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;
61}