@@ -81,40 +81,45 @@ export const WalletProvider: React.FC<WalletProviderProps> = ({ children }) => {
8181 const invokeSnap = useInvokeSnap ( ) ;
8282 const requestSnap = useRequestSnap ( ) ;
8383 const { error : metamaskError , setError : setMetamaskError } = useMetaMaskContext ( ) ;
84+ const isCheckingRef = React . useRef ( false ) ;
8485
8586 // Check for existing connection on mount
8687 const checkExistingConnection = async ( ) => {
87- // Prevent concurrent calls using functional state update for atomicity
88- let shouldProceed = false ;
89- setState ( prev => {
90- if ( prev . isCheckingConnection ) {
91- console . log ( 'Already checking connection, skipping...' ) ;
92- return prev ; // Already checking, don't proceed
93- }
94- shouldProceed = true ;
95- return { ...prev , isCheckingConnection : true , loadingStep : 'Checking existing connection...' } ;
96- } ) ;
88+ console . log ( '[checkExistingConnection] Function called, isCheckingRef:' , isCheckingRef . current ) ;
9789
98- if ( ! shouldProceed ) {
90+ // Use ref to prevent concurrent calls
91+ if ( isCheckingRef . current ) {
92+ console . log ( '[checkExistingConnection] Already checking (ref), skipping...' ) ;
9993 return ;
10094 }
10195
96+ isCheckingRef . current = true ;
97+ console . log ( '[checkExistingConnection] Set isCheckingRef to true' ) ;
98+
99+ setState ( prev => ( {
100+ ...prev ,
101+ isCheckingConnection : true ,
102+ loadingStep : 'Checking existing connection...' ,
103+ } ) ) ;
104+
102105 try {
103- console . log ( 'Starting connection check...' ) ;
106+ console . log ( '[checkExistingConnection] Starting connection check...' ) ;
104107
105108 // Check if we have a stored xpub in localStorage
106109 const storedXpub = localStorage . getItem ( STORAGE_KEYS . XPUB ) ;
107110 const storedNetwork = localStorage . getItem ( STORAGE_KEYS . NETWORK ) || 'dev-testnet' ;
108111
109- console . log ( 'Stored xpub:' , storedXpub ? 'Found' : 'Not found' ) ;
112+ console . log ( '[checkExistingConnection] Stored xpub:' , storedXpub ? 'Found' : 'Not found' ) ;
113+ console . log ( '[checkExistingConnection] Stored network:' , storedNetwork ) ;
110114
111115 if ( ! storedXpub ) {
112- console . log ( 'No stored xpub found, user needs to connect' ) ;
116+ console . log ( '[checkExistingConnection] No stored xpub found, user needs to connect' ) ;
113117 setState ( prev => ( {
114118 ...prev ,
115119 isCheckingConnection : false ,
116120 loadingStep : '' ,
117121 } ) ) ;
122+ console . log ( '[checkExistingConnection] State updated to show connect screen' ) ;
118123 return ;
119124 }
120125
@@ -208,6 +213,8 @@ export const WalletProvider: React.FC<WalletProviderProps> = ({ children }) => {
208213 error : 'Failed to reconnect. Please try connecting manually.' ,
209214 } ) ) ;
210215 } finally {
216+ console . log ( '[checkExistingConnection] Finally block - resetting isCheckingRef' ) ;
217+ isCheckingRef . current = false ;
211218 setState ( prev => ( { ...prev , isCheckingConnection : false } ) ) ;
212219 }
213220 } ;
@@ -363,21 +370,49 @@ export const WalletProvider: React.FC<WalletProviderProps> = ({ children }) => {
363370
364371 // Check for existing connection on mount
365372 React . useEffect ( ( ) => {
373+ console . log ( '[useEffect] Mounting WalletProvider' ) ;
374+ let timeoutId : NodeJS . Timeout ;
375+
366376 // Add a small delay to avoid race condition with React StrictMode double-mount
367377 const timer = setTimeout ( ( ) => {
368- checkExistingConnection ( ) . catch ( ( error ) => {
369- console . error ( 'Connection check failed:' , error ) ;
370- // Ensure we reset the checking state on error
378+ console . log ( '[useEffect] Timer fired, calling checkExistingConnection' ) ;
379+
380+ // Set a timeout to prevent infinite hanging
381+ timeoutId = setTimeout ( ( ) => {
382+ console . error ( '[useEffect] Connection check timed out after 10 seconds' ) ;
371383 setState ( prev => ( {
372384 ...prev ,
373385 isCheckingConnection : false ,
374386 loadingStep : '' ,
375- error : 'Failed to check connection' ,
387+ error : null ,
376388 } ) ) ;
377- } ) ;
389+ } , 10000 ) ;
390+
391+ checkExistingConnection ( )
392+ . then ( ( ) => {
393+ console . log ( '[useEffect] checkExistingConnection completed successfully' ) ;
394+ } )
395+ . catch ( ( error ) => {
396+ console . error ( '[useEffect] Connection check failed with error:' , error ) ;
397+ // Ensure we reset the checking state on error
398+ setState ( prev => ( {
399+ ...prev ,
400+ isCheckingConnection : false ,
401+ loadingStep : '' ,
402+ error : 'Failed to check connection' ,
403+ } ) ) ;
404+ } )
405+ . finally ( ( ) => {
406+ console . log ( '[useEffect] checkExistingConnection finally block' ) ;
407+ clearTimeout ( timeoutId ) ;
408+ } ) ;
378409 } , 100 ) ;
379410
380- return ( ) => clearTimeout ( timer ) ;
411+ return ( ) => {
412+ console . log ( '[useEffect] Unmounting WalletProvider' ) ;
413+ clearTimeout ( timer ) ;
414+ if ( timeoutId ) clearTimeout ( timeoutId ) ;
415+ } ;
381416 } , [ ] ) ;
382417
383418 // Sync MetaMask errors to wallet state
0 commit comments