Skip to content

Commit

Permalink
Merge pull request #15 from aquiladev/header-connect-wallet
Browse files Browse the repository at this point in the history
New header with connect button and extended info
  • Loading branch information
aquiladev authored Jul 17, 2021
2 parents 63b425d + 5e2a79f commit b1e80b1
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 101 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"private": true,
"homepage": "./",
"dependencies": {
"@ethersproject/units": "^5.4.0",
"@gitgraph/react": "1.5.4",
"@gnosis.pm/safe-apps-provider": "0.2.3",
"@gnosis.pm/safe-apps-react-sdk": "2.2.0",
"@gnosis.pm/safe-react-components": "0.5.0",
"@material-ui/core": "4.11.2",
"@material-ui/icons": "4.11.2",
"@material-ui/lab": "4.0.0-alpha.57",
"@metamask/jazzicon": "^2.0.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
Expand Down
50 changes: 6 additions & 44 deletions src/DefaultApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import {
import Web3 from 'web3';

import Container from '@material-ui/core/Container';
import Grid from '@material-ui/core/Grid';
import Card from '@material-ui/core/Card';
import CardActionArea from '@material-ui/core/CardActionArea';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
import Typography from '@material-ui/core/Typography';
import Backdrop from '@material-ui/core/Backdrop';
import CircularProgress from '@material-ui/core/CircularProgress';
import Alert from '@material-ui/lab/Alert';

import GlobalStyle from './components/GlobalStyle';
import mmLogo from './images/mm.png';
import { injected, useEagerConnect, useInactiveListener } from './hooks';
import Domains from './components/Domains';
import Lookup from './components/Lookup';
Expand Down Expand Up @@ -47,6 +38,9 @@ const useStyles = makeStyles((theme) => ({
overflow: 'hidden',
minHeight: '100%'
},
content: {
minHeight: 100
},
title: {
fontSize: '1rem',
[theme.breakpoints.up('sm')]: {
Expand Down Expand Up @@ -99,7 +93,7 @@ function getLibrary(provider) {
function DefaultApp() {
const classes = useStyles();
const context = useWeb3React();
const { connector, library, account, chainId, activate, deactivate, active, error } = context;
const { connector, library, account, chainId, active, error } = context;

const [activatingConnector, setActivatingConnector] = useState();
const [isLookup, setIsLookup] = useState();
Expand All @@ -114,19 +108,17 @@ function DefaultApp() {
useInactiveListener(!triedEager || !!activatingConnector);

const currentConnector = injected
const activating = currentConnector === activatingConnector
const connected = currentConnector === connector

return (
<ThemeProvider theme={theme}>
<div className={classes.root}>
<Header
<Header
active={active}
account={account}
deactivate={deactivate}
isLookup={isLookup}
setIsLookup={setIsLookup} />
<Container maxWidth='lg'>
<Container maxWidth='lg' className={classes.content}>
{
!!error &&
<Alert
Expand All @@ -137,36 +129,6 @@ function DefaultApp() {
{getErrorMessage(error)}
</Alert>
}
{
(!connected || !account) &&
<>
<Grid
container
spacing={0}
direction='column'
alignItems='center'
justify='center'
style={{ minHeight: '100vh' }}
>
<Card className={classes.card}>
<CardActionArea onClick={() => {
setActivatingConnector(currentConnector)
activate(injected)
}}>
<CardMedia component='img' image={mmLogo} className={classes.cardMedia} />
<CardContent>
<Typography variant='h5' component='h2'>
MetaMask
</Typography>
</CardContent>
</CardActionArea>
</Card>
</Grid>
<Backdrop className={classes.backdrop} open={activating}>
<CircularProgress color='inherit' />
</Backdrop>
</>
}
{connected && account && !isLookup && <Domains library={library} account={account} chainId={chainId} />}
{connected && isLookup && <Lookup library={library} chainId={chainId} />}
</Container>
Expand Down
76 changes: 76 additions & 0 deletions src/components/AccountModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { useWeb3React } from '@web3-react/core';
import Popover from '@material-ui/core/Popover';
import Container from '@material-ui/core/Container';
import Box from '@material-ui/core/Box';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';

import Identicon from './Identicon';

const useStyles = makeStyles(() => ({
modal: {
padding: 20
},
box: {
display: 'flex'
},
account: {
paddingLeft: 8
},
btn: {
padding: 0,
marginLeft: 20
}
}));

export default function AccountModal({anchorEl, onClose}) {
const classes = useStyles();

const { account, deactivate } = useWeb3React();

const open = Boolean(anchorEl);

const handleClose = () => {
onClose && onClose();
};

return (
<Popover
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
>
<Container className={classes.modal}>
<Box className={classes.box}>
<Identicon account={account} />
<Typography className={classes.account}>
{account &&
`${account.slice(0, 6)}...${account.slice(
account.length - 4,
account.length
)}`}
</Typography>
<Button size='small'
className={classes.btn}
onClick={() => {
deactivate();
handleClose();
}}
>
Disconnect
</Button>
</Box>
</Container>
</Popover>
)
}
109 changes: 109 additions & 0 deletions src/components/ConnectButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Box from '@material-ui/core/Box';
import { Typography } from '@material-ui/core';
import { indigo, amber } from '@material-ui/core/colors';
import { useWeb3React } from '@web3-react/core';
import { formatEther } from "@ethersproject/units";

import { injected } from './../hooks';
import Identicon from './Identicon';
import AccountModal from './AccountModal';
import { CHAIN_ID_NETWORK } from './../utils/constants';

const useStyles = makeStyles(() => ({
box_net: {
display: 'flex',
alignItems: 'center',
background: indigo[400],
borderRadius: 6,
padding: 8,
marginRight: 8,
color: amber[500],
},
box_acc: {
display: 'flex',
alignItems: 'center',
background: indigo[400],
borderRadius: 6,
paddingLeft: 8,
},
info_btn: {
margin: '2px 2px 2px 6px',
background: indigo[500],
color: 'white',
textTransform: 'none',
},
info_btn_text: {
paddingRight: 6,
},
connect_btn: {
background: indigo[400],
color: 'white',
}
}));

export default function ConnectButton() {
const classes = useStyles();

const { account, library, chainId, activate } = useWeb3React();

const [balance, setBalance] = React.useState();
const [modalEl, setModalEl] = React.useState(null);

React.useEffect(() => {
if (!!account && !!library) {
let stale = false

library.eth
.getBalance(account)
.then((balance) => {
if (!stale) {
setBalance(balance)
}
})
.catch(() => {
if (!stale) {
setBalance(null)
}
})

return () => {
stale = true
setBalance(undefined)
}
}
}, [account, library, chainId])

return account ? (
<>
<Box className={classes.box_net}>
<Typography>
{CHAIN_ID_NETWORK[chainId] || chainId}
</Typography>
</Box>
<Box className={classes.box_acc}>
{balance && parseFloat(formatEther(balance)).toFixed(3)} ETH
<Button className={classes.info_btn} onClick={(event) => setModalEl(event.currentTarget)}>
<Typography className={classes.info_btn_text}>
{account &&
`${account.slice(0, 6)}...${account.slice(
account.length - 4,
account.length
)}`}
</Typography>
<Identicon account={account} />
</Button>
<AccountModal anchorEl={modalEl} onClose={() => setModalEl(null)}/>
</Box>
</>
) : (
<Button
className={classes.connect_btn}
onClick={() => activate(injected)}
>
Connect to a wallet
</Button>
);
}
8 changes: 6 additions & 2 deletions src/components/Domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const useStyles = makeStyles((theme) => ({
},
btn: {
margin: '0 10px',
},
noDomains: {
textAlign: 'center'
}
}));

Expand Down Expand Up @@ -483,14 +486,15 @@ const Domains = ({ library, account, chainId }) => {
</Dialog>
{
fetched && data[stateKey] && !data[stateKey].domains.length &&
<p>No .crypto domains found.
<p className={classes.noDomains}>No domains found.
<Button color='primary'
variant='contained'
className={classes.btn}
onClick={() => { setFreeDomain(true) }}>
Claim free domain
</Button>
OR <a href='https://unstoppabledomains.com/'>Buy here</a></p>
OR <a href='https://unstoppabledomains.com/'>Buy here</a>
</p>
}
{
<Backdrop className={classes.backdrop} open={defaultResolving}>
Expand Down
Loading

0 comments on commit b1e80b1

Please sign in to comment.