From 4df84b8b14d32991037263e27342ac8e1fbeead6 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 9 Jun 2021 19:41:12 +0200 Subject: [PATCH] split up network list into main & test networks --- src/components/atoms/NetworkName.tsx | 4 +- .../UserPreferences/Chain.module.css | 7 ++ .../molecules/UserPreferences/Chain.tsx | 99 +++++++++++++++---- src/components/molecules/Wallet/Network.tsx | 14 +-- src/providers/Web3.tsx | 7 +- src/utils/web3.ts | 2 +- 6 files changed, 101 insertions(+), 32 deletions(-) diff --git a/src/components/atoms/NetworkName.tsx b/src/components/atoms/NetworkName.tsx index 502b16e76..84ad76dc1 100644 --- a/src/components/atoms/NetworkName.tsx +++ b/src/components/atoms/NetworkName.tsx @@ -5,7 +5,7 @@ import { ReactComponent as PolygonIcon } from '../../images/polygon.svg' import { ReactComponent as MoonbeamIcon } from '../../images/moonbeam.svg' import { EthereumListsChain, - getNetworkData, + getNetworkDataById, getNetworkDisplayName } from '../../utils/web3' import styles from './NetworkName.module.css' @@ -53,7 +53,7 @@ export default function NetworkName({ const data = useStaticQuery(networksQuery) const networksList: { node: EthereumListsChain }[] = data.allNetworksMetadataJson.edges - const networkData = getNetworkData(networksList, networkId) + const networkData = getNetworkDataById(networksList, networkId) const networkName = getNetworkDisplayName(networkData, networkId) return ( diff --git a/src/components/molecules/UserPreferences/Chain.module.css b/src/components/molecules/UserPreferences/Chain.module.css index 10f587861..6795a2c52 100644 --- a/src/components/molecules/UserPreferences/Chain.module.css +++ b/src/components/molecules/UserPreferences/Chain.module.css @@ -1,3 +1,10 @@ +.titleGroup { + font-size: var(--font-size-small); + margin-bottom: calc(var(--spacer) / 6); + margin-top: calc(var(--spacer) / 2); + color: var(--color-secondary); +} + .chains { composes: box from '../../atoms/Box.module.css'; box-shadow: none; diff --git a/src/components/molecules/UserPreferences/Chain.tsx b/src/components/molecules/UserPreferences/Chain.tsx index f3b1e5f46..e8ca1a907 100644 --- a/src/components/molecules/UserPreferences/Chain.tsx +++ b/src/components/molecules/UserPreferences/Chain.tsx @@ -6,8 +6,56 @@ import NetworkName from '../../atoms/NetworkName' import { removeItemFromArray } from '../../../utils' import FormHelp from '../../atoms/Input/Help' import styles from './Chain.module.css' +import { useStaticQuery, graphql } from 'gatsby' +import { EthereumListsChain, getNetworkDataById } from '../../../utils/web3' + +const networksQuery = graphql` + query { + allNetworksMetadataJson { + edges { + node { + chain + network + networkId + chainId + } + } + } + } +` + +function ChainItem({ + isDefaultChecked, + chainId, + handleChainChanged +}: { + isDefaultChecked: boolean + chainId: number + handleChainChanged: (e: ChangeEvent) => void +}) { + return ( +
+ +
+ ) +} export default function Chain(): ReactElement { + const data = useStaticQuery(networksQuery) + const networksList: { node: EthereumListsChain }[] = + data.allNetworksMetadataJson.edges + const { appConfig } = useSiteMetadata() const { chainIds, setChainIds } = useUserPreferences() @@ -23,27 +71,42 @@ export default function Chain(): ReactElement { setChainIds(newChainIds) } + const chainsMain = appConfig.chainIdsSupported + .filter((chainId: number) => { + const networkData = getNetworkDataById(networksList, chainId) + return networkData.network === 'mainnet' + }) + .map((chainId) => ( + + )) + + const chainsTest = appConfig.chainIdsSupported + .filter((chainId: number) => { + const networkData = getNetworkDataById(networksList, chainId) + return networkData.network !== 'mainnet' + }) + .map((chainId) => ( + + )) + return (
  • -
    - {appConfig.chainIdsSupported.map((chainId) => ( -
    - -
    - ))} -
    + +

    Main

    +
    {chainsMain}
    +

    Test

    +
    {chainsTest}
    Switch the data source for the interface.
  • diff --git a/src/components/molecules/Wallet/Network.tsx b/src/components/molecules/Wallet/Network.tsx index b9eccd893..750228591 100644 --- a/src/components/molecules/Wallet/Network.tsx +++ b/src/components/molecules/Wallet/Network.tsx @@ -1,7 +1,6 @@ import React, { useState, useEffect, ReactElement } from 'react' -import { useOcean } from '../../../providers/Ocean' import Status from '../../atoms/Status' -import { ConfigHelper, ConfigHelperConfig } from '@oceanprotocol/lib' +import { ConfigHelper } from '@oceanprotocol/lib' import Badge from '../../atoms/Badge' import Tooltip from '../../atoms/Tooltip' import { useWeb3 } from '../../../providers/Web3' @@ -11,7 +10,8 @@ import styles from './Network.module.css' export default function Network(): ReactElement { const { networkId, isTestnet } = useWeb3() - const [isSupportedNetwork, setIsSupportedNetwork] = useState() + const [isSupportedOceanNetwork, setIsSupportedOceanNetwork] = + useState() useEffect(() => { // take network from user when present @@ -19,13 +19,15 @@ export default function Network(): ReactElement { // Check networkId against ocean.js ConfigHelper configs // to figure out if network is supported. - const isSupportedNetwork = Boolean(new ConfigHelper().getConfig(network)) - setIsSupportedNetwork(isSupportedNetwork) + const isSupportedOceanNetwork = Boolean( + new ConfigHelper().getConfig(network) + ) + setIsSupportedOceanNetwork(isSupportedOceanNetwork) }, [networkId]) return networkId ? (
    - {!isSupportedNetwork && ( + {!isSupportedOceanNetwork && ( diff --git a/src/providers/Web3.tsx b/src/providers/Web3.tsx index a368d9c80..93217ee80 100644 --- a/src/providers/Web3.tsx +++ b/src/providers/Web3.tsx @@ -15,7 +15,7 @@ import { Logger } from '@oceanprotocol/lib' import { isBrowser } from '../utils' import { EthereumListsChain, - getNetworkData, + getNetworkDataById, getNetworkDisplayName } from '../utils/web3' import { graphql, useStaticQuery } from 'gatsby' @@ -230,7 +230,7 @@ function Web3Provider({ children }: { children: ReactNode }): ReactElement { useEffect(() => { if (!networkId) return - const networkData = getNetworkData(networksList, networkId) + const networkData = getNetworkDataById(networksList, networkId) setNetworkData(networkData) Logger.log('[web3] Network metadata found.', networkData) @@ -339,6 +339,3 @@ const useWeb3 = (): Web3ProviderValue => useContext(Web3Context) export { Web3Provider, useWeb3, Web3ProviderValue, Web3Context } export default Web3Provider -function getTokenBalance() { - throw new Error('Function not implemented.') -} diff --git a/src/utils/web3.ts b/src/utils/web3.ts index fa5ce829a..ae7ab7117 100644 --- a/src/utils/web3.ts +++ b/src/utils/web3.ts @@ -54,7 +54,7 @@ export function getNetworkDisplayName( return displayName } -export function getNetworkData( +export function getNetworkDataById( data: { node: EthereumListsChain }[], networkId: number ): EthereumListsChain {