enable button if the modal is exited in the middle of the flow
This commit is contained in:
@@ -173,13 +173,17 @@ export async function connectWallet(chainId) {
|
||||
// Return a promise that resolves when connection is complete
|
||||
return new Promise((resolve, reject) => {
|
||||
let resolved = false
|
||||
let unsubscribeProvider = null
|
||||
let unsubscribeState = null
|
||||
|
||||
const unsubscribe = web3modal.subscribeProvider(async (newState) => {
|
||||
// Subscribe to provider changes (fires when wallet connects)
|
||||
unsubscribeProvider = web3modal.subscribeProvider(async (newState) => {
|
||||
console.log('Provider state changed:', newState)
|
||||
|
||||
if (newState.provider && !resolved) {
|
||||
resolved = true
|
||||
unsubscribe()
|
||||
unsubscribeProvider?.()
|
||||
unsubscribeState?.()
|
||||
|
||||
try {
|
||||
const walletProvider = newState.provider
|
||||
@@ -212,9 +216,25 @@ export async function connectWallet(chainId) {
|
||||
}
|
||||
})
|
||||
|
||||
// Subscribe to modal state (fires when modal opens/closes)
|
||||
unsubscribeState = web3modal.subscribeState((state) => {
|
||||
// If modal closes without connection, resolve the promise
|
||||
if (state.open === false && !resolved) {
|
||||
console.log('Modal closed without connection')
|
||||
resolved = true
|
||||
unsubscribeProvider?.()
|
||||
unsubscribeState?.()
|
||||
resolve() // Resolve (not reject) so the button re-enables
|
||||
}
|
||||
})
|
||||
|
||||
// Open the modal
|
||||
web3modal.open().catch((e) => {
|
||||
unsubscribe()
|
||||
if (!resolved) {
|
||||
resolved = true
|
||||
unsubscribeProvider?.()
|
||||
unsubscribeState?.()
|
||||
|
||||
if (e.reason === 'rejected' || e.message?.includes('rejected')) {
|
||||
const ws = useWalletStore();
|
||||
const tx = ws.transaction
|
||||
@@ -226,13 +246,17 @@ export async function connectWallet(chainId) {
|
||||
} else {
|
||||
reject(e)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Timeout after 60 seconds
|
||||
setTimeout(() => {
|
||||
if (!resolved) {
|
||||
unsubscribe()
|
||||
reject(new Error('Connection timeout'))
|
||||
resolved = true
|
||||
unsubscribeProvider?.()
|
||||
unsubscribeState?.()
|
||||
console.log('Connection timeout')
|
||||
resolve() // Resolve instead of reject so button re-enables
|
||||
}
|
||||
}, 60000)
|
||||
})
|
||||
@@ -709,6 +733,7 @@ export async function addNetwork(chainId) {
|
||||
}
|
||||
|
||||
export async function addNetworkAndConnectWallet(chainId) {
|
||||
|
||||
try {
|
||||
await connectWallet(chainId)
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user