import React from 'react'; import { connect } from 'react-redux'; import { toChecksumAddress } from 'ethereumjs-util'; import { UnitDisplay, NewTabLink } from 'components/ui'; import { IWallet, TrezorWallet, LedgerWallet, Balance } from 'libs/wallet'; import translate, { translateRaw } from 'translations'; import Spinner from 'components/ui/Spinner'; import { getNetworkConfig, getOffline } from 'selectors/config'; import { AppState } from 'reducers'; import { NetworkConfig } from 'types/network'; import { TRefreshAccountBalance, refreshAccountBalance } from 'actions/wallet'; import { etherChainExplorerInst } from 'config/data'; import './AccountInfo.scss'; import AccountAddress from './AccountAddress'; interface OwnProps { wallet: IWallet; } interface StateProps { balance: Balance; network: ReturnType; isOffline: ReturnType; } interface State { showLongBalance: boolean; address: string; confirmAddr: boolean; } interface DispatchProps { refreshAccountBalance: TRefreshAccountBalance; } type Props = OwnProps & StateProps & DispatchProps; class AccountInfo extends React.Component { public state = { showLongBalance: false, address: '', confirmAddr: false }; public setAddressFromWallet() { const address = this.props.wallet.getAddressString(); if (address !== this.state.address) { this.setState({ address }); } } public componentDidMount() { this.setAddressFromWallet(); } public componentDidUpdate() { this.setAddressFromWallet(); } public toggleConfirmAddr = () => { this.setState(state => { return { confirmAddr: !state.confirmAddr }; }); }; public toggleShowLongBalance = (e: React.FormEvent) => { e.preventDefault(); this.setState(state => { return { showLongBalance: !state.showLongBalance }; }); }; public render() { const { network, isOffline, balance } = this.props; const { address, showLongBalance, confirmAddr } = this.state; let blockExplorer; let tokenExplorer; if (!network.isCustom) { // this is kind of ugly but its the result of typeguards, maybe we can find a cleaner solution later on such as just dedicating it to a selector blockExplorer = network.blockExplorer; tokenExplorer = network.tokenExplorer; } const wallet = this.props.wallet as LedgerWallet | TrezorWallet; return (
{typeof wallet.displayAddress === 'function' && ( )}
{translate('SIDEBAR_ACCOUNTBAL')}
  • {balance.wei && ( {balance.isPending ? ( ) : ( !isOffline && ( ) )} )}
{(!!blockExplorer || !!tokenExplorer) && (
{translate('SIDEBAR_TRANSHISTORY')}
    {!!blockExplorer && (
  • {`${network.name} (${blockExplorer.origin})`}
  • )} {network.id === 'ETH' && (
  • {`${network.name} (${etherChainExplorerInst.origin})`}
  • )} {!!tokenExplorer && (
  • {`Tokens (${tokenExplorer.name})`}
  • )}
)}
); } private setSymbol(network: NetworkConfig) { if (network.isTestnet) { return `${network.unit} (${translateRaw('TESTNET')})`; } return network.unit; } } function mapStateToProps(state: AppState): StateProps { return { balance: state.wallet.balance, network: getNetworkConfig(state), isOffline: getOffline(state) }; } const mapDispatchToProps: DispatchProps = { refreshAccountBalance }; export default connect(mapStateToProps, mapDispatchToProps)(AccountInfo);