Skip to content
This repository was archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Replace xDai to GC from Header Logo, routes and emptyMessage on UserD…
Browse files Browse the repository at this point in the history
…etailsTable
  • Loading branch information
henrypalacios committed Dec 21, 2021
1 parent 6795db3 commit f95e1f9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/apps/explorer/ExplorerApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GenericLayout } from 'components/layout'
import { Header } from './layout/Header'
import { media } from 'theme/styles/media'

import { NetworkUpdater, RedirectMainnet } from 'state/network'
import { NetworkUpdater, RedirectMainnet, RedirectXdai } from 'state/network'
import { initAnalytics } from 'api/analytics'
import RouteAnalytics from 'components/analytics/RouteAnalytics'
import NetworkAnalytics from 'components/analytics/NetworkAnalytics'
Expand Down Expand Up @@ -158,7 +158,8 @@ export const ExplorerApp: React.FC = () => {
<StateUpdaters />
<Switch>
<Route path="/mainnet" component={RedirectMainnet} />
<Route path={['/xdai', '/rinkeby', '/']} component={AppContent} />
<Route path="/xdai" component={RedirectXdai} />
<Route path={['/gc', '/rinkeby', '/']} component={AppContent} />
</Switch>
</Router>
{process.env.NODE_ENV === 'development' && <Console />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Order, getAccountOrders } from 'api/operator'
import Spinner from 'components/common/Spinner'
import { BlockExplorerLink } from 'components/common/BlockExplorerLink'
import { MEDIA } from 'const'
import { PREFIX_BY_NETWORK_ID } from 'state/network'
import { networkOptions } from 'components/NetworkSelector'

const Wrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -50,7 +52,7 @@ const StyledBlockExplorerLink = styled(BlockExplorerLink)`
}
`
interface OrdersInNetwork {
network: string
network: number
}

interface ResultSeachInAnotherNetwork {
Expand All @@ -64,6 +66,10 @@ type EmptyMessageProps = ResultSeachInAnotherNetwork & {
ownerAddress: string
}

const _findNetworkName = (networkId: number): string => {
return networkOptions.find((e) => e.id === networkId)?.name || 'Unknown network'
}

export const EmptyOrdersMessage = ({
isLoading,
networkId,
Expand All @@ -84,7 +90,7 @@ export const EmptyOrdersMessage = ({
) : (
<>
<p>
No orders found on <strong>{Network[networkId]}</strong> for:{' '}
No orders found on <strong>{_findNetworkName(networkId)}</strong> for:{' '}
<StyledBlockExplorerLink
identifier={ownerAddress}
type="address"
Expand All @@ -100,10 +106,10 @@ export const EmptyOrdersMessage = ({
{ordersInNetworks.map((e) => (
<li key={e.network}>
<Link
to={`/${e.network.toLowerCase()}/address/${ownerAddress}`}
to={`/${PREFIX_BY_NETWORK_ID.get(e.network)}/address/${ownerAddress}`}
onClick={(): void => setLoadingState(true)}
>
{e.network}
{_findNetworkName(e.network)}
</Link>
</li>
))}
Expand Down Expand Up @@ -137,7 +143,7 @@ export const useSearchInAnotherNetwork = (
.then((response) => {
if (!response.length) return

return { network: Network[network] }
return { network }
})
.catch((e) => {
console.error(`Failed to fetch order in ${Network[network]}`, e)
Expand Down
7 changes: 3 additions & 4 deletions src/apps/explorer/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import React from 'react'
import { Navigation } from 'components/layout/GenericLayout/Navigation'
import { Header as GenericHeader } from 'components/layout/GenericLayout/Header'
import { NetworkSelector } from 'components/NetworkSelector'
import { getNetworkFromId } from '@gnosis.pm/dex-js'
import { useNetworkId } from 'state/network'
import { PREFIX_BY_NETWORK_ID, useNetworkId } from 'state/network'
import styled from 'styled-components'

const Logo = styled.span`
Expand Down Expand Up @@ -36,10 +35,10 @@ export const Header: React.FC = () => {
return null
}

const network = networkId !== 1 ? getNetworkFromId(networkId).toLowerCase() : null
const prefixNetwork = PREFIX_BY_NETWORK_ID.get(networkId)

return (
<GenericHeader logoAlt="Gnosis Protocol" linkTo={`/${network || ''}`} label={<Logo>Gnosis Protocol</Logo>}>
<GenericHeader logoAlt="Gnosis Protocol" linkTo={`/${prefixNetwork || ''}`} label={<Logo>Gnosis Protocol</Logo>}>
<Navigation>
<NetworkSelector networkId={networkId} />
{/*
Expand Down
4 changes: 2 additions & 2 deletions src/components/NetworkSelector/NetworkSelector.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const Option = styled.div`
&.rinkeby {
background: ${({ theme }): string => theme.yellow4};
}
&.xdai {
&.gnosischain {
background: ${({ theme }): string => theme.orange1};
}
&.ethereum {
Expand Down Expand Up @@ -97,7 +97,7 @@ export const NetworkLabel = styled.span`
color: ${({ theme }): string => theme.textSecondary1};
}
&.xdai {
&.gnosischain {
background: ${({ theme }): string => theme.orangeOpacity};
color: ${({ theme }): string => theme.orange};
}
Expand Down
11 changes: 6 additions & 5 deletions src/components/NetworkSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SelectorContainer, OptionsContainer, Option, NetworkLabel, StyledFAIcon
import { replaceURL } from 'utils/url'
import { NO_REDIRECT_HOME_ROUTES } from 'const'
import { Network } from 'types'
import { cleanNetworkName } from 'utils'

type networkSelectorProps = {
networkId: number
Expand All @@ -17,16 +18,16 @@ type NetworkOptions = {
url: string
}

const networkOptions: NetworkOptions[] = [
export const networkOptions: NetworkOptions[] = [
{
id: Network.Mainnet,
name: 'Ethereum',
url: '',
},
{
id: Network.xDAI,
name: 'xDAI',
url: 'xdai',
name: 'Gnosis Chain',
url: 'gc',
},
{
id: Network.Rinkeby,
Expand Down Expand Up @@ -70,13 +71,13 @@ export const NetworkSelector: React.FC<networkSelectorProps> = ({ networkId }) =
}
return (
<SelectorContainer ref={selectContainer} onClick={(): void => setOpen(!open)}>
<NetworkLabel className={name}>{name}</NetworkLabel>
<NetworkLabel className={cleanNetworkName(name)}>{name}</NetworkLabel>
<span className={`arrow ${open && 'open'}`} />
{open && (
<OptionsContainer width={selectContainer.current?.offsetWidth || 0}>
{networkOptions.map((network) => (
<Option onClick={(): void => redirectToNetwork(network.url, networkId)} key={network.id}>
<div className={`dot ${network.name.toLowerCase()} `} />
<div className={`dot ${cleanNetworkName(network.name)} `} />
<div className={`name ${network.id === networkId && 'selected'}`}>{network.name}</div>
{network.id === networkId && <StyledFAIcon icon={faCheck} />}
</Option>
Expand Down
30 changes: 21 additions & 9 deletions src/state/network/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { web3 } from 'apps/explorer/api'
const MAINNET_PREFIX = ''
const NETWORK_PREFIXES_RAW: [Network, string][] = [
[Network.Mainnet, ''],
[Network.xDAI, 'xdai'],
[Network.xDAI, 'gc'],
[Network.Rinkeby, 'rinkeby'],
]
const PREFIX_BY_NETWORK_ID: Map<Network, string> = new Map(NETWORK_PREFIXES_RAW)
export const PREFIX_BY_NETWORK_ID: Map<Network, string> = new Map(NETWORK_PREFIXES_RAW)
const NETWORK_ID_BY_PREFIX: Map<string, Network> = new Map(NETWORK_PREFIXES_RAW.map(([key, value]) => [value, key]))

function getNetworkId(network = MAINNET_PREFIX): Network {
Expand All @@ -29,13 +29,13 @@ function getNetworkPrefix(network: Network): string {
}

/**
* Decompose URL pathname like /xdai/orders/123
* Decompose URL pathname like /gc/orders/123
*
* @returns ['xdai', 'orders/123']
* @returns ['gc', 'orders/123']
*/
export const useDecomposedPath = (): [string, string] | [] => {
const { pathname } = useLocation()
const pathMatchArray = pathname.match('/(rinkeby|xdai|mainnet)?/?(.*)')
const pathMatchArray = pathname.match('/(rinkeby|xdai|mainnet|gc)?/?(.*)')

return pathMatchArray == null ? [] : [pathMatchArray[1], pathMatchArray[2]]
}
Expand All @@ -59,12 +59,24 @@ export const RedirectToNetwork = (props: { networkId: Network }): JSX.Element |
return <Redirect push={false} to={newPath} />
}

/** Replace Network name in URL from X to Y */
export const SubstituteNetworkName = (from: string, toNetworkName = ''): string => {
const { pathname } = useLocation()

const pathMatchArray = pathname.match(`/${from}(.*)`)
return pathMatchArray && pathMatchArray.length > 0 ? `${toNetworkName}${pathMatchArray[1]}` : '/'
}

/** Redirects to the canonnical URL for mainnet */
export const RedirectMainnet = (): JSX.Element => {
const { pathname } = useLocation()
const newPath = SubstituteNetworkName('mainnet')

return <Redirect push={false} to={newPath} />
}

const pathMatchArray = pathname.match('/mainnet(.*)')
const newPath = pathMatchArray && pathMatchArray.length > 0 ? pathMatchArray[1] : '/'
/** Redirects to the xDai to the GnosisChain new name */
export const RedirectXdai = (): JSX.Element => {
const newPath = SubstituteNetworkName('xdai', '/gc')

return <Redirect push={false} to={newPath} />
}
Expand All @@ -77,7 +89,7 @@ export const NetworkUpdater: React.FC = () => {
const location = useLocation()

useEffect(() => {
const networkMatchArray = location.pathname.match('^/(rinkeby|xdai)')
const networkMatchArray = location.pathname.match('^/(rinkeby|gc)')
const network = networkMatchArray && networkMatchArray.length > 0 ? networkMatchArray[1] : undefined
const networkId = getNetworkId(network)

Expand Down
9 changes: 8 additions & 1 deletion src/utils/miscellaneous.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BN from 'bn.js'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { TEN, ZERO } from '@gnosis.pm/dex-js'

const toChecksumAddress = Web3.utils.toChecksumAddress

Expand All @@ -9,7 +10,6 @@ import { AssertionError } from 'assert'
import { AuctionElement, Trade, Order } from 'api/exchange/ExchangeApi'
import { batchIdToDate } from './time'
import { ORDER_FILLED_FACTOR, MINIMUM_ALLOWANCE_DECIMALS, DEFAULT_TIMEOUT, NATIVE_TOKEN_ADDRESS } from 'const'
import { TEN, ZERO } from '@gnosis.pm/dex-js'

export function assertNonNull<T>(val: T, message: string): asserts val is NonNullable<T> {
if (val === undefined || val === null) {
Expand Down Expand Up @@ -280,3 +280,10 @@ export const isAnAddressAccount = (text: string): boolean => {
* @returns true if valid or false if not
*/
export const isEns = (text: string): boolean => text.match(/[a-zA-Z0-9]+\.[a-zA-Z]+$/)?.input !== undefined

/** Convert string to lowercase and remove whitespace */
export function cleanNetworkName(networkName: string | undefined): string {
if (!networkName) return ''

return networkName.replace(/\s+/g, '').toLowerCase()
}

0 comments on commit f95e1f9

Please sign in to comment.