@@ -2,6 +2,7 @@ import * as React from "react";
22import { toast } from "sonner" ;
33
44import { SidebarRelayConnectionCompactCard } from "@/features/sidebar/ui/SidebarRelayConnectionCard" ;
5+ import { useRelayConnection } from "@/shared/api/useRelayConnection" ;
56import { useReconnectRelay } from "@/shared/api/useReconnectRelay" ;
67import { cn } from "@/shared/lib/cn" ;
78import { isRelayUnreachableError } from "@/shared/lib/relayError" ;
@@ -30,7 +31,15 @@ function OnboardingRelayConnectionErrorCard({
3031 isSaving : boolean ;
3132 message : string ;
3233} ) {
33- const { isPending : isReconnectPending , reconnect } = useReconnectRelay ( ) ;
34+ const {
35+ isPending : isReconnectPending ,
36+ isWaitingOnReconnectHook,
37+ reconnect,
38+ } = useReconnectRelay ( ) ;
39+ // Track whether a reconnect attempt was ever initiated from this card so we
40+ // don't call markSuccess() on a "connected" state that pre-dates any click.
41+ const hadActiveReconnectRef = React . useRef ( false ) ;
42+ const relayConnectionState = useRelayConnection ( ) ;
3443 const [ dismissedErrorMessage , setDismissedErrorMessage ] = React . useState <
3544 string | null
3645 > ( null ) ;
@@ -73,23 +82,43 @@ function OnboardingRelayConnectionErrorCard({
7382 } , ONBOARDING_CONNECTIVITY_SUCCESS_AUTO_DISMISS_MS ) ;
7483 } , [ message ] ) ;
7584
85+ // Observe real relay connection state. When this card initiated a reconnect
86+ // attempt (phase-3 path: reconnect() returns false) and the relay later
87+ // becomes connected, mark success. Guard: only fire when we actually
88+ // started a reconnect so a pre-existing connected state doesn't trigger it.
89+ React . useEffect ( ( ) => {
90+ if ( relayConnectionState === "connected" && hadActiveReconnectRef . current ) {
91+ hadActiveReconnectRef . current = false ;
92+ markSuccess ( ) ;
93+ }
94+ } , [ relayConnectionState , markSuccess ] ) ;
95+
7696 const runConnectivityAction = React . useCallback (
7797 ( runAction : ( ) => Promise < boolean | undefined > ) => {
7898 if ( reconnectActionPendingRef . current ) {
7999 return ;
80100 }
81101
102+ hadActiveReconnectRef . current = true ;
82103 reconnectActionPendingRef . current = true ;
83104 setIsReconnectActionPending ( true ) ;
84105 setHasSuccess ( false ) ;
85106 void Promise . resolve ( )
86107 . then ( runAction )
87108 . then ( ( didReconnect ) => {
88109 if ( didReconnect !== false ) {
110+ // Synchronous success (phase 1) — clear the ref and mark success
111+ // immediately. The connection-state effect won't fire because the
112+ // ref was just cleared.
113+ hadActiveReconnectRef . current = false ;
89114 markSuccess ( ) ;
90115 }
116+ // didReconnect === false means phase 3 is active; the
117+ // connection-state effect will call markSuccess() when the relay
118+ // becomes connected.
91119 } )
92120 . catch ( ( error ) => {
121+ hadActiveReconnectRef . current = false ;
93122 const detail = error instanceof Error ? error . message : String ( error ) ;
94123 toast . error ( `Could not reconnect to the relay. ${ detail } ` ) ;
95124 } )
@@ -116,6 +145,7 @@ function OnboardingRelayConnectionErrorCard({
116145 isActionDisabled = { isActionPending }
117146 isConnected = { hasSuccess }
118147 isReconnectPending = { isActionPending }
148+ isWaitingOnReconnectHook = { isWaitingOnReconnectHook }
119149 onDismiss = { ( ) => setDismissedErrorMessage ( message ) }
120150 onReconnect = { handleReconnectRelay }
121151 surface = "secondary"
0 commit comments