11import type { NavigationState , PartialState } from "@react-navigation/native" ;
22import { CommonActions , StackActions } from "@react-navigation/native" ;
3- import type { EnvironmentId } from "@t3tools/contracts" ;
43import { Atom } from "effect/reactivity" ;
54
6- import { findLinkedRoute } from "../../lib/appLinking" ;
5+ import { findLinkedRoute , updateLinkedRoute } from "../../lib/appLinking" ;
76import { rootNavigationRef , whenRootNavigationReady } from "../../lib/rootNavigation" ;
87import { appAtomRegistry } from "../../state/atom-registry" ;
98import { leaveSettingsPage } from "../settings/settings-navigation" ;
@@ -44,10 +43,10 @@ function isAutoConnectFlag(value: unknown): boolean {
4443 return value === "1" || value === "true" ;
4544}
4645
47- async function connect ( pairingUrl : string , expectedEnvironmentId : EnvironmentId | undefined ) {
46+ async function connect ( pairingUrl : string ) {
4847 const attempt : DevAutoConnectAttempt = { pairingUrl, status : "connecting" } ;
4948 appAtomRegistry . set ( devAutoConnectAttemptAtom , attempt ) ;
50- const outcome = await pairEnvironment ( pairingUrl , expectedEnvironmentId ) ;
49+ const outcome = await pairEnvironment ( pairingUrl ) ;
5150 // A later link replaced or cleared this attempt; its result belongs to no open form.
5251 if ( appAtomRegistry . get ( devAutoConnectAttemptAtom ) !== attempt ) return ;
5352 if ( ! outcome . ok ) {
@@ -71,6 +70,20 @@ async function connect(pairingUrl: string, expectedEnvironmentId: EnvironmentId
7170 } ) ;
7271}
7372
73+ /**
74+ * `routeFor` makes a pairing an "Add route" to a saved environment, which skips the guard against
75+ * a pairing taking over that environment's id. Only in-app navigation may set it, never a link.
76+ */
77+ export function withoutPairingTarget (
78+ state : PartialState < NavigationState > | undefined ,
79+ ) : PartialState < NavigationState > | undefined {
80+ return updateLinkedRoute ( state , PAIRING_FORM_ROUTES , ( route ) => {
81+ if ( route . params === undefined || ! ( "routeFor" in route . params ) ) return route ;
82+ const { routeFor : _routeFor , ...params } = route . params as { readonly routeFor ?: unknown } ;
83+ return { ...route , params } ;
84+ } ) ;
85+ }
86+
7487/**
7588 * `connections/new?pairingUrl=…&autoConnect=1` (or `settings/environment-new?…`) pairs from the
7689 * link itself, so a remount of the form never pairs twice. Production builds ignore the flag so
@@ -85,11 +98,10 @@ export function startDevAutoConnect(
8598 const params = ( route . params ?? { } ) as {
8699 readonly autoConnect ?: unknown ;
87100 readonly pairingUrl ?: unknown ;
88- readonly routeFor ?: EnvironmentId ;
89101 } ;
90102 const pairingUrl = typeof params . pairingUrl === "string" ? params . pairingUrl . trim ( ) : "" ;
91103 if ( isAutoConnectFlag ( params . autoConnect ) && pairingUrl . length > 0 ) {
92- void connect ( pairingUrl , params . routeFor ) ;
104+ void connect ( pairingUrl ) ;
93105 } else {
94106 // Only links put a pairing URL on the form, so a form opened by any other link starts
95107 // clean instead of showing an earlier auto-connect's failure for the same URL.
0 commit comments