-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from aquiladev/header-connect-wallet
New header with connect button and extended info
- Loading branch information
Showing
10 changed files
with
328 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.