11import { useState , useCallback , useEffect , useRef } from 'react' ;
2- import { networkService , NetworkInfo } from '@/services/networkService' ;
2+ import { networkService } from '@/services/networkService' ;
3+
4+ // Minimal local type for the network info returned by the backend.
5+ // Keep this small to avoid coupling to a non-exported service type.
6+ type NetworkInfo = {
7+ network : string ;
8+ chainId ?: number | null ;
9+ } ;
310
411/** How often to re-check the network while a wallet is connected (ms). */
512const POLL_INTERVAL_MS = 10_000 ;
613
714export type NetworkStatus =
8- | 'idle' // no wallet connected
9- | 'loading' // first fetch in progress
10- | 'match' // wallet network matches .env config
11- | 'mismatch' // wallet is on the wrong network
12- | 'error' ; // fetch failed
15+ | 'idle' // no wallet connected
16+ | 'loading' // first fetch in progress
17+ | 'match' // wallet network matches .env config
18+ | 'mismatch' // wallet is on the wrong network
19+ | 'error' ; // fetch failed
1320
1421/**
1522 * useNetworkCheck — polls the backend to detect whether the connected wallet
@@ -23,8 +30,7 @@ export type NetworkStatus =
2330 * - `recheck` — manually trigger a re-check immediately
2431 */
2532export function useNetworkCheck ( address : string | null ) {
26- const expectedNetwork =
27- process . env . NEXT_PUBLIC_STELLAR_NETWORK ?? 'testnet' ;
33+ const expectedNetwork = process . env . NEXT_PUBLIC_STELLAR_NETWORK ?? 'testnet' ;
2834
2935 const [ walletNetwork , setWalletNetwork ] = useState < NetworkInfo | null > ( null ) ;
3036 const [ status , setStatus ] = useState < NetworkStatus > ( 'idle' ) ;
@@ -33,9 +39,8 @@ export function useNetworkCheck(address: string | null) {
3339
3440 const check = useCallback ( async ( ) => {
3541 if ( ! address ) {
36-
3742 setStatus ( 'idle' ) ;
38-
43+
3944 setWalletNetwork ( null ) ;
4045 return ;
4146 }
@@ -47,18 +52,41 @@ export function useNetworkCheck(address: string | null) {
4752 try {
4853 const response = await networkService . getWalletNetwork ( address ) ;
4954 if ( response . success && response . data ) {
50- setWalletNetwork ( response . data ) ;
55+ const incoming : NetworkInfo = response . data ;
56+
57+ // Determine match based on configured expected network (case-insensitive).
5158 const match =
52- response . data . network . toLowerCase ( ) ===
53- expectedNetwork . toLowerCase ( ) ;
54- setStatus ( match ? 'match' : 'mismatch' ) ;
59+ incoming . network ?. toLowerCase ( ) === expectedNetwork . toLowerCase ( ) ;
60+
61+ // Only update state when something actually changed. Use functional
62+ // updates to avoid reading stale values from the closure and to
63+ // prevent setting new object identities when the logical data is
64+ // identical — this prevents unnecessary re-renders (the likely
65+ // cause of the infinite loop reported).
66+ setWalletNetwork ( ( prev ) => {
67+ if (
68+ prev &&
69+ prev . network ?. toLowerCase ( ) === incoming . network ?. toLowerCase ( ) &&
70+ ( prev . chainId ?? null ) === ( incoming . chainId ?? null )
71+ ) {
72+ return prev ;
73+ }
74+ return incoming ;
75+ } ) ;
76+
77+ setStatus ( ( prev ) => {
78+ const newStatus : typeof status = match ? 'match' : 'mismatch' ;
79+ if ( prev === newStatus ) return prev ;
80+ return newStatus ;
81+ } ) ;
5582 } else {
5683 setError ( response . message || 'Failed to check network' ) ;
5784 setStatus ( 'error' ) ;
5885 }
5986 } catch ( err : any ) {
6087 setError (
61- err . response ?. data ?. message || 'An error occurred while checking network'
88+ err . response ?. data ?. message ||
89+ 'An error occurred while checking network'
6290 ) ;
6391 setStatus ( 'error' ) ;
6492 }
@@ -71,7 +99,7 @@ export function useNetworkCheck(address: string | null) {
7199 if ( ! address ) {
72100 // eslint-disable-next-line react-hooks/set-state-in-effect
73101 setStatus ( 'idle' ) ;
74-
102+
75103 setWalletNetwork ( null ) ;
76104 return ;
77105 }
@@ -91,4 +119,4 @@ export function useNetworkCheck(address: string | null) {
91119 error,
92120 recheck : check ,
93121 } ;
94- }
122+ }
0 commit comments