Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
2604cdfd
Unverified
Commit
2604cdfd
authored
Sep 08, 2022
by
vignesh mohankumar
Committed by
GitHub
Sep 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: only initialize using chain query (#4567)
parent
94dc3898
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
35 deletions
+8
-35
useSyncChainQuery.ts
src/hooks/useSyncChainQuery.ts
+8
-35
No files found.
src/hooks/useSyncChainQuery.ts
View file @
2604cdfd
import
{
useWeb3React
}
from
'
@web3-react/core
'
import
{
useWeb3React
}
from
'
@web3-react/core
'
import
{
CHAIN_IDS_TO_NAMES
,
SupportedChainId
}
from
'
constants/chains
'
import
{
CHAIN_IDS_TO_NAMES
}
from
'
constants/chains
'
import
{
ParsedQs
}
from
'
qs
'
import
{
ParsedQs
}
from
'
qs
'
import
{
useCallback
,
useEffect
,
useState
}
from
'
react
'
import
{
useEffect
,
useState
}
from
'
react
'
import
{
useLocation
,
useNavigate
}
from
'
react-router-dom
'
import
{
useSearchParams
}
from
'
react-router-dom
'
import
{
replaceURLParam
}
from
'
utils/routes
'
import
useParsedQueryString
from
'
./useParsedQueryString
'
import
useParsedQueryString
from
'
./useParsedQueryString
'
import
usePrevious
from
'
./usePrevious
'
import
usePrevious
from
'
./usePrevious
'
...
@@ -22,15 +21,8 @@ function getParsedChainId(parsedQs?: ParsedQs) {
...
@@ -22,15 +21,8 @@ function getParsedChainId(parsedQs?: ParsedQs) {
return
getChainIdFromName
(
chain
)
return
getChainIdFromName
(
chain
)
}
}
function
getChainNameFromId
(
id
:
string
|
number
)
{
// casting here may not be right but fine to return undefined if it's not a supported chain ID
return
CHAIN_IDS_TO_NAMES
[
id
as
SupportedChainId
]
||
''
}
export
default
function
useSyncChainQuery
()
{
export
default
function
useSyncChainQuery
()
{
const
{
chainId
,
isActive
}
=
useWeb3React
()
const
{
chainId
,
isActive
}
=
useWeb3React
()
const
navigate
=
useNavigate
()
const
{
search
}
=
useLocation
()
const
parsedQs
=
useParsedQueryString
()
const
parsedQs
=
useParsedQueryString
()
const
urlChainId
=
getParsedChainId
(
parsedQs
)
const
urlChainId
=
getParsedChainId
(
parsedQs
)
...
@@ -46,35 +38,16 @@ export default function useSyncChainQuery() {
...
@@ -46,35 +38,16 @@ export default function useSyncChainQuery() {
}
}
},
[
chainId
,
previousChainId
])
},
[
chainId
,
previousChainId
])
const
replaceURLChainParam
=
useCallback
(()
=>
{
const
[
searchParams
,
setSearchParams
]
=
useSearchParams
()
if
(
chainId
)
{
navigate
({
search
:
replaceURLParam
(
search
,
'
chain
'
,
getChainNameFromId
(
chainId
))
},
{
replace
:
true
})
}
},
[
chainId
,
search
,
navigate
])
const
chainQueryUnpopulated
=
!
urlChainId
&&
chainId
const
chainChanged
=
chainId
!==
previousChainId
const
chainQueryStale
=
urlChainId
!==
chainId
const
chainQueryManuallyUpdated
=
urlChainId
&&
urlChainId
!==
previousUrlChainId
&&
isActive
const
chainQueryManuallyUpdated
=
urlChainId
&&
urlChainId
!==
previousUrlChainId
&&
isActive
return
useEffect
(()
=>
{
return
useEffect
(()
=>
{
if
(
chainQueryUnpopulated
)
{
if
(
chainQueryManuallyUpdated
)
{
// If there is no chain query param, set it to the current chain
replaceURLChainParam
()
}
else
if
(
chainChanged
&&
chainQueryStale
)
{
// If the chain changed but the query param is stale, update to the current chain
replaceURLChainParam
()
}
else
if
(
chainQueryManuallyUpdated
)
{
// If the query param changed, and the chain didn't change, then activate the new chain
// If the query param changed, and the chain didn't change, then activate the new chain
selectChain
(
urlChainId
)
selectChain
(
urlChainId
)
searchParams
.
delete
(
'
chain
'
)
setSearchParams
(
searchParams
)
}
}
},
[
},
[
chainQueryManuallyUpdated
,
urlChainId
,
selectChain
,
searchParams
,
setSearchParams
])
chainQueryUnpopulated
,
chainChanged
,
chainQueryStale
,
chainQueryManuallyUpdated
,
urlChainId
,
selectChain
,
replaceURLChainParam
,
])
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment