@@ -2,7 +2,7 @@ import { useAuth } from "@clerk/react";
22import { useAtomValue } from "@effect/atom-react" ;
33import { AuthRelayReadScope , AuthRelayWriteScope , type AuthSessionState } from "@t3tools/contracts" ;
44import { AsyncResult , Atom } from "effect/reactivity" ;
5- import { useEffect , useRef , useState } from "react" ;
5+ import { useState } from "react" ;
66
77import {
88 CONNECT_ONBOARDING_OPT_OUT_STORAGE_KEY ,
@@ -72,76 +72,60 @@ function ConfiguredConnectOnboardingDialog() {
7272 const [ publishAgentActivity , setPublishAgentActivity ] = useState ( true ) ;
7373 const [ dontShowAgain , setDontShowAgain ] = useState ( false ) ;
7474 const [ isApplying , setIsApplying ] = useState ( false ) ;
75- const prefilledFromLinkStateRef = useRef ( false ) ;
76- const observedAccountRef = useRef < string | null | undefined > ( undefined ) ;
75+ const [ prefilledFromLinkState , setPrefilledFromLinkState ] = useState ( false ) ;
76+ // undefined until auth first settles, so the account already signed in at load
77+ // never reads as a fresh sign-in.
78+ const [ observedAccount , setObservedAccount ] = useState < string | null | undefined > ( undefined ) ;
7779
78- useEffect ( ( ) => {
79- if ( step === "publish" && sessionScopesKnown && ! showPublishStep && ! isApplying ) {
80- setStep ( "devices" ) ;
81- }
82- } , [ isApplying , sessionScopesKnown , showPublishStep , step ] ) ;
80+ if ( step === "publish" && sessionScopesKnown && ! showPublishStep && ! isApplying ) {
81+ setStep ( "devices" ) ;
82+ }
8383
8484 const optOutAccounts = optOutState . optOutAccounts ;
8585
86- useEffect ( ( ) => {
87- if ( ! isLoaded ) return ;
88- // A loaded-but-incomplete snapshot (signed in, user id not yet populated)
89- // must not be recorded as signed-out — the next render would then look
90- // like a fresh sign-in on a cold load.
91- if ( isSignedIn && ! userId ) return ;
92- const previousAccount = observedAccountRef . current ;
86+ // A loaded-but-incomplete snapshot (signed in, user id not yet populated)
87+ // must not be recorded as signed-out — the next render would then look
88+ // like a fresh sign-in on a cold load.
89+ if ( isLoaded && ! ( isSignedIn && ! userId ) ) {
9390 const nextAccount = isSignedIn && userId ? userId : null ;
94- observedAccountRef . current = nextAccount ;
95- if ( previousAccount !== undefined && previousAccount !== nextAccount && nextAccount !== null ) {
96- setRequestedAccount ( nextAccount ) ;
91+ if ( nextAccount !== observedAccount ) {
92+ setObservedAccount ( nextAccount ) ;
93+ if ( observedAccount !== undefined && nextAccount !== null ) {
94+ setRequestedAccount ( nextAccount ) ;
95+ }
9796 }
98- } , [ isLoaded , isSignedIn , userId ] ) ;
97+ }
98+
99+ if ( openForAccount !== null && ( ! isSignedIn || userId !== openForAccount ) ) {
100+ setOpenForAccount ( null ) ;
101+ }
102+ if ( requestedAccount !== null && ( ! isSignedIn || userId !== requestedAccount ) ) {
103+ setRequestedAccount ( null ) ;
104+ }
99105
100106 // A manageable session implies a primary environment, so when the scopes
101107 // allow publishing, wait for the connection target too — otherwise the
102108 // wizard could open on the devices step moments before the publish step
103109 // becomes available and freeze there.
104110 const publishStepDecided = ! canManageRelay || controller . linkState . target !== null ;
105111
106- useEffect ( ( ) => {
107- if ( requestedAccount === null || openForAccount !== null ) return ;
112+ if ( requestedAccount !== null && openForAccount === null ) {
108113 if ( optOutAccounts . includes ( requestedAccount ) ) {
109114 setRequestedAccount ( null ) ;
110- return ;
111- }
112- if ( ! sessionScopesKnown || ! publishStepDecided ) return ;
113- setRequestedAccount ( null ) ;
114- prefilledFromLinkStateRef . current = false ;
115- setExposeEnvironment ( true ) ;
116- setPublishAgentActivity ( true ) ;
117- setDontShowAgain ( false ) ;
118- setStep ( canManageRelay && controller . linkState . target !== null ? "publish" : "devices" ) ;
119- setOpenForAccount ( requestedAccount ) ;
120- } , [
121- canManageRelay ,
122- controller . linkState . target ,
123- openForAccount ,
124- optOutAccounts ,
125- publishStepDecided ,
126- requestedAccount ,
127- sessionScopesKnown ,
128- ] ) ;
129-
130- useEffect ( ( ) => {
131- if ( openForAccount !== null && ( ! isSignedIn || userId !== openForAccount ) ) {
132- setOpenForAccount ( null ) ;
133- }
134- if ( requestedAccount !== null && ( ! isSignedIn || userId !== requestedAccount ) ) {
115+ } else if ( sessionScopesKnown && publishStepDecided ) {
135116 setRequestedAccount ( null ) ;
117+ setPrefilledFromLinkState ( false ) ;
118+ setExposeEnvironment ( true ) ;
119+ setPublishAgentActivity ( true ) ;
120+ setDontShowAgain ( false ) ;
121+ setStep ( canManageRelay && controller . linkState . target !== null ? "publish" : "devices" ) ;
122+ setOpenForAccount ( requestedAccount ) ;
136123 }
137- } , [ isSignedIn , openForAccount , requestedAccount , userId ] ) ;
124+ }
138125
139126 const linkStateData = controller . linkState . data ;
140- useEffect ( ( ) => {
141- if ( openForAccount === null || prefilledFromLinkStateRef . current || linkStateData === null ) {
142- return ;
143- }
144- prefilledFromLinkStateRef . current = true ;
127+ if ( openForAccount !== null && ! prefilledFromLinkState && linkStateData !== null ) {
128+ setPrefilledFromLinkState ( true ) ;
145129 if (
146130 linkStateData . linked &&
147131 linkStateData . linkRevoked !== true &&
@@ -150,7 +134,7 @@ function ConfiguredConnectOnboardingDialog() {
150134 setExposeEnvironment ( linkStateData . managedTunnelActive ?? linkStateData . linked ) ;
151135 setPublishAgentActivity ( linkStateData . publishAgentActivity ) ;
152136 }
153- } , [ linkStateData , openForAccount ] ) ;
137+ }
154138
155139 const complete = ( ) => {
156140 if ( isApplying ) return ;
0 commit comments