import * as React from "react"; import styled from "styled-components"; const { compose } = window.wp.compose; const { withSelect, withDispatch } = window.wp.data; import { SelectType, DispatchType } from "../../../typings/gutenberg"; import { EthAddress } from "@joincivil/core"; import { fonts, Button, buttonSizes, AddressWithMetaMaskIcon } from "@joincivil/components"; import { hasInjectedProvider, getMetaMaskEnabled as isMetaMaskEnabled } from "../../util"; import { saveAddressToProfile } from "../../api-helpers"; import { ErrorText, ErrorHeading, BodySection } from "../styles"; import { NETWORK_NICE_NAME, urls } from "../../constants"; export interface PanelWalletStatusProps { noProvider: boolean; isCorrectNetwork: boolean; wpUserWalletAddress?: EthAddress; web3ProviderAddress?: EthAddress; metaMaskEnabled?: boolean; enableMetamask(): Promise; } export interface PanelWalletStatusState { creationModalOpen: boolean; profileWalletAddress?: EthAddress; } const ProfileWalletAddress = styled.div` word-wrap: break-word; font-family: ${fonts.MONOSPACE}; margin-top: -4px; margin-bottom: 8px; `; const WalletAddressLabel = styled.div` font-weight: 500; margin-bottom: 8px; `; class PanelWalletStatusComponent extends React.Component { public render(): JSX.Element | null { const faqText = ( <> Read our FAQ {" "} for help. ); let errorHeading = null; let errorBody = null; if (this.props.noProvider) { errorHeading = "Not logged into wallet"; errorBody = (

Don’t have a wallet? Having a wallet is mandatory and we recommend{" "} MetaMask , which you can use to create and set up your wallet and address. {faqText}

); } else if (!this.props.metaMaskEnabled) { errorHeading = "Wallet not enabled"; errorBody = (

); } else if (!this.props.web3ProviderAddress) { errorHeading = "Wallet locked"; errorBody =

Please log in to your wallet to continue. {faqText}

; } else if (!this.props.isCorrectNetwork) { errorHeading = "Change network"; errorBody = (

Looks like you’re using an unsupported Ethereum network. Make sure you're using the {NETWORK_NICE_NAME}.{" "} {faqText}

); } else if (!this.props.wpUserWalletAddress) { errorHeading = "Missing wallet address"; errorBody = ( <>

Please save your wallet address from your connected MetaMask wallet to your WordPress user profile.

Public wallet address ); } else if (this.props.wpUserWalletAddress !== this.props.web3ProviderAddress) { errorHeading = "Wallet address mismatch"; errorBody = ( <>

Your WordPress user profile wallet address does not match your MetaMask wallet address.

Profile wallet address {this.props.wpUserWalletAddress} Connected wallet address ); } else { return null; } return ( Wallet {errorHeading && {errorHeading}} {errorBody} ); } private enableMetamask = async () => this.props.enableMetamask(); private saveAddress = async () => saveAddressToProfile(this.props.web3ProviderAddress!); } export const PanelWalletStatus = compose([ withSelect( (select: SelectType, ownProps: Partial): Partial => { const { isCorrectNetwork, getWeb3ProviderAddress, getCurrentWpUserAddress, getMetaMaskEnabled } = select( "civil/blockchain", ); return { metaMaskEnabled: getMetaMaskEnabled(), noProvider: !hasInjectedProvider(), isCorrectNetwork: isCorrectNetwork(), web3ProviderAddress: getWeb3ProviderAddress(), wpUserWalletAddress: getCurrentWpUserAddress(), }; }, ), withDispatch( (dispatch: DispatchType, ownProps: Partial): Partial => { const { setMetamaskIsEnabled } = dispatch("civil/blockchain"); const enableMetamask = async (): Promise => { setMetamaskIsEnabled(await isMetaMaskEnabled()); }; return { enableMetamask, }; }, ), ])(PanelWalletStatusComponent);