|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import styled from "styled-components"; |
| 3 | +import { generateOnRampURL } from "@coinbase/cbpay-js"; |
| 4 | +import theme from "shared/lib/designSystem/theme"; |
| 5 | +import colors from "shared/lib/designSystem/colors"; |
| 6 | +import { useWeb3Wallet } from "shared/lib/hooks/useWeb3Wallet"; |
| 7 | +import { Chains, ID_TO_CHAINS } from "shared/lib/constants/constants"; |
| 8 | +import { BuyIcon } from "shared/lib/assets/icons/icons"; |
| 9 | + |
| 10 | +const ButtonContainer = styled.div` |
| 11 | + display: flex; |
| 12 | + align-items: center; |
| 13 | + justify-content: center; |
| 14 | + border-radius: ${theme.border.radius}; |
| 15 | + background: ${colors.background.two}; |
| 16 | + height: 48px; |
| 17 | + width: 48px; |
| 18 | +
|
| 19 | + &:hover { |
| 20 | + svg { |
| 21 | + path { |
| 22 | + opacity: ${theme.hover.opacity}; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | +`; |
| 27 | + |
| 28 | +const IconContainer = styled.div` |
| 29 | + height: 24px; |
| 30 | + width: 24px; |
| 31 | + display: flex; |
| 32 | + align-items: center; |
| 33 | + justify-content: center; |
| 34 | +`; |
| 35 | + |
| 36 | +type DestinationWallet = { |
| 37 | + address: string; |
| 38 | + blockchains: string[]; |
| 39 | +}; |
| 40 | + |
| 41 | +export const BuyButton: React.FC = () => { |
| 42 | + const [onrampURL, setOnrampURL] = useState<string>(); |
| 43 | + const { account, chainId } = useWeb3Wallet(); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + const destinationWallets: DestinationWallet[] = []; |
| 47 | + const addWallet = ( |
| 48 | + blockchain: "ethereum" | "avalanche-c-chain" | "solana" |
| 49 | + ) => { |
| 50 | + if (!account) { |
| 51 | + return; |
| 52 | + } |
| 53 | + destinationWallets.push({ |
| 54 | + address: account, |
| 55 | + blockchains: [blockchain], |
| 56 | + }); |
| 57 | + }; |
| 58 | + if (account && account.length > 0 && chainId) { |
| 59 | + switch (ID_TO_CHAINS[chainId]) { |
| 60 | + case Chains.Ethereum: |
| 61 | + addWallet("ethereum"); |
| 62 | + break; |
| 63 | + case Chains.Avalanche: |
| 64 | + addWallet("avalanche-c-chain"); |
| 65 | + break; |
| 66 | + case Chains.Solana: |
| 67 | + addWallet("solana"); |
| 68 | + } |
| 69 | + } |
| 70 | + if (destinationWallets.length < 1) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const appId = process.env.REACT_APP_COINBASE_APPID; |
| 75 | + if (!appId) return; |
| 76 | + |
| 77 | + setOnrampURL( |
| 78 | + generateOnRampURL({ |
| 79 | + appId, |
| 80 | + destinationWallets, |
| 81 | + }) |
| 82 | + ); |
| 83 | + }, [account, chainId]); |
| 84 | + |
| 85 | + if (!account || !onrampURL) return <></>; |
| 86 | + return ( |
| 87 | + <a href={onrampURL} target="_blank" rel="noreferrer noopener"> |
| 88 | + <ButtonContainer> |
| 89 | + <IconContainer> |
| 90 | + <BuyIcon /> |
| 91 | + </IconContainer> |
| 92 | + </ButtonContainer> |
| 93 | + </a> |
| 94 | + ); |
| 95 | +}; |
0 commit comments