@@ -12,8 +12,8 @@ import {
1212 type StaticScreenProps ,
1313} from "@react-navigation/native" ;
1414import type { EnvironmentId } from "@t3tools/contracts" ;
15- import { AsyncResult } from "effect/reactivity " ;
16- import { useEffect , useRef , useState } from "react" ;
15+ import { useAtomValue } from "@ effect/atom-react " ;
16+ import { useState } from "react" ;
1717import { Linking , Pressable , StyleSheet , View } from "react-native" ;
1818import Svg , { Defs , RadialGradient , Rect , Stop } from "react-native-svg" ;
1919import { useSafeAreaInsets } from "react-native-safe-area-context" ;
@@ -24,13 +24,11 @@ import { SettingsScreen } from "../settings/components/SettingsScreen";
2424import { AppText as Text , AppTextInput , type AppTextInputProps } from "../../components/ui/AppText" ;
2525import { FrostedCutout } from "../../components/ui/FrostedCutout" ;
2626import { buildPairingUrl , extractPairingUrlFromQrPayload , parsePairingUrl } from "./pairing" ;
27- import {
28- setPendingConnectionError ,
29- useRemoteConnections ,
30- } from "../../state/use-remote-environment-registry" ;
27+ import { useRemoteConnections } from "../../state/use-remote-environment-registry" ;
3128import { SymbolView } from "../../components/ui/AppSymbol" ;
3229import { hasCloudPublicConfig } from "../cloud/publicConfig" ;
3330import { CloudEnvironmentRows } from "./CloudEnvironmentRows" ;
31+ import { devAutoConnectAttemptAtom } from "./devAutoConnect" ;
3432import { splitEnvironmentSections } from "./environmentSections" ;
3533import { leaveSettingsPage } from "../settings/settings-navigation" ;
3634import { workspaceHasColumns } from "../workspace/workspace-selection-store" ;
@@ -45,73 +43,73 @@ type ConnectionsNewRouteParams = {
4543export function ConnectionsNewRouteScreen ( {
4644 route,
4745} : StaticScreenProps < ConnectionsNewRouteParams | undefined > ) {
48- const {
49- connectionPairingUrl,
50- onChangeConnectionPairingUrl,
51- onConnectPress,
52- pairingConnectionError,
53- } = useRemoteConnections ( ) ;
54- const navigation = useNavigation ( ) ;
55- const routeName = useRoute ( ) . name ;
5646 const params = route . params ?? { } ;
5747 // Dev-only prefill prevents production links from supplying an attacker-chosen host and token.
5848 const routePairingUrl = __DEV__ ? ( params . pairingUrl ?. trim ( ) ?? "" ) : "" ;
59- const shouldAutoConnect =
60- __DEV__ &&
61- routePairingUrl . length > 0 &&
62- ( params . autoConnect === "1" || params . autoConnect === "true" ) ;
49+ // A new prefill link replaces whatever the form held.
50+ return (
51+ < PairingForm
52+ key = { routePairingUrl }
53+ routePairingUrl = { routePairingUrl }
54+ routeFor = { params . routeFor }
55+ />
56+ ) ;
57+ }
58+
59+ function PairingForm ( {
60+ routePairingUrl,
61+ routeFor,
62+ } : {
63+ readonly routePairingUrl : string ;
64+ readonly routeFor : EnvironmentId | undefined ;
65+ } ) {
66+ const { connectionPairingUrl, onChangeConnectionPairingUrl, onConnectPress } =
67+ useRemoteConnections ( ) ;
68+ const navigation = useNavigation ( ) ;
69+ const routeName = useRoute ( ) . name ;
6370 const insets = useSafeAreaInsets ( ) ;
64- const [ hostInput , setHostInput ] = useState ( "" ) ;
65- const [ codeInput , setCodeInput ] = useState ( "" ) ;
71+ const [ initialPairing ] = useState ( ( ) => parsePairingUrl ( routePairingUrl || connectionPairingUrl ) ) ;
72+ const [ hostInput , setHostInput ] = useState ( initialPairing . host ) ;
73+ const [ codeInput , setCodeInput ] = useState ( initialPairing . code ) ;
6674 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
75+ const [ submitError , setSubmitError ] = useState < string | null > ( null ) ;
76+ const autoConnectAttempt = useAtomValue ( devAutoConnectAttemptAtom ) ;
77+ const autoConnect =
78+ autoConnectAttempt !== null &&
79+ routePairingUrl . length > 0 &&
80+ autoConnectAttempt . pairingUrl === routePairingUrl
81+ ? autoConnectAttempt
82+ : null ;
83+ // The dev auto-connect report applies until the user starts their own attempt.
84+ const [ ownAttempt , setOwnAttempt ] = useState ( false ) ;
85+ const connecting = isSubmitting || ( ! ownAttempt && autoConnect ?. status === "connecting" ) ;
86+ const pairingConnectionError =
87+ submitError ?? ( ! ownAttempt && autoConnect ?. status === "failed" ? autoConnect . error : null ) ;
6788 const [ cameraPermission , requestCameraPermission ] = useCameraPermissions ( ) ;
6889 const screenFocused = useIsFocused ( ) ;
6990 const [ scannerLocked , setScannerLocked ] = useState ( false ) ;
7091 // A good scan parks the camera so it can't re-read the code over later edits.
7192 const [ scanComplete , setScanComplete ] = useState ( false ) ;
72- const attemptedAutoConnectRef = useRef < string | null > ( null ) ;
7393
7494 const headerIconColor = useUniwindTheme ( ) [ "--color-icon" ] ;
7595
76- const connectDisabled = isSubmitting || hostInput . trim ( ) . length === 0 ;
77-
78- useEffect ( ( ) => {
79- const { host, code } = parsePairingUrl ( connectionPairingUrl ) ;
80- // oxlint-disable-next-line react/set-state-in-effect -- A new pairing URL reseeds the editable host and code fields.
81- setHostInput ( host ) ;
82- setCodeInput ( code ) ;
83- } , [ connectionPairingUrl ] ) ;
96+ const connectDisabled = connecting || hostInput . trim ( ) . length === 0 ;
8497
85- useEffect ( ( ) => {
86- if ( routePairingUrl . length === 0 ) {
87- return ;
88- }
89-
90- const { host, code } = parsePairingUrl ( routePairingUrl ) ;
91- // oxlint-disable-next-line react/set-state-in-effect -- A pairing URL from the route reseeds the editable host and code fields.
92- setHostInput ( host ) ;
93- setCodeInput ( code ) ;
94- } , [ routePairingUrl ] ) ;
95-
96- useEffect ( ( ) => {
97- if ( pairingConnectionError ) {
98- // oxlint-disable-next-line react/set-state-in-effect -- A pairing failure reported by the connection runtime ends the submit.
99- setIsSubmitting ( false ) ;
100- }
101- } , [ pairingConnectionError ] ) ;
98+ const clearError = ( ) => {
99+ setSubmitError ( null ) ;
100+ setOwnAttempt ( true ) ;
101+ } ;
102102
103103 const handleHostChange = ( value : string ) => {
104104 setHostInput ( value ) ;
105- setPendingConnectionError ( null ) ;
105+ clearError ( ) ;
106106 } ;
107107
108108 const handleCodeChange = ( value : string ) => {
109109 setCodeInput ( value ) ;
110- setPendingConnectionError ( null ) ;
110+ clearError ( ) ;
111111 } ;
112112
113- useEffect ( ( ) => ( ) => setPendingConnectionError ( null ) , [ ] ) ;
114-
115113 const handleScanPress = async ( ) => {
116114 const permission = await requestCameraPermission ( ) ;
117115 if ( permission . granted ) {
@@ -156,17 +154,21 @@ export function ConnectionsNewRouteScreen({
156154 }
157155 } ;
158156
159- const connectAndClose = async ( pairingUrl : string ) => {
157+ const handleSubmit = async ( ) => {
158+ const pairingUrl = buildPairingUrl ( hostInput , codeInput ) ;
159+ setOwnAttempt ( true ) ;
160+ setSubmitError ( null ) ;
160161 setIsSubmitting ( true ) ;
161162 onChangeConnectionPairingUrl ( pairingUrl ) ;
162- const result = await onConnectPress ( pairingUrl , params . routeFor ) ;
163+ const outcome = await onConnectPress ( pairingUrl , routeFor ) ;
163164 setIsSubmitting ( false ) ;
165+ setSubmitError ( outcome . error ) ;
164166 void Haptics . notificationAsync (
165- AsyncResult . isSuccess ( result )
167+ outcome . ok
166168 ? Haptics . NotificationFeedbackType . Success
167169 : Haptics . NotificationFeedbackType . Error ,
168170 ) ;
169- if ( AsyncResult . isSuccess ( result ) ) {
171+ if ( outcome . ok ) {
170172 // Replacing with Home would nest it inside any sheet below; return to the opener.
171173 if ( ! navigation . canGoBack ( ) ) {
172174 navigation . dispatch ( StackActions . replace ( "Home" ) ) ;
@@ -177,27 +179,14 @@ export function ConnectionsNewRouteScreen({
177179 }
178180 } ;
179181
180- const handleSubmit = async ( ) => {
181- await connectAndClose ( buildPairingUrl ( hostInput , codeInput ) ) ;
182- } ;
183-
184- useEffect ( ( ) => {
185- if ( ! shouldAutoConnect || attemptedAutoConnectRef . current === routePairingUrl ) {
186- return ;
187- }
188-
189- attemptedAutoConnectRef . current = routePairingUrl ;
190- void connectAndClose ( routePairingUrl ) ;
191- } , [ connectAndClose , routePairingUrl , shouldAutoConnect ] ) ;
192-
193182 return (
194183 < SettingsScreen
195184 formSheet = { routeName === "ConnectionsNew" }
196185 title = "Add environment"
197186 actions = { [
198187 {
199- accessibilityLabel : isSubmitting ? "Connecting" : "Add environment" ,
200- loading : isSubmitting ,
188+ accessibilityLabel : connecting ? "Connecting" : "Add environment" ,
189+ loading : connecting ,
201190 icon : "checkmark" ,
202191 tintColor : headerIconColor ,
203192 disabled : connectDisabled ,
0 commit comments