Commit b0b61f88 authored by Jordan Frankfurt's avatar Jordan Frankfurt Committed by GitHub

fix: refactors the way chainId is accessed in some places (#4818)

fix: refactors the way some data is accessed
parent 5d4b25f4
import { useWeb3React } from '@web3-react/core'
import clsx from 'clsx' import clsx from 'clsx'
import { L2NetworkLogo, LogoContainer } from 'components/Tokens/TokenTable/TokenRow' import { L2NetworkLogo, LogoContainer } from 'components/Tokens/TokenTable/TokenRow'
import { VerifiedIcon } from 'components/TokenSafety/TokenSafetyIcon' import { VerifiedIcon } from 'components/TokenSafety/TokenSafetyIcon'
import { getChainInfo } from 'constants/chainInfo' import { getChainInfo } from 'constants/chainInfo'
import { getTokenDetailsURL, useGlobalChainId } from 'graphql/data/util' import { getTokenDetailsURL } from 'graphql/data/util'
import uriToHttp from 'lib/utils/uriToHttp' import uriToHttp from 'lib/utils/uriToHttp'
import { Box } from 'nft/components/Box' import { Box } from 'nft/components/Box'
import { Column, Row } from 'nft/components/Flex' import { Column, Row } from 'nft/components/Flex'
...@@ -103,10 +104,10 @@ export const CollectionRow = ({ ...@@ -103,10 +104,10 @@ export const CollectionRow = ({
} }
function useBridgedAddress(token: FungibleToken): [string | undefined, number | undefined, string | undefined] { function useBridgedAddress(token: FungibleToken): [string | undefined, number | undefined, string | undefined] {
const globalChainId = useGlobalChainId() const { chainId: connectedChainId } = useWeb3React()
const bridgedAddress = globalChainId ? token.extensions?.bridgeInfo?.[globalChainId]?.tokenAddress : undefined const bridgedAddress = connectedChainId ? token.extensions?.bridgeInfo?.[connectedChainId]?.tokenAddress : undefined
if (bridgedAddress && globalChainId) { if (bridgedAddress && connectedChainId) {
return [bridgedAddress, globalChainId, getChainInfo(globalChainId)?.circleLogoUrl] return [bridgedAddress, connectedChainId, getChainInfo(connectedChainId)?.circleLogoUrl]
} }
return [undefined, undefined, undefined] return [undefined, undefined, undefined]
} }
......
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { useWeb3React } from '@web3-react/core'
import Web3Status from 'components/Web3Status' import Web3Status from 'components/Web3Status'
import { NftVariant, useNftFlag } from 'featureFlags/flags/nft' import { NftVariant, useNftFlag } from 'featureFlags/flags/nft'
import { useGlobalChainName } from 'graphql/data/util' import { chainIdToBackendName } from 'graphql/data/util'
import { Box } from 'nft/components/Box' import { Box } from 'nft/components/Box'
import { Row } from 'nft/components/Flex' import { Row } from 'nft/components/Flex'
import { UniIcon } from 'nft/components/icons' import { UniIcon } from 'nft/components/icons'
...@@ -37,7 +38,8 @@ const MenuItem = ({ href, id, isActive, children }: MenuItemProps) => { ...@@ -37,7 +38,8 @@ const MenuItem = ({ href, id, isActive, children }: MenuItemProps) => {
const PageTabs = () => { const PageTabs = () => {
const { pathname } = useLocation() const { pathname } = useLocation()
const nftFlag = useNftFlag() const nftFlag = useNftFlag()
const chainName = useGlobalChainName() const { chainId: connectedChainId } = useWeb3React()
const chainName = chainIdToBackendName(connectedChainId)
const isPoolActive = const isPoolActive =
pathname.startsWith('/pool') || pathname.startsWith('/pool') ||
......
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { useGlobalChainName } from 'graphql/data/util' import { useWeb3React } from '@web3-react/core'
import { chainIdToBackendName } from 'graphql/data/util'
import { X } from 'react-feather' import { X } from 'react-feather'
import { Link, useNavigate } from 'react-router-dom' import { Link, useNavigate } from 'react-router-dom'
import { useShowTokensPromoBanner } from 'state/user/hooks' import { useShowTokensPromoBanner } from 'state/user/hooks'
...@@ -62,7 +63,8 @@ export default function TokensBanner() { ...@@ -62,7 +63,8 @@ export default function TokensBanner() {
const theme = useTheme() const theme = useTheme()
const [showTokensPromoBanner, setShowTokensPromoBanner] = useShowTokensPromoBanner() const [showTokensPromoBanner, setShowTokensPromoBanner] = useShowTokensPromoBanner()
const navigate = useNavigate() const navigate = useNavigate()
const chainName = useGlobalChainName().toLowerCase() const { chainId: connectedChainId } = useWeb3React()
const chainName = chainIdToBackendName(connectedChainId).toLowerCase()
const navigateToExplorePage = () => { const navigateToExplorePage = () => {
navigate(`/tokens/${chainName}`) navigate(`/tokens/${chainName}`)
......
import { NATIVE_CHAIN_ID } from 'analytics/constants' import { NATIVE_CHAIN_ID } from 'analytics/constants'
import { SupportedChainId } from 'constants/chains' import { SupportedChainId } from 'constants/chains'
import { ZERO_ADDRESS } from 'constants/misc' import { ZERO_ADDRESS } from 'constants/misc'
import { useAppSelector } from 'state/hooks'
import { Chain, HistoryDuration } from './__generated__/TokenQuery.graphql' import { Chain, HistoryDuration } from './__generated__/TokenQuery.graphql'
...@@ -41,13 +40,10 @@ export const CHAIN_ID_TO_BACKEND_NAME: { [key: number]: Chain } = { ...@@ -41,13 +40,10 @@ export const CHAIN_ID_TO_BACKEND_NAME: { [key: number]: Chain } = {
[SupportedChainId.OPTIMISM_GOERLI]: 'OPTIMISM', [SupportedChainId.OPTIMISM_GOERLI]: 'OPTIMISM',
} }
export function useGlobalChainId() { export function chainIdToBackendName(chainId: number | undefined) {
return useAppSelector((state) => state.application.chainId) return chainId && CHAIN_ID_TO_BACKEND_NAME[chainId]
} ? CHAIN_ID_TO_BACKEND_NAME[chainId]
: CHAIN_ID_TO_BACKEND_NAME[SupportedChainId.MAINNET]
export function useGlobalChainName() {
const chainId = useGlobalChainId()
return chainId && CHAIN_ID_TO_BACKEND_NAME[chainId] ? CHAIN_ID_TO_BACKEND_NAME[chainId] : 'ETHEREUM'
} }
export const URL_CHAIN_PARAM_TO_BACKEND: { [key: string]: Chain } = { export const URL_CHAIN_PARAM_TO_BACKEND: { [key: string]: Chain } = {
......
import { useWeb3React } from '@web3-react/core'
import { Chain } from 'graphql/data/__generated__/TokenQuery.graphql' import { Chain } from 'graphql/data/__generated__/TokenQuery.graphql'
import { useGlobalChainName } from 'graphql/data/util' import { chainIdToBackendName } from 'graphql/data/util'
import { useEffect, useRef } from 'react' import { useEffect, useRef } from 'react'
export const useOnGlobalChainSwitch = (callback: (chain: Chain) => void) => { export const useOnGlobalChainSwitch = (callback: (chain: Chain) => void) => {
const globalChainName = useGlobalChainName() const { chainId: connectedChainId } = useWeb3React()
const globalChainName = chainIdToBackendName(connectedChainId)
const prevGlobalChainRef = useRef(globalChainName) const prevGlobalChainRef = useRef(globalChainName)
useEffect(() => { useEffect(() => {
if (prevGlobalChainRef.current !== globalChainName) { if (prevGlobalChainRef.current !== globalChainName) {
......
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { useWeb3React } from '@web3-react/core'
import { PageName } from 'analytics/constants' import { PageName } from 'analytics/constants'
import { Trace } from 'analytics/Trace' import { Trace } from 'analytics/Trace'
import { MAX_WIDTH_MEDIA_BREAKPOINT, MEDIUM_MEDIA_BREAKPOINT } from 'components/Tokens/constants' import { MAX_WIDTH_MEDIA_BREAKPOINT, MEDIUM_MEDIA_BREAKPOINT } from 'components/Tokens/constants'
...@@ -9,7 +10,7 @@ import SearchBar from 'components/Tokens/TokenTable/SearchBar' ...@@ -9,7 +10,7 @@ import SearchBar from 'components/Tokens/TokenTable/SearchBar'
import TimeSelector from 'components/Tokens/TokenTable/TimeSelector' import TimeSelector from 'components/Tokens/TokenTable/TimeSelector'
import TokenTable, { LoadingTokenTable } from 'components/Tokens/TokenTable/TokenTable' import TokenTable, { LoadingTokenTable } from 'components/Tokens/TokenTable/TokenTable'
import { FavoriteTokensVariant, useFavoriteTokensFlag } from 'featureFlags/flags/favoriteTokens' import { FavoriteTokensVariant, useFavoriteTokensFlag } from 'featureFlags/flags/favoriteTokens'
import { isValidBackendChainName, useGlobalChainName } from 'graphql/data/util' import { chainIdToBackendName, isValidBackendChainName } from 'graphql/data/util'
import { useOnGlobalChainSwitch } from 'hooks/useGlobalChainSwitch' import { useOnGlobalChainSwitch } from 'hooks/useGlobalChainSwitch'
import { useResetAtom } from 'jotai/utils' import { useResetAtom } from 'jotai/utils'
import { useEffect } from 'react' import { useEffect } from 'react'
...@@ -71,18 +72,19 @@ const Tokens = () => { ...@@ -71,18 +72,19 @@ const Tokens = () => {
const resetFilterString = useResetAtom(filterStringAtom) const resetFilterString = useResetAtom(filterStringAtom)
const location = useLocation() const location = useLocation()
const navigate = useNavigate() const navigate = useNavigate()
const { chainName } = useParams<{ chainName?: string }>() const { chainName: chainNameParam } = useParams<{ chainName?: string }>()
const globalChainName = useGlobalChainName() const { chainId: connectedChainId } = useWeb3React()
const connectedChainName = chainIdToBackendName(connectedChainId)
useEffect(() => { useEffect(() => {
resetFilterString() resetFilterString()
}, [location, resetFilterString]) }, [location, resetFilterString])
useEffect(() => { useEffect(() => {
if (!chainName) { if (!chainNameParam) {
navigate(`/tokens/${globalChainName.toLowerCase()}`) navigate(`/tokens/${connectedChainName.toLowerCase()}`)
} }
}, [chainName, globalChainName, navigate]) }, [chainNameParam, connectedChainName, navigate])
useOnGlobalChainSwitch((chain) => { useOnGlobalChainSwitch((chain) => {
if (isValidBackendChainName(chain)) { if (isValidBackendChainName(chain)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment