lbry-desktop/ui/component/common/credit-amount.jsx

103 lines
2.3 KiB
React
Raw Normal View History

2018-03-26 14:32:43 -07:00
// @flow
import React from 'react';
import classnames from 'classnames';
2020-09-02 16:08:37 -04:00
import LbcSymbol from 'component/common/lbc-symbol';
2019-08-27 10:43:42 -04:00
import { formatCredits, formatFullPrice } from 'lbry-redux';
2018-03-26 14:32:43 -07:00
type Props = {
amount: number,
precision: number,
showFree: boolean,
showFullPrice: boolean,
showPlus: boolean,
isEstimate?: boolean,
showLBC?: boolean,
2018-03-26 14:32:43 -07:00
fee?: boolean,
className?: string,
noFormat?: boolean,
2021-04-23 15:59:48 -04:00
size?: number,
superChat?: boolean,
superChatLight?: boolean,
2018-03-26 14:32:43 -07:00
};
class CreditAmount extends React.PureComponent<Props> {
static defaultProps = {
precision: 2,
showFree: false,
showFullPrice: false,
showPlus: false,
showLBC: true,
noFormat: false,
2018-03-26 14:32:43 -07:00
};
render() {
const {
amount,
precision,
showFullPrice,
showFree,
showPlus,
isEstimate,
fee,
showLBC,
className,
noFormat,
2021-04-23 15:59:48 -04:00
size,
superChat,
superChatLight,
} = this.props;
2018-03-26 14:32:43 -07:00
const minimumRenderableAmount = 10 ** (-1 * precision);
const fullPrice = formatFullPrice(amount, 2);
const isFree = parseFloat(amount) === 0;
let formattedAmount;
if (showFullPrice) {
formattedAmount = fullPrice;
} else {
formattedAmount =
amount > 0 && amount < minimumRenderableAmount
? `<${minimumRenderableAmount}`
2019-08-27 10:43:42 -04:00
: formatCredits(amount, precision, true);
2018-03-26 14:32:43 -07:00
}
let amountText;
if (showFree && isFree) {
2019-07-02 17:00:05 -04:00
amountText = __('Free');
2018-03-26 14:32:43 -07:00
} else {
amountText = noFormat ? amount : formattedAmount;
2018-03-26 14:32:43 -07:00
if (showPlus && amount > 0) {
amountText = `+${amountText}`;
}
if (showLBC) {
2021-04-23 15:59:48 -04:00
amountText = <LbcSymbol postfix={amountText} size={size} />;
2018-03-26 14:32:43 -07:00
}
if (fee) {
2020-01-03 16:58:12 -05:00
amountText = __('%amount% fee', { amount: amountText });
2018-03-26 14:32:43 -07:00
}
}
return (
2021-04-23 15:59:48 -04:00
<span
title={fullPrice}
className={classnames(className, {
'super-chat': superChat,
'super-chat--light': superChatLight,
})}
>
2020-05-21 11:38:28 -04:00
<span className="credit-amount">{amountText}</span>
2018-03-26 14:32:43 -07:00
{isEstimate ? (
2019-05-07 17:38:29 -04:00
<span className="credit-amount__estimate" title={__('This is an estimate and does not include data fees')}>
2018-03-26 14:32:43 -07:00
*
</span>
) : null}
</span>
);
}
}
export default CreditAmount;