fix: use chain query param properly when activating (#6114)

* fix: use `chain` query param properly when activating

* move

* Update src/hooks/useSyncChainQuery.ts

Co-authored-by: Jordan Frankfurt <jordanwfrankfurt@gmail.com>

---------

Co-authored-by: Jordan Frankfurt <jordanwfrankfurt@gmail.com>
This commit is contained in:
Vignesh Mohankumar 2023-03-09 22:24:14 -05:00 committed by GitHub
parent a90318cbe9
commit ae559d164a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,6 +32,8 @@ export default function useSyncChainQuery() {
// Can't use `usePrevious` because `chainId` can be undefined while activating.
const [previousChainId, setPreviousChainId] = useState<number | undefined>(undefined)
const [nextChainId, setNextChainId] = useState<number | undefined>(undefined)
useEffect(() => {
if (chainId && chainId !== previousChainId) {
setPreviousChainId(chainId)
@ -40,14 +42,21 @@ export default function useSyncChainQuery() {
const [searchParams, setSearchParams] = useSearchParams()
const chainQueryManuallyUpdated = urlChainId && urlChainId !== previousUrlChainId && isActive
useEffect(() => {
const chainQueryManuallyUpdated = urlChainId && urlChainId !== previousUrlChainId
if (chainQueryManuallyUpdated) {
setNextChainId(urlChainId)
}
}, [previousUrlChainId, urlChainId])
return useEffect(() => {
if (chainQueryManuallyUpdated) {
if (nextChainId && isActive) {
// If the query param changed, and the chain didn't change, then activate the new chain
selectChain(urlChainId)
searchParams.delete('chain')
setSearchParams(searchParams)
selectChain(nextChainId).then(() => {
searchParams.delete('chain')
setSearchParams(searchParams)
setNextChainId(undefined)
})
}
}, [chainQueryManuallyUpdated, urlChainId, selectChain, searchParams, setSearchParams])
}, [nextChainId, urlChainId, selectChain, searchParams, setSearchParams, isActive])
}