/* @flow */ 'use strict'; import React, { PureComponent } from 'react'; import { Link, NavLink } from 'react-router-dom'; import BigNumber from 'bignumber.js'; import { findDeviceAccounts } from '~/js/reducers/AccountsReducer'; import { findSelectedDevice } from '~/js/reducers/TrezorConnectReducer'; import Loader from '~/js/components/common/LoaderCircle'; import Tooltip from 'rc-tooltip'; import type { Props } from './index'; import type { TrezorDevice } from '~/flowtype'; const AccountSelection = (props: Props): ?React$Element => { const selected = findSelectedDevice(props.connect); if (!selected) return null; const { location } = props.router; const urlParams = location.state; const accounts = props.accounts; const baseUrl: string = urlParams.deviceInstance ? `/device/${urlParams.device}:${urlParams.deviceInstance}` : `/device/${urlParams.device}`; const { config } = props.localStorage; const selectedCoin = config.coins.find(c => c.network === location.state.network); if (!selectedCoin) return; const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); const deviceAccounts: Array = findDeviceAccounts(accounts, selected, location.state.network); let selectedAccounts = deviceAccounts.map((account, i) => { // const url: string = `${baseUrl}/network/${location.state.network}/account/${i}`; const url: string = location.pathname.replace(/account+\/([0-9]*)/, `account/${i}`); let balance: string = 'Loading...'; if (account.balance !== '') { if (fiatRate) { const accountBalance = new BigNumber(account.balance); const fiat = accountBalance.times(fiatRate.value).toFixed(2); balance = `${ account.balance } ${ selectedCoin.symbol } / $${ fiat }`; } else { balance = `${ account.balance } ${ selectedCoin.symbol }`; } } return ( { `Account #${(account.index + 1 )}` } { account.loaded ? balance : "Loading..." } ) }); if (selectedAccounts.length < 1) { if (selected.connected) { const url: string = location.pathname.replace(/account+\/([0-9]*)/, `account/0`); selectedAccounts = ( Account #1 Loading... ) } } let discoveryStatus = null; const discovery = props.discovery.find(d => d.deviceState === selected.state && d.network === location.state.network); if (discovery) { if (discovery.completed) { // TODO: add only if last one is not empty //if (selectedAccounts.length > 0 && selectedAccounts[selectedAccounts.length - 1]) const lastAccount = deviceAccounts[deviceAccounts.length - 1]; if (lastAccount && (new BigNumber(lastAccount.balance).greaterThan(0) || lastAccount.nonce > 0)) { discoveryStatus = (
Add account
); } else { const tooltip = (
To add a new account, last account must have some transactions.
) discoveryStatus = ( } overlay={ tooltip } placement="top">
Add account
); } } else if (!selected.connected || !selected.available) { discoveryStatus = (
Accounts could not be loaded { `Connect ${ selected.instanceLabel } device` }
); } else { discoveryStatus = (
Loading accounts...
); } } let backButton = null; if (selectedCoin) { backButton = ( { selectedCoin.name } ); } return (
{ backButton }
{ selectedAccounts }
{ discoveryStatus }
); } export default AccountSelection;