import React from 'react'; import { connect } from 'react-redux'; import { AppState } from 'reducers'; import { addCustomToken, removeCustomToken, TAddCustomToken, TRemoveCustomToken } from 'actions/customTokens'; import { scanWalletForTokens, TScanWalletForTokens, setWalletTokens, TSetWalletTokens } from 'actions/wallet'; import { getAllTokens, getOffline } from 'selectors/config'; import { getTokenBalances, getWalletInst, getWalletConfig, TokenBalance } from 'selectors/wallet'; import translate from 'translations'; import Balances from './Balances'; import Spinner from 'components/ui/Spinner'; import { Token } from 'types/network'; import './index.scss'; interface StateProps { wallet: AppState['wallet']['inst']; walletConfig: AppState['wallet']['config']; tokens: Token[]; tokenBalances: TokenBalance[]; tokensError: AppState['wallet']['tokensError']; isTokensLoading: AppState['wallet']['isTokensLoading']; hasSavedWalletTokens: AppState['wallet']['hasSavedWalletTokens']; isOffline: AppState['config']['meta']['offline']; } interface ActionProps { addCustomToken: TAddCustomToken; removeCustomToken: TRemoveCustomToken; scanWalletForTokens: TScanWalletForTokens; setWalletTokens: TSetWalletTokens; } type Props = StateProps & ActionProps; class TokenBalances extends React.Component { public render() { const { tokens, walletConfig, tokenBalances, hasSavedWalletTokens, isTokensLoading, tokensError, isOffline } = this.props; const walletTokens = walletConfig ? walletConfig.tokens : []; let content; if (isOffline) { content = (
Token balances are unavailable offline
); } else if (tokensError) { content =
{tokensError}
; } else if (isTokensLoading) { content = (
); } else if (!walletTokens) { content = ( ); } else { const shownBalances = tokenBalances.filter(t => walletTokens.includes(t.symbol)); content = ( ); } return (
{translate('sidebar_TokenBal')}
{content}
); } private scanWalletForTokens = () => { if (this.props.wallet) { this.props.scanWalletForTokens(this.props.wallet); this.setState({ hasScanned: true }); } }; } function mapStateToProps(state: AppState): StateProps { return { wallet: getWalletInst(state), walletConfig: getWalletConfig(state), tokens: getAllTokens(state), tokenBalances: getTokenBalances(state), tokensError: state.wallet.tokensError, isTokensLoading: state.wallet.isTokensLoading, hasSavedWalletTokens: state.wallet.hasSavedWalletTokens, isOffline: getOffline(state) }; } export default connect(mapStateToProps, { addCustomToken, removeCustomToken, scanWalletForTokens, setWalletTokens })(TokenBalances);