ghost-portal/packages/ui-app/src/Balance.tsx
Jaco Greeff 014ae780e4
Split ui-api, update API version (#589)
* Split ui-api, update API version

* Update (awaiting API changes to go through)

* Cleanup ui-api exports

* Bump API (SubscriptionPromise support)

* Linting fixes

* Update renames

* Do display balances.Transfer (shows for non-owned accounts)

* Bump API
2019-01-12 09:38:07 +01:00

70 lines
1.8 KiB
TypeScript

// Copyright 2017-2019 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BareProps } from './types';
import BN from 'bn.js';
import React from 'react';
import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types';
import RxBalance from '@polkadot/ui-reactive/Balance';
import { balanceFormat } from '@polkadot/ui-reactive/util/index';
import classes from './util/classes';
export type Props = BareProps & {
balance?: Balance | Array<Balance> | BN,
label?: string,
value?: AccountId | AccountIndex | Address | string | Uint8Array | null,
withLabel?: boolean
};
export default class BalanceDisplay extends React.PureComponent<Props> {
render () {
const { balance, className, label, value, style } = this.props;
if (!value) {
return null;
}
return balance
? this.renderProvided()
: (
<RxBalance
className={classes('ui--Balance', className)}
label={label}
params={value}
style={style}
/>
);
}
private renderProvided () {
const { balance, className, label, style } = this.props;
if (!balance) {
return null;
}
let value = `${balanceFormat(Array.isArray(balance) ? balance[0] : balance)}`;
if (Array.isArray(balance)) {
const totals = balance.filter((value, index) => index !== 0);
const total = totals.reduce((total, value) => total.add(value), new BN(0)).gtn(0)
? `(+${totals.map((balance) => balanceFormat(balance)).join(', ')})`
: '';
value = `${value} ${total}`;
}
return (
<div
className={classes('ui--Balance', className)}
style={style}
>
{label}{value}
</div>
);
}
}