TypeScript
refactor(mobile): app link parsing reads top to bottom with pipe (#26330)
Julius Marminge2349c81afc1c
diff --git a/apps/mobile/src/App.tsx b/apps/mobile/src/App.tsxindex 78299a2aa48..225496bb7a4 100644--- a/apps/mobile/src/App.tsx+++ b/apps/mobile/src/App.tsx@@ -13,9 +13,11 @@ import {createPathConfigForStaticNavigation,createStaticNavigation,getActionFromState,+ getStateFromPath,type NavigationAction,type NavigationState,} from "@react-navigation/native";+import { pipe } from "effect/Function";import { RegistryContext } from "@effect/atom-react";import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages";@@ -48,12 +50,22 @@ import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-import { environmentThreadShells } from "./state/threads";import { environmentShell } from "./state/shell";import { OverlayPortalHost } from "./components/ui/OverlayPortal";-import { shouldHandleAppLink } from "./lib/appLinking";+import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";import { hasCloudPublicConfig } from "./features/cloud/publicConfig";-import { pairingFormLinkAction } from "./features/connection/devAutoConnect";-import { resolvePendingNewTaskPromptLink } from "./features/prompt-links/newTaskPromptLink";-import { resolvePendingThreadPromptLink } from "./features/prompt-links/threadPromptLink";-import { getAppStateFromPath } from "./lib/appLinkState";+import { takeScheduledTaskNewLink } from "./features/settings/scheduled-task-editor-state";+import {+ pairingFormLinkAction,+ startDevAutoConnect,+ withoutPairingTarget,+} from "./features/connection/devAutoConnect";+import {+ resolvePendingNewTaskPromptLink,+ takeNewTaskPromptLink,+} from "./features/prompt-links/newTaskPromptLink";+import {+ resolvePendingThreadPromptLink,+ takeThreadPromptLink,+} from "./features/prompt-links/threadPromptLink";import {disposeOnFoundationReplace,type FoundationHotModule,@@ -63,10 +75,12 @@ import { installUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarningimport { SubscriptionUsageCoordinator } from "./widgets/SubscriptionUsageCoordinator";import { installSubscriptionUsageRefresh } from "./widgets/useSubscriptionUsage";import { GlobalVoiceInputControl } from "./features/voice-input/GlobalVoiceInputControl";+import { restoreSettingsLinkState } from "./features/settings/settings-navigation";import {hydrateWorkspaceSelections,launchWorkspaceURL,recordWorkspaceSelections,+ settingsLinkPage,} from "./features/workspace/workspace-selection-store";import { workspaceDetailLinkAction } from "./features/workspace/workspace-detail-selection";import { NativeLayoutMetricsProvider } from "./native/native-layout-metrics";@@ -94,6 +108,19 @@ const linkingConfig = {screens: createPathConfigForStaticNavigation(RootStack, { initialRouteName: "Home" }) ?? {},} as const;+function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) {+ return pipe(+ getStateFromPath(path, options),+ (state) => restoreDraftLinkState(state, path),+ (state) => restoreSettingsLinkState(state, settingsLinkPage()),+ takeThreadPromptLink,+ takeNewTaskPromptLink,+ withoutPairingTarget,+ startDevAutoConnect,+ takeScheduledTaskNewLink,+ );+}+const appLinking = {prefixes: [Linking.createURL("/"), "t3code://", "t3code-dev://", "t3code-preview://"],config: { initialRouteName: "Home" },diff --git a/apps/mobile/src/lib/appLinkState.test.ts b/apps/mobile/src/lib/appLinkState.test.tsdeleted file mode 100644index 9b907df8005..00000000000--- a/apps/mobile/src/lib/appLinkState.test.ts+++ /dev/null@@ -1,72 +0,0 @@-import * as NodeModule from "node:module";-import { expect, it, vi } from "vite-plus/test";--const harness = vi.hoisted(() => {- vi.stubGlobal("__DEV__", true);- return { pair: vi.fn() };-});--// The native package pulls in React Native; its link parsing and actions live in core and routers.-vi.mock("@react-navigation/native", () => {- const requireFromNative = NodeModule.createRequire(- NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),- );- const requireFromCore = NodeModule.createRequire(- requireFromNative.resolve("@react-navigation/core/package.json"),- );- const { getStateFromPath } = requireFromNative("@react-navigation/core");- const { CommonActions, StackActions } = requireFromCore("@react-navigation/routers");- return { getStateFromPath, CommonActions, StackActions };-});--vi.mock("./rootNavigation", () => ({- rootNavigationRef: { isReady: () => false },- whenRootNavigationReady: () => {},-}));-vi.mock("../components/ui/Dialog", () => ({ alert: () => {} }));-vi.mock("../features/prompt-links/prompt-link-actions", () => ({}));-// Stores the prompt-link steps read only when a link carries a prompt; they load the native runtime.-vi.mock("../state/projects", () => ({}));-vi.mock("../state/pull-requests", () => ({}));-vi.mock("../state/server", () => ({}));-vi.mock("../state/threads", () => ({}));-vi.mock("../state/use-composer-drafts", () => ({}));-vi.mock("../state/workspace", () => ({}));-vi.mock("../state/use-remote-environment-registry", () => ({ pairEnvironment: harness.pair }));-vi.mock("../features/workspace/workspace-selection-store", () => ({- settingsLinkPage: () => undefined,- workspaceHasColumns: () => false,-}));--import { findLinkedRoute } from "./appLinking";-import { getAppStateFromPath } from "./appLinkState";--const PAIRING_URL = "http://192.168.1.10:3773/pair#token=abc";--// RootStack imports every screen, which Node cannot load; these are its paths for the pairing form.-const linking = {- initialRouteName: "Home",- screens: {- Home: "",- Settings: "settings",- SettingsEnvironmentNew: "settings/environment-new",- ConnectionsNew: "connections/new",- },-};--it("a link cannot aim a pairing at a saved environment", () => {- harness.pair.mockResolvedValue({ ok: false, error: null });- for (const path of ["connections/new", "settings/environment-new"]) {- harness.pair.mockClear();- const state = getAppStateFromPath(- `${path}?routeFor=victim&pairingUrl=${encodeURIComponent(PAIRING_URL)}&autoConnect=1`,- linking,- );-- expect(findLinkedRoute(state, ["ConnectionsNew", "SettingsEnvironmentNew"])?.params).toEqual({- pairingUrl: PAIRING_URL,- autoConnect: "1",- });- expect(harness.pair).toHaveBeenCalledExactlyOnceWith(PAIRING_URL);- }-});diff --git a/apps/mobile/src/lib/appLinkState.ts b/apps/mobile/src/lib/appLinkState.tsdeleted file mode 100644index f404819f68f..00000000000--- a/apps/mobile/src/lib/appLinkState.ts+++ /dev/null@@ -1,27 +0,0 @@-import { getStateFromPath } from "@react-navigation/native";--import { startDevAutoConnect, withoutPairingTarget } from "../features/connection/devAutoConnect";-import { takeNewTaskPromptLink } from "../features/prompt-links/newTaskPromptLink";-import { takeThreadPromptLink } from "../features/prompt-links/threadPromptLink";-import { takeScheduledTaskNewLink } from "../features/settings/scheduled-task-editor-state";-import { restoreSettingsLinkState } from "../features/settings/settings-navigation";-import { settingsLinkPage } from "../features/workspace/workspace-selection-store";-import { restoreDraftLinkState } from "./appLinking";--/** Parses an app link into navigation state, taking each link-owned side effect on the way. */-export function getAppStateFromPath(path: string, options: Parameters<typeof getStateFromPath>[1]) {- return takeScheduledTaskNewLink(- startDevAutoConnect(- withoutPairingTarget(- takeNewTaskPromptLink(- takeThreadPromptLink(- restoreSettingsLinkState(- restoreDraftLinkState(getStateFromPath(path, options), path),- settingsLinkPage(),- ),- ),- ),- ),- ),- );-}