select networks dynamically

This commit is contained in:
Mati Dastugue 2021-06-03 15:59:41 -03:00
parent 7a186026cd
commit 3011b65d62
4 changed files with 45 additions and 38 deletions

View File

@ -5,6 +5,7 @@ import Popper from '@material-ui/core/Popper'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import * as React from 'react' import * as React from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import styled from 'styled-components'
import { Divider, Icon } from '@gnosis.pm/safe-react-components' import { Divider, Icon } from '@gnosis.pm/safe-react-components'
import Provider from './Provider' import Provider from './Provider'
@ -20,7 +21,7 @@ import { useStateHandler } from 'src/logic/hooks/useStateHandler'
import SafeLogo from '../assets/gnosis-safe-multisig-logo.svg' import SafeLogo from '../assets/gnosis-safe-multisig-logo.svg'
import { getNetworkName, getNetworks } from 'src/config' import { getNetworkName, getNetworks } from 'src/config'
import styled from 'styled-components' import { sameString } from 'src/utils/strings'
const StyledDivider = styled(Divider)` const StyledDivider = styled(Divider)`
margin: 0; margin: 0;
@ -84,10 +85,8 @@ const styles = () => ({
const Layout = ({ classes, providerDetails, providerInfo }) => { const Layout = ({ classes, providerDetails, providerInfo }) => {
const { clickAway, open, toggle } = useStateHandler() const { clickAway, open, toggle } = useStateHandler()
const { clickAway: clickAwayNetworks, open: openNetworks, toggle: toggleNetworks } = useStateHandler() const { clickAway: clickAwayNetworks, open: openNetworks, toggle: toggleNetworks } = useStateHandler()
const networkName = getNetworkName() // Network name to be use for comparision const networkName = getNetworkName().toLowerCase()
const networks = getNetworks() const networks = getNetworks()
console.log(networkName)
return ( return (
<Row className={classes.summary}> <Row className={classes.summary}>
<Col className={classes.logo} middle="xs" start="xs"> <Col className={classes.logo} middle="xs" start="xs">
@ -124,6 +123,7 @@ const Layout = ({ classes, providerDetails, providerInfo }) => {
/> />
<NetworkSelector <NetworkSelector
open={openNetworks} open={openNetworks}
selected={networkName}
toggle={toggleNetworks} toggle={toggleNetworks}
render={(networkRef) => ( render={(networkRef) => (
<Popper <Popper
@ -141,8 +141,10 @@ const Layout = ({ classes, providerDetails, providerInfo }) => {
{networks.map((network) => ( {networks.map((network) => (
<React.Fragment key={network.id}> <React.Fragment key={network.id}>
<StyledLink href={network.safeUrl}> <StyledLink href={network.safeUrl}>
<NetworkLabel networkName={network.label} /> <NetworkLabel networkInfo={network} networkName={network.label} />
{sameString(networkName, network.label.toLowerCase()) && (
<Icon type="check" size="md" color="primary" /> <Icon type="check" size="md" color="primary" />
)}
</StyledLink> </StyledLink>
<StyledDivider /> <StyledDivider />
</React.Fragment> </React.Fragment>

View File

@ -1,43 +1,48 @@
import { makeStyles } from '@material-ui/core/styles'
import * as React from 'react' import * as React from 'react'
import styled from 'styled-components'
import Col from 'src/components/layout/Col' import Col from 'src/components/layout/Col'
import Paragraph from 'src/components/layout/Paragraph' import Paragraph from 'src/components/layout/Paragraph'
import { getNetworkInfo } from 'src/config' import { getNetworkInfo } from 'src/config'
import { border, md, screenSm, sm, xs, fontColor } from 'src/theme/variables' import { border, md, screenSm, sm, xs, fontColor } from 'src/theme/variables'
import { NetworkSettings } from 'src/config/networks/network'
const networkInfo = getNetworkInfo() const currentNetworkInfo = getNetworkInfo()
const useStyles = makeStyles({ const StyledCol = styled(Col)`
container: { flex-grow: 0;
flexGrow: 0, padding: 0 ${sm};
padding: `0 ${sm}`, @media (min-width: ${screenSm}px) {
[`@media (min-width: ${screenSm}px)`]: { padding-left: ${md};
paddingLeft: md, padding-right: ${md};
paddingRight: md, }
}, `
}, const StyledParagraph = styled(Paragraph)<{ network: Partial<NetworkSettings> }>`
text: { background-color: ${(props) => props.network?.backgroundColor ?? border};
backgroundColor: `${networkInfo?.backgroundColor ?? border}`, color: ${(props) => props.network?.textColor ?? fontColor};
color: `${networkInfo?.textColor ?? fontColor}`, border-radius: 3px;
borderRadius: '3px', line-height: normal;
lineHeight: 'normal', text-transform: capitalize;
margin: '0', margin: 0;
padding: `${xs} ${sm}`, padding: ${xs} ${sm};
minWidth: '70px', min-width: 70px;
textAlign: 'center', text-align: center;
}, `
})
const NetworkLabel = ({ networkName }): React.ReactElement => { const NetworkLabel = ({
const classes = useStyles() networkName,
networkInfo,
}: {
networkName: string
networkInfo?: Partial<NetworkSettings>
}): React.ReactElement => {
const selectedNetwork = networkInfo || currentNetworkInfo
return ( return (
<Col className={classes.container} middle="xs" start="xs"> <StyledCol middle="xs" start="xs">
<Paragraph className={classes.text} size="xs"> <StyledParagraph network={selectedNetwork} size="xs">
{networkName} {networkName}
</Paragraph> </StyledParagraph>
</Col> </StyledCol>
) )
} }

View File

@ -46,14 +46,13 @@ class NetworkSelector extends React.Component<any> {
} }
render() { render() {
const { render, classes, open, toggle } = this.props const { render, classes, open, toggle, selected } = this.props
return ( return (
<> <>
<div className={classes.root} ref={this.myRef}> <div className={classes.root} ref={this.myRef}>
<Col className={classes.networkList} end="sm" middle="xs" onClick={toggle}> <Col className={classes.networkList} end="sm" middle="xs" onClick={toggle}>
{/* ToDo read the networkname from the current wallet */} <NetworkLabel networkName={selected} />
<NetworkLabel networkName="Ethereum" />
<IconButton className={classes.expand} disableRipple> <IconButton className={classes.expand} disableRipple>
{open ? <ExpandLess /> : <ExpandMore />} {open ? <ExpandLess /> : <ExpandMore />}
</IconButton> </IconButton>

View File

@ -73,6 +73,7 @@ export const getNetworks = (): { id: number; label: string; backgroundColor: str
id: networkObj.network.id, id: networkObj.network.id,
label: networkObj.network.label, label: networkObj.network.label,
backgroundColor: networkObj.network.backgroundColor, backgroundColor: networkObj.network.backgroundColor,
textColor: networkObj.network.textColor,
safeUrl: networkObj.environment[getCurrentEnvironment()].safeUrl, safeUrl: networkObj.environment[getCurrentEnvironment()].safeUrl,
})) }))
} }