2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2022-01-14 21:24:16 +01:00
|
|
|
import 'scss/component/_superchat.scss';
|
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
import { formatCredits, formatFullPrice } from 'util/format-credits';
|
2018-03-26 23:32:43 +02:00
|
|
|
import classnames from 'classnames';
|
2020-09-02 22:08:37 +02:00
|
|
|
import LbcSymbol from 'component/common/lbc-symbol';
|
2021-11-05 20:31:51 +01:00
|
|
|
import React from 'react';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
type Props = {
|
2021-11-05 20:31:51 +01:00
|
|
|
amount?: number,
|
|
|
|
className?: string,
|
|
|
|
customAmounts?: { amountFiat: number, amountLBC: number },
|
|
|
|
fee?: boolean,
|
2021-12-28 16:51:37 +01:00
|
|
|
hideTitle?: boolean,
|
2021-11-05 20:31:51 +01:00
|
|
|
isEstimate?: boolean,
|
|
|
|
isFiat?: boolean,
|
|
|
|
noFormat?: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
precision: number,
|
|
|
|
showFree: boolean,
|
|
|
|
showFullPrice: boolean,
|
2018-07-25 02:50:04 +02:00
|
|
|
showLBC?: boolean,
|
2021-11-05 20:31:51 +01:00
|
|
|
showPlus: boolean,
|
2021-04-23 21:59:48 +02:00
|
|
|
size?: number,
|
|
|
|
superChat?: boolean,
|
|
|
|
superChatLight?: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class CreditAmount extends React.PureComponent<Props> {
|
|
|
|
static defaultProps = {
|
2021-11-05 20:31:51 +01:00
|
|
|
noFormat: false,
|
2018-03-26 23:32:43 +02:00
|
|
|
precision: 2,
|
|
|
|
showFree: false,
|
|
|
|
showFullPrice: false,
|
2018-07-25 02:50:04 +02:00
|
|
|
showLBC: true,
|
2021-11-05 20:31:51 +01:00
|
|
|
showPlus: false,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-09-15 21:46:36 +02:00
|
|
|
const {
|
|
|
|
amount,
|
2021-11-05 20:31:51 +01:00
|
|
|
className,
|
|
|
|
customAmounts,
|
|
|
|
fee,
|
2021-12-28 16:51:37 +01:00
|
|
|
hideTitle,
|
2021-11-05 20:31:51 +01:00
|
|
|
isEstimate,
|
|
|
|
isFiat,
|
|
|
|
noFormat,
|
2020-09-15 21:46:36 +02:00
|
|
|
precision,
|
|
|
|
showFree,
|
2021-11-05 20:31:51 +01:00
|
|
|
showFullPrice,
|
2020-09-15 21:46:36 +02:00
|
|
|
showLBC,
|
2021-11-05 20:31:51 +01:00
|
|
|
showPlus,
|
2021-04-23 21:59:48 +02:00
|
|
|
size,
|
|
|
|
superChat,
|
|
|
|
superChatLight,
|
2020-09-15 21:46:36 +02:00
|
|
|
} = this.props;
|
2018-03-26 23:32:43 +02:00
|
|
|
const minimumRenderableAmount = 10 ** (-1 * precision);
|
2021-08-18 20:06:12 +02:00
|
|
|
|
|
|
|
// return null, otherwise it will try and convert undefined to a string
|
2021-11-05 20:31:51 +01:00
|
|
|
if (amount === undefined && customAmounts === undefined) return null;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
function getAmountText(amount: number, isFiat?: boolean) {
|
|
|
|
const fullPrice = formatFullPrice(amount, 2);
|
|
|
|
const isFree = parseFloat(amount) === 0;
|
|
|
|
let formattedAmount;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
if (showFullPrice) {
|
|
|
|
formattedAmount = fullPrice;
|
|
|
|
} else {
|
|
|
|
formattedAmount =
|
|
|
|
amount > 0 && amount < minimumRenderableAmount
|
|
|
|
? `<${minimumRenderableAmount}`
|
|
|
|
: formatCredits(amount, precision, true);
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
if (showFree && isFree) {
|
|
|
|
return __('Free');
|
|
|
|
} else {
|
|
|
|
let amountText = noFormat ? amount : formattedAmount;
|
|
|
|
|
|
|
|
if (showPlus && amount > 0) {
|
|
|
|
amountText = `+${amountText}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showLBC && !isFiat) {
|
|
|
|
amountText = <LbcSymbol postfix={amountText} size={size} />;
|
|
|
|
} else if (showLBC && isFiat) {
|
2022-04-20 01:22:47 +02:00
|
|
|
amountText = (
|
|
|
|
<p style={{ display: 'inline' }}>
|
|
|
|
${isNaN(Number(amountText)) ? amountText : (Math.round(Number(amount) * 100) / 100).toFixed(2)}
|
|
|
|
</p>
|
|
|
|
);
|
2021-11-05 20:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fee) {
|
|
|
|
amountText = __('%amount% fee', { amount: amountText });
|
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
return amountText;
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-04-23 21:59:48 +02:00
|
|
|
<span
|
2021-12-28 16:51:37 +01:00
|
|
|
title={amount && !hideTitle ? formatFullPrice(amount, 2) : ''}
|
2022-01-14 21:24:16 +01:00
|
|
|
className={classnames(className, { superChat: superChat, 'superChat--light': superChatLight })}
|
2021-04-23 21:59:48 +02:00
|
|
|
>
|
2021-11-05 20:31:51 +01:00
|
|
|
{customAmounts
|
|
|
|
? Object.values(customAmounts).map((amount, index) => (
|
|
|
|
<span key={String(amount)} className="credit-amount">
|
|
|
|
{getAmountText(Number(amount), !index)}
|
|
|
|
</span>
|
|
|
|
))
|
|
|
|
: amount && <span className="credit-amount">{getAmountText(amount, isFiat)}</span>}
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
{isEstimate ? (
|
2019-05-07 23:38:29 +02:00
|
|
|
<span className="credit-amount__estimate" title={__('This is an estimate and does not include data fees')}>
|
2018-03-26 23:32:43 +02:00
|
|
|
*
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CreditAmount;
|