Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix routes when network toggled #223

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import NetworkDropdown from 'components/Menu/NetworkDropdown'
import { useActiveNetworkVersion } from 'state/application/hooks'
import { networkPrefix } from 'utils/networkPrefix'
import { AutoColumn } from 'components/Column'
import { SUPPORTED_NETWORK_PATH_NAMES } from '../../constants/networks'

const HeaderFrame = styled.div`
display: grid;
Expand Down Expand Up @@ -71,16 +72,19 @@ const Title = styled(NavLink)`
pointer-events: auto;
justify-self: flex-start;
margin-right: 12px;

:hover {
cursor: pointer;
}

${({ theme }) => theme.mediaWidth.upToSmall`
justify-self: center;
`};
`

const UniIcon = styled.div`
transition: transform 0.3s ease;

:hover {
transform: rotate(-5deg);
}
Expand Down Expand Up @@ -169,7 +173,7 @@ export default function Header() {
<StyledNavLink
id={`pool-nav-link`}
to={networkPrefix(activeNewtork)}
isActive={(match, { pathname }) => pathname === '/'}
isActive={(match, { pathname }) => SUPPORTED_NETWORK_PATH_NAMES.includes(pathname)}
>
Overview
</StyledNavLink>
Expand Down
18 changes: 16 additions & 2 deletions src/components/Menu/NetworkDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { useState, useRef, useMemo } from 'react'
import { useLocation } from 'react-router-dom'
import { RowFixed, RowBetween } from 'components/Row'
import { PolygonNetworkInfo, SUPPORTED_NETWORK_VERSIONS } from 'constants/networks'
import useTheme from 'hooks/useTheme'
import React, { useState, useRef } from 'react'
import { ChevronDown } from 'react-feather'
import { useActiveNetworkVersion } from 'state/application/hooks'
import styled from 'styled-components'
import { StyledInternalLink, TYPE } from 'theme'
import { useOnClickOutside } from 'hooks/useOnClickOutside'
import { AutoColumn } from 'components/Column'
import { EthereumNetworkInfo } from '../../constants/networks'
import { HEADER_EXTRA_TABS_ROUTES } from '../../constants/tabs'

const Container = styled.div`
position: relative;
Expand Down Expand Up @@ -55,6 +57,7 @@ const NetworkRow = styled(RowBetween)<{ active?: boolean; disabled?: boolean }>`
background-color: ${({ theme, active }) => (active ? theme.bg2 : theme.bg1)};
border-radius: 8px;
opacity: ${({ disabled }) => (disabled ? '0.5' : 1)};

:hover {
cursor: ${({ disabled }) => (disabled ? 'initial' : 'pointer')};
opacity: ${({ disabled }) => (disabled ? 0.5 : 0.7)};
Expand Down Expand Up @@ -84,12 +87,18 @@ const GreenDot = styled.div`
export default function NetworkDropdown() {
const [activeNetwork] = useActiveNetworkVersion()
const theme = useTheme()
const { pathname } = useLocation()

const [showMenu, setShowMenu] = useState(false)

const node = useRef<HTMLDivElement>(null)
useOnClickOutside(node, () => setShowMenu(false))

const currentTabPath = useMemo(
() => Object.values(HEADER_EXTRA_TABS_ROUTES).find((extraTab) => pathname.includes(extraTab)) || '',
[pathname]
)

return (
<Container ref={node}>
<Wrapper onClick={() => setShowMenu(!showMenu)}>
Expand All @@ -114,7 +123,12 @@ export default function NetworkDropdown() {
</TYPE.main>
{SUPPORTED_NETWORK_VERSIONS.map((n) => {
return (
<StyledInternalLink key={n.id} to={`${n === EthereumNetworkInfo ? '' : '/' + n.route}/`}>
<StyledInternalLink
key={n.id}
to={`${
n.name === EthereumNetworkInfo.name ? `/${currentTabPath}` : `${n.pathName}${currentTabPath}`
}`}
>
<NetworkRow
onClick={() => {
setShowMenu(false)
Expand Down
12 changes: 12 additions & 0 deletions src/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum SupportedNetwork {
export type NetworkInfo = {
id: SupportedNetwork
route: string
pathName: string
name: string
imageURL: string
bgColor: string
Expand All @@ -24,6 +25,7 @@ export type NetworkInfo = {
export const EthereumNetworkInfo: NetworkInfo = {
id: SupportedNetwork.ETHEREUM,
route: '',
pathName: '/',
name: 'Ethereum',
bgColor: '#fc077d',
primaryColor: '#fc077d',
Expand All @@ -34,6 +36,7 @@ export const EthereumNetworkInfo: NetworkInfo = {
export const ArbitrumNetworkInfo: NetworkInfo = {
id: SupportedNetwork.ARBITRUM,
route: 'arbitrum',
pathName: '/arbitrum/',
name: 'Arbitrum',
imageURL: ARBITRUM_LOGO_URL,
bgColor: '#0A294B',
Expand All @@ -45,6 +48,7 @@ export const ArbitrumNetworkInfo: NetworkInfo = {
export const OptimismNetworkInfo: NetworkInfo = {
id: SupportedNetwork.OPTIMISM,
route: 'optimism',
pathName: '/optimism/',
name: 'Optimism',
bgColor: '#F01B36',
primaryColor: '#F01B36',
Expand All @@ -56,6 +60,7 @@ export const OptimismNetworkInfo: NetworkInfo = {
export const PolygonNetworkInfo: NetworkInfo = {
id: SupportedNetwork.POLYGON,
route: 'polygon',
pathName: '/polygon/',
name: 'Polygon',
bgColor: '#8247e5',
primaryColor: '#8247e5',
Expand All @@ -64,6 +69,13 @@ export const PolygonNetworkInfo: NetworkInfo = {
blurb: '',
}

export const SUPPORTED_NETWORK_PATH_NAMES = [
EthereumNetworkInfo.pathName,
ArbitrumNetworkInfo.pathName,
OptimismNetworkInfo.pathName,
PolygonNetworkInfo.pathName,
]

export const SUPPORTED_NETWORK_VERSIONS: NetworkInfo[] = [
EthereumNetworkInfo,
PolygonNetworkInfo,
Expand Down
4 changes: 4 additions & 0 deletions src/constants/tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const HEADER_EXTRA_TABS_ROUTES = {
pools: 'pools',
tokens: 'tokens',
}