|
| 1 | +import React from "react" |
| 2 | +import { Box, Button, Image, Text } from "@chakra-ui/react" |
| 3 | +import { Account } from "@ledgerhq/wallet-api-client" |
| 4 | +import BitcoinIcon from "../../assets/bitcoin.svg" |
| 5 | +import EthereumIcon from "../../assets/ethereum.svg" |
| 6 | +import InfoIcon from "../../assets/info.svg" |
| 7 | +import { BITCOIN } from "../../constants" |
| 8 | +import { |
| 9 | + useRequestBitcoinAccount, |
| 10 | + useRequestEthereumAccount, |
| 11 | + useWalletContext, |
| 12 | +} from "../../hooks" |
| 13 | +import { formatSatoshiAmount, truncateAddress } from "../../utils" |
| 14 | + |
| 15 | +export type ConnectButtonsProps = { |
| 16 | + leftIcon: string |
| 17 | + rightIcon: string |
| 18 | + account: Account | undefined |
| 19 | + requestAccount: () => Promise<void> |
| 20 | +} |
| 21 | + |
| 22 | +function ConnectButton({ |
| 23 | + leftIcon, |
| 24 | + rightIcon, |
| 25 | + account, |
| 26 | + requestAccount, |
| 27 | +}: ConnectButtonsProps) { |
| 28 | + const styles = !account ? { color: "error", borderColor: "error" } : undefined |
| 29 | + return ( |
| 30 | + <Button |
| 31 | + variant="outline" |
| 32 | + sx={styles} |
| 33 | + leftIcon={<Image src={leftIcon} />} |
| 34 | + // TODO: Add a tooltip here |
| 35 | + rightIcon={!account ? <Image src={rightIcon} /> : undefined} |
| 36 | + onClick={requestAccount} |
| 37 | + > |
| 38 | + {account ? truncateAddress(account.address) : "Not connected"} |
| 39 | + </Button> |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +export default function ConnectWallet() { |
| 44 | + const { requestAccount: requestBitcoinAccount } = useRequestBitcoinAccount() |
| 45 | + const { requestAccount: requestEthereumAccount } = useRequestEthereumAccount() |
| 46 | + const { btcAccount, ethAccount } = useWalletContext() |
| 47 | + |
| 48 | + return ( |
| 49 | + <Box display="flex" alignItems="center" gap="4"> |
| 50 | + <Box display="flex" gap="2"> |
| 51 | + <Text>Balance</Text> |
| 52 | + <Text as="b"> |
| 53 | + {!btcAccount || btcAccount?.balance.isZero() |
| 54 | + ? "0.00" |
| 55 | + : formatSatoshiAmount(btcAccount.balance.toString())} |
| 56 | + </Text> |
| 57 | + <Text>{BITCOIN.symbol}</Text> |
| 58 | + </Box> |
| 59 | + <ConnectButton |
| 60 | + leftIcon={BitcoinIcon} |
| 61 | + rightIcon={InfoIcon} |
| 62 | + account={btcAccount} |
| 63 | + requestAccount={async () => { |
| 64 | + await requestBitcoinAccount() |
| 65 | + }} |
| 66 | + /> |
| 67 | + <ConnectButton |
| 68 | + leftIcon={EthereumIcon} |
| 69 | + rightIcon={InfoIcon} |
| 70 | + account={ethAccount} |
| 71 | + requestAccount={async () => { |
| 72 | + await requestEthereumAccount() |
| 73 | + }} |
| 74 | + /> |
| 75 | + </Box> |
| 76 | + ) |
| 77 | +} |
0 commit comments