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 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,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
|
// Open the modal
|
||||||
web3modal.open().catch((e) => {
|
web3modal.open().catch((e) => {
|
||||||
unsubscribe()
|
if (!resolved) {
|
||||||
if (e.reason === 'rejected' || e.message?.includes('rejected')) {
|
resolved = true
|
||||||
const ws = useWalletStore();
|
unsubscribeProvider?.()
|
||||||
const tx = ws.transaction
|
unsubscribeState?.()
|
||||||
if (tx) {
|
|
||||||
tx.state = TransactionState.Rejected
|
if (e.reason === 'rejected' || e.message?.includes('rejected')) {
|
||||||
ws.transaction = null
|
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
|
// 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user