@@ -12,8 +12,8 @@ import { |
12 | 12 | type StaticScreenProps, |
13 | 13 | } from "@react-navigation/native"; |
14 | 14 | import 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"; |
17 | 17 | import { Linking, Pressable, StyleSheet, View } from "react-native"; |
18 | 18 | import Svg, { Defs, RadialGradient, Rect, Stop } from "react-native-svg"; |
19 | 19 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
@@ -24,13 +24,11 @@ import { SettingsScreen } from "../settings/components/SettingsScreen"; |
24 | 24 | import { AppText as Text, AppTextInput, type AppTextInputProps } from "../../components/ui/AppText"; |
25 | 25 | import { FrostedCutout } from "../../components/ui/FrostedCutout"; |
26 | 26 | import { 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"; |
31 | 28 | import { SymbolView } from "../../components/ui/AppSymbol"; |
32 | 29 | import { hasCloudPublicConfig } from "../cloud/publicConfig"; |
33 | 30 | import { CloudEnvironmentRows } from "./CloudEnvironmentRows"; |
| 31 | +import { devAutoConnectAttemptAtom } from "./devAutoConnect"; |
34 | 32 | import { splitEnvironmentSections } from "./environmentSections"; |
35 | 33 | import { leaveSettingsPage } from "../settings/settings-navigation"; |
36 | 34 | import { workspaceHasColumns } from "../workspace/workspace-selection-store"; |
@@ -45,73 +43,73 @@ type ConnectionsNewRouteParams = { |
45 | 43 | export function ConnectionsNewRouteScreen({ |
46 | 44 | route, |
47 | 45 | }: StaticScreenProps<ConnectionsNewRouteParams | undefined>) { |
48 | | - const { |
49 | | - connectionPairingUrl, |
50 | | - onChangeConnectionPairingUrl, |
51 | | - onConnectPress, |
52 | | - pairingConnectionError, |
53 | | - } = useRemoteConnections(); |
54 | | - const navigation = useNavigation(); |
55 | | - const routeName = useRoute().name; |
56 | 46 | const params = route.params ?? {}; |
57 | 47 | // Dev-only prefill prevents production links from supplying an attacker-chosen host and token. |
58 | 48 | 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; |
63 | 70 | 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); |
66 | 74 | 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); |
67 | 88 | const [cameraPermission, requestCameraPermission] = useCameraPermissions(); |
68 | 89 | const screenFocused = useIsFocused(); |
69 | 90 | const [scannerLocked, setScannerLocked] = useState(false); |
70 | 91 | // A good scan parks the camera so it can't re-read the code over later edits. |
71 | 92 | const [scanComplete, setScanComplete] = useState(false); |
72 | | - const attemptedAutoConnectRef = useRef<string | null>(null); |
73 | 93 |
|
74 | 94 | const headerIconColor = useUniwindTheme()["--color-icon"]; |
75 | 95 |
|
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; |
84 | 97 |
|
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 | + }; |
102 | 102 |
|
103 | 103 | const handleHostChange = (value: string) => { |
104 | 104 | setHostInput(value); |
105 | | - setPendingConnectionError(null); |
| 105 | + clearError(); |
106 | 106 | }; |
107 | 107 |
|
108 | 108 | const handleCodeChange = (value: string) => { |
109 | 109 | setCodeInput(value); |
110 | | - setPendingConnectionError(null); |
| 110 | + clearError(); |
111 | 111 | }; |
112 | 112 |
|
113 | | - useEffect(() => () => setPendingConnectionError(null), []); |
114 | | - |
115 | 113 | const handleScanPress = async () => { |
116 | 114 | const permission = await requestCameraPermission(); |
117 | 115 | if (permission.granted) { |
@@ -156,17 +154,21 @@ export function ConnectionsNewRouteScreen({ |
156 | 154 | } |
157 | 155 | }; |
158 | 156 |
|
159 | | - const connectAndClose = async (pairingUrl: string) => { |
| 157 | + const handleSubmit = async () => { |
| 158 | + const pairingUrl = buildPairingUrl(hostInput, codeInput); |
| 159 | + setOwnAttempt(true); |
| 160 | + setSubmitError(null); |
160 | 161 | setIsSubmitting(true); |
161 | 162 | onChangeConnectionPairingUrl(pairingUrl); |
162 | | - const result = await onConnectPress(pairingUrl, params.routeFor); |
| 163 | + const outcome = await onConnectPress(pairingUrl, routeFor); |
163 | 164 | setIsSubmitting(false); |
| 165 | + setSubmitError(outcome.error); |
164 | 166 | void Haptics.notificationAsync( |
165 | | - AsyncResult.isSuccess(result) |
| 167 | + outcome.ok |
166 | 168 | ? Haptics.NotificationFeedbackType.Success |
167 | 169 | : Haptics.NotificationFeedbackType.Error, |
168 | 170 | ); |
169 | | - if (AsyncResult.isSuccess(result)) { |
| 171 | + if (outcome.ok) { |
170 | 172 | // Replacing with Home would nest it inside any sheet below; return to the opener. |
171 | 173 | if (!navigation.canGoBack()) { |
172 | 174 | navigation.dispatch(StackActions.replace("Home")); |
@@ -177,27 +179,14 @@ export function ConnectionsNewRouteScreen({ |
177 | 179 | } |
178 | 180 | }; |
179 | 181 |
|
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 | | - |
193 | 182 | return ( |
194 | 183 | <SettingsScreen |
195 | 184 | formSheet={routeName === "ConnectionsNew"} |
196 | 185 | title="Add environment" |
197 | 186 | actions={[ |
198 | 187 | { |
199 | | - accessibilityLabel: isSubmitting ? "Connecting" : "Add environment", |
200 | | - loading: isSubmitting, |
| 188 | + accessibilityLabel: connecting ? "Connecting" : "Add environment", |
| 189 | + loading: connecting, |
201 | 190 | icon: "checkmark", |
202 | 191 | tintColor: headerIconColor, |
203 | 192 | disabled: connectDisabled, |
|