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 a promise that resolves when connection is complete
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let resolved = false 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) console.log('Provider state changed:', newState)
if (newState.provider && !resolved) { if (newState.provider && !resolved) {
resolved = true resolved = true
unsubscribe() unsubscribeProvider?.()
unsubscribeState?.()
try { try {
const walletProvider = newState.provider 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 // Open the modal
web3modal.open().catch((e) => { web3modal.open().catch((e) => {
unsubscribe() if (!resolved) {
resolved = true
unsubscribeProvider?.()
unsubscribeState?.()
if (e.reason === 'rejected' || e.message?.includes('rejected')) { if (e.reason === 'rejected' || e.message?.includes('rejected')) {
const ws = useWalletStore(); const ws = useWalletStore();
const tx = ws.transaction const tx = ws.transaction
@@ -226,13 +246,17 @@ export async function connectWallet(chainId) {
} else { } else {
reject(e) reject(e)
} }
}
}) })
// Timeout after 60 seconds // Timeout after 60 seconds
setTimeout(() => { setTimeout(() => {
if (!resolved) { if (!resolved) {
unsubscribe() resolved = true
reject(new Error('Connection timeout')) unsubscribeProvider?.()
unsubscribeState?.()
console.log('Connection timeout')
resolve() // Resolve instead of reject so button re-enables
} }
}, 60000) }, 60000)
}) })
@@ -709,6 +733,7 @@ export async function addNetwork(chainId) {
} }
export async function addNetworkAndConnectWallet(chainId) { export async function addNetworkAndConnectWallet(chainId) {
try { try {
await connectWallet(chainId) await connectWallet(chainId)
} catch (e) { } catch (e) {