@@ -13,6 +13,10 @@ import {
1313 ISupportedWallet ,
1414} from '@creit.tech/stellar-wallets-kit' ;
1515
16+ // Singleton to prevent multiple initializations
17+ let globalWalletKit : StellarWalletsKit | null = null ;
18+ let isInitializing = false ;
19+
1620// POC Mode - No Stellar Wallets Kit initialization to avoid custom element conflicts
1721const POC_MODE = process . env . NODE_ENV === 'development' ;
1822
@@ -92,8 +96,34 @@ export const useWallet = (): UseWalletReturn => {
9296 useEffect ( ( ) => {
9397 const initializeWalletKit = async ( ) => {
9498 try {
99+ // Use singleton pattern to prevent multiple initializations
100+ if ( globalWalletKit ) {
101+ console . log ( '♻️ Using existing Stellar Wallets Kit instance' ) ;
102+ setWalletKit ( globalWalletKit ) ;
103+ const supportedWallets = await globalWalletKit . getSupportedWallets ( ) ;
104+ setAvailableWallets ( supportedWallets ) ;
105+ await checkConnectionStatus ( globalWalletKit ) ;
106+ return ;
107+ }
108+
109+ if ( isInitializing ) {
110+ console . log ( '⏳ Stellar Wallets Kit initialization already in progress...' ) ;
111+ return ;
112+ }
113+
114+ isInitializing = true ;
95115 console . log ( '🚀 Initializing Stellar Wallets Kit...' ) ;
96116
117+ // Check if custom elements are already defined (prevents HMR issues)
118+ const customElementsToCheck = [ 'stellar-wallets-modal' , 'stellar-wallets-button' , 'stellar-accounts-selector' ] ;
119+ const alreadyDefined = customElementsToCheck . some ( name => customElements . get ( name ) ) ;
120+
121+ if ( alreadyDefined ) {
122+ console . log ( '⚠️ Custom elements already defined, skipping initialization to prevent HMR conflicts' ) ;
123+ isInitializing = false ;
124+ return ;
125+ }
126+
97127 // Create wallet modules
98128 const modules = [
99129 new FreighterModule ( ) ,
@@ -108,6 +138,9 @@ export const useWallet = (): UseWalletReturn => {
108138 modules,
109139 } ) ;
110140
141+ // Store globally to prevent re-initialization
142+ globalWalletKit = kit ;
143+
111144 setWalletKit ( kit ) ;
112145
113146 // Get available wallets
@@ -132,9 +165,11 @@ export const useWallet = (): UseWalletReturn => {
132165
133166 // Check if already connected
134167 await checkConnectionStatus ( kit ) ;
168+ isInitializing = false ;
135169 } catch ( error ) {
136170 console . error ( '❌ Failed to initialize Stellar Wallets Kit:' , error ) ;
137171 console . log ( '🔄 Falling back to direct Freighter API...' ) ;
172+ isInitializing = false ;
138173
139174 // Fallback to direct Freighter detection
140175 const freighterDetected = typeof window !== 'undefined' &&
@@ -205,6 +240,16 @@ export const useWallet = (): UseWalletReturn => {
205240 } ;
206241
207242 initializeWalletKit ( ) ;
243+
244+ // Cleanup function to prevent memory leaks
245+ return ( ) => {
246+ if ( walletKit ) {
247+ console . log ( '🧹 Cleaning up Stellar Wallets Kit...' ) ;
248+ // The StellarWalletsKit doesn't have a cleanup method, but we can clear our state
249+ setWalletKit ( null ) ;
250+ setAvailableWallets ( [ ] ) ;
251+ }
252+ } ;
208253 } , [ ] ) ;
209254
210255 const connect = useCallback (
0 commit comments