enable button if the modal is exited in the middle of the flow

This commit is contained in:
2026-01-07 12:31:56 -04:00
parent 7965eab85b
commit 069fb31f82

View File

@@ -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,27 +216,47 @@ 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 (e.reason === 'rejected' || e.message?.includes('rejected')) {
const ws = useWalletStore();
const tx = ws.transaction
if (tx) {
tx.state = TransactionState.Rejected
ws.transaction = null
if (!resolved) {
resolved = true
unsubscribeProvider?.()
unsubscribeState?.()
if (e.reason === 'rejected' || e.message?.includes('rejected')) {
const ws = useWalletStore();
const tx = ws.transaction
if (tx) {
tx.state = TransactionState.Rejected
ws.transaction = null
}
resolve() // Don't reject on user cancellation
} else {
reject(e)
}
resolve() // Don't reject on user cancellation
} 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) {