fix: remove error popup when switching chains from URL param (#6866)

This commit is contained in:
eddie 2023-07-03 17:13:12 -07:00 committed by GitHub
parent 7b8114b401
commit 5640c115de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 20 deletions

@ -119,3 +119,12 @@ describe('network switching', () => {
cy.get(`#swap-currency-output .token-symbol-container`).should('contain.text', 'Select token')
})
})
describe('network switching from URL param', () => {
it('should switch network from URL param', () => {
cy.visit('/swap?chain=polygon', { ethereum: 'hardhat' })
cy.get(getTestSelector('web3-status-connected'))
cy.wait('@wallet_switchEthereumChain')
waitsForActiveChain('Polygon')
})
})

@ -22,7 +22,7 @@ export default function useSelectChain() {
try {
await switchChain(connector, targetChain)
} catch (error) {
if (!didUserReject(connection, error)) {
if (!didUserReject(connection, error) && error.code !== -32002 /* request already pending */) {
console.error('Failed to switch networks', error)
dispatch(addPopup({ content: { failedSwitchNetwork: targetChain }, key: 'failed-network-switch' }))
}

@ -1,11 +1,10 @@
import { useWeb3React } from '@web3-react/core'
import { CHAIN_IDS_TO_NAMES } from 'constants/chains'
import { ParsedQs } from 'qs'
import { useEffect, useState } from 'react'
import { useEffect } from 'react'
import { useSearchParams } from 'react-router-dom'
import useParsedQueryString from './useParsedQueryString'
import usePrevious from './usePrevious'
import useSelectChain from './useSelectChain'
function getChainIdFromName(name: string) {
@ -26,28 +25,14 @@ export default function useSyncChainQuery() {
const parsedQs = useParsedQueryString()
const urlChainId = getParsedChainId(parsedQs)
const previousUrlChainId = usePrevious(urlChainId)
const selectChain = useSelectChain()
// Can't use `usePrevious` because `chainId` can be undefined while activating.
const [previousChainId, setPreviousChainId] = useState<number | undefined>(undefined)
useEffect(() => {
if (chainId && chainId !== previousChainId) {
setPreviousChainId(chainId)
}
}, [chainId, previousChainId])
const [searchParams, setSearchParams] = useSearchParams()
const chainQueryManuallyUpdated = urlChainId && urlChainId !== previousUrlChainId && isActive
return useEffect(() => {
if (chainQueryManuallyUpdated) {
// If the query param changed, and the chain didn't change, then activate the new chain
useEffect(() => {
if (isActive && urlChainId && chainId !== urlChainId) {
selectChain(urlChainId)
searchParams.delete('chain')
setSearchParams(searchParams)
}
}, [chainQueryManuallyUpdated, urlChainId, selectChain, searchParams, setSearchParams])
}, [urlChainId, selectChain, searchParams, setSearchParams, isActive, chainId])
}