lbry-desktop/ui/component/walletSendTip/view.jsx

317 lines
12 KiB
React
Raw Permalink Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2020-06-08 20:42:29 +02:00
import * as ICONS from 'constants/icons';
import * as PAGES from 'constants/pages';
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2019-02-13 17:27:20 +01:00
import { FormField, Form } from 'component/common/form';
import { MINIMUM_PUBLISH_BID } from 'constants/claim';
import CreditAmount from 'component/common/credit-amount';
import I18nMessage from 'component/i18nMessage';
import { Lbryio } from 'lbryinc';
2020-06-08 20:42:29 +02:00
import Card from 'component/common/card';
import classnames from 'classnames';
import ChannelSelector from 'component/channelSelector';
2020-09-02 22:08:37 +02:00
import LbcSymbol from 'component/common/lbc-symbol';
import { parseURI } from 'lbry-redux';
import usePersistedState from 'effects/use-persisted-state';
import WalletSpendableBalanceHelp from 'component/walletSpendableBalanceHelp';
2020-06-08 20:42:29 +02:00
const DEFAULT_TIP_AMOUNTS = [1, 5, 25, 100];
2017-09-17 22:33:52 +02:00
2020-06-15 16:04:44 +02:00
type SupportParams = { amount: number, claim_id: string, channel_id?: string };
2018-03-26 23:32:43 +02:00
type Props = {
uri: string,
claimIsMine: boolean,
2018-03-26 23:32:43 +02:00
title: string,
2019-04-24 16:02:08 +02:00
claim: StreamClaim,
2018-03-26 23:32:43 +02:00
isPending: boolean,
2020-06-15 16:04:44 +02:00
sendSupport: (SupportParams, boolean) => void,
2020-04-30 00:02:53 +02:00
closeModal: () => void,
balance: number,
isSupport: boolean,
2020-06-15 16:04:44 +02:00
fetchingChannels: boolean,
instantTipEnabled: boolean,
instantTipMax: { amount: number, currency: string },
activeChannelClaim: ?ChannelClaim,
incognito: boolean,
2018-03-26 23:32:43 +02:00
};
function WalletSendTip(props: Props) {
const {
uri,
title,
isPending,
claimIsMine,
balance,
2020-06-08 20:42:29 +02:00
claim = {},
instantTipEnabled,
instantTipMax,
sendSupport,
2020-04-30 00:02:53 +02:00
closeModal,
2020-06-15 16:04:44 +02:00
fetchingChannels,
incognito,
activeChannelClaim,
} = props;
const [presetTipAmount, setPresetTipAmount] = usePersistedState('comment-support:presetTip', DEFAULT_TIP_AMOUNTS[0]);
const [customTipAmount, setCustomTipAmount] = usePersistedState('comment-support:customTip', 1.0);
const [useCustomTip, setUseCustomTip] = usePersistedState('comment-support:useCustomTip', false);
const [tipError, setTipError] = React.useState();
const [sendAsTip, setSendAsTip] = usePersistedState('comment-support:sendAsTip', true);
2020-06-15 16:04:44 +02:00
const [isConfirming, setIsConfirming] = React.useState(false);
const { claim_id: claimId } = claim;
2020-06-15 16:04:44 +02:00
const { channelName } = parseURI(uri);
2020-08-26 22:28:33 +02:00
const noBalance = balance === 0;
const tipAmount = useCustomTip ? customTipAmount : presetTipAmount;
const isSupport = claimIsMine || !sendAsTip;
React.useEffect(() => {
const regexp = RegExp(/^(\d*([.]\d{0,8})?)$/);
const validTipInput = regexp.test(String(tipAmount));
let tipError;
if (!tipAmount) {
tipError = __('Amount must be a number');
} else if (tipAmount <= 0) {
tipError = __('Amount must be a positive number');
} else if (tipAmount < MINIMUM_PUBLISH_BID) {
tipError = __('Amount must be higher');
} else if (!validTipInput) {
tipError = __('Amount must have no more than 8 decimal places');
} else if (tipAmount === balance) {
tipError = __('Please decrease the amount to account for transaction fees');
} else if (tipAmount > balance) {
2020-09-04 19:14:48 +02:00
tipError = __('Not enough Credits');
}
setTipError(tipError);
}, [tipAmount, balance, setTipError]);
2018-03-26 23:32:43 +02:00
function sendSupportOrConfirm(instantTipMaxAmount = null) {
2020-06-15 16:04:44 +02:00
let selectedChannelId;
if (!incognito && activeChannelClaim) {
selectedChannelId = activeChannelClaim.claim_id;
2020-06-15 16:04:44 +02:00
}
if (
!isSupport &&
!isConfirming &&
(!instantTipMaxAmount || !instantTipEnabled || tipAmount > instantTipMaxAmount)
) {
setIsConfirming(true);
} else {
2020-06-15 16:04:44 +02:00
const supportParams: SupportParams = { amount: tipAmount, claim_id: claimId };
if (selectedChannelId) {
supportParams.channel_id = selectedChannelId;
}
sendSupport(supportParams, isSupport);
2020-04-30 00:02:53 +02:00
closeModal();
}
}
function handleSubmit() {
if (tipAmount && claimId) {
if (instantTipEnabled) {
if (instantTipMax.currency === 'LBC') {
sendSupportOrConfirm(instantTipMax.amount);
} else {
// Need to convert currency of instant purchase maximum before trying to send support
Lbryio.getExchangeRates().then(({ LBC_USD }) => {
sendSupportOrConfirm(instantTipMax.amount / LBC_USD);
});
}
} else {
sendSupportOrConfirm();
}
2018-03-26 23:32:43 +02:00
}
2017-09-17 22:33:52 +02:00
}
function handleCustomPriceChange(event: SyntheticInputEvent<*>) {
const tipAmount = parseFloat(event.target.value);
setCustomTipAmount(tipAmount);
2017-09-17 22:33:52 +02:00
}
return (
2020-06-08 20:42:29 +02:00
<Form onSubmit={handleSubmit}>
2020-08-26 22:28:33 +02:00
{noBalance ? (
<Card
2020-09-02 22:08:37 +02:00
title={<I18nMessage tokens={{ lbc: <LbcSymbol size={22} /> }}>Supporting content requires %lbc%</I18nMessage>}
subtitle={
<I18nMessage tokens={{ lbc: <LbcSymbol /> }}>
With %lbc%, you can send tips to your favorite creators, or help boost their content for more people to
see.
</I18nMessage>
}
actions={
<div className="section__actions">
2020-08-26 22:28:33 +02:00
<Button
icon={ICONS.REWARDS}
button="primary"
label={__('Earn Rewards')}
navigate={`/$/${PAGES.REWARDS}`}
/>
<Button icon={ICONS.BUY} button="secondary" label={__('Buy Credits')} navigate={`/$/${PAGES.BUY}`} />
<Button button="link" label={__('Nevermind')} onClick={closeModal} />
</div>
}
/>
) : (
<Card
title={<LbcSymbol postfix={claimIsMine ? __('Boost your content') : __('Support this content')} size={22} />}
subtitle={
<React.Fragment>
{!claimIsMine && (
<div className="section">
<Button
key="tip"
icon={ICONS.SUPPORT}
label={__('Tip')}
button="alt"
onClick={() => setSendAsTip(true)}
className={classnames('button-toggle', { 'button-toggle--active': sendAsTip })}
/>
<Button
key="boost"
icon={ICONS.TRENDING}
label={__('Boost')}
button="alt"
onClick={() => setSendAsTip(false)}
className={classnames('button-toggle', { 'button-toggle--active': !sendAsTip })}
/>
</div>
)}
<div className="section__subtitle">
{isSupport
? __(
'This will increase the overall bid amount for this content, which will boost its ability to be discovered while active.'
)
: __('Show this channel your appreciation by sending a donation.')}{' '}
<Button label={__('Learn more')} button="link" href="https://lbry.com/faq/tipping" />
</div>
</React.Fragment>
}
actions={
2020-06-15 16:04:44 +02:00
isConfirming ? (
<>
<div className="section section--padded card--inline confirm__wrapper">
<div className="section">
<div className="confirm__label">{__('To --[the tip recipient]--')}</div>
2020-06-15 16:04:44 +02:00
<div className="confirm__value">{channelName || title}</div>
<div className="confirm__label">{__('From --[the tip sender]--')}</div>
<div className="confirm__value">
{activeChannelClaim && !incognito ? activeChannelClaim.name : __('Anonymous')}
</div>
<div className="confirm__label">{__(isSupport ? 'Boosting' : 'Tipping')}</div>
2020-09-02 22:08:37 +02:00
<div className="confirm__value">
2020-09-10 17:54:41 +02:00
<LbcSymbol postfix={tipAmount} size={22} />
2020-09-02 22:08:37 +02:00
</div>
2020-06-15 16:04:44 +02:00
</div>
</div>
<div className="section__actions">
2020-06-15 16:11:29 +02:00
<Button
autoFocus
onClick={handleSubmit}
button="primary"
disabled={isPending}
label={__('Confirm')}
/>
2020-06-15 16:04:44 +02:00
<Button button="link" label={__('Cancel')} onClick={() => setIsConfirming(false)} />
</div>
</>
) : (
<>
<div className="section">
<ChannelSelector />
2020-06-15 16:04:44 +02:00
</div>
<div className="section">
{DEFAULT_TIP_AMOUNTS.map((amount) => (
2020-06-15 16:04:44 +02:00
<Button
key={amount}
disabled={amount > balance}
button="alt"
2020-06-15 16:41:59 +02:00
className={classnames('button-toggle button-toggle--expandformobile', {
2020-06-15 16:04:44 +02:00
'button-toggle--active': tipAmount === amount,
'button-toggle--disabled': amount > balance,
})}
2020-09-02 22:08:37 +02:00
label={amount}
2020-09-10 17:54:41 +02:00
icon={ICONS.LBC}
onClick={() => {
setPresetTipAmount(amount);
setUseCustomTip(false);
}}
2020-06-15 16:04:44 +02:00
/>
))}
<Button
button="alt"
2020-06-15 16:41:59 +02:00
className={classnames('button-toggle button-toggle--expandformobile', {
2020-06-15 16:04:44 +02:00
'button-toggle--active': !DEFAULT_TIP_AMOUNTS.includes(tipAmount),
})}
2020-09-10 17:54:41 +02:00
icon={ICONS.LBC}
2020-06-15 16:04:44 +02:00
label={__('Custom')}
onClick={() => setUseCustomTip(true)}
/>
{DEFAULT_TIP_AMOUNTS.some((val) => val > balance) && (
<Button
button="secondary"
className="button-toggle-group-action"
icon={ICONS.BUY}
2020-09-02 22:08:37 +02:00
title={__('Buy more LBRY Credits')}
navigate={`/$/${PAGES.BUY}`}
/>
)}
2020-06-15 16:04:44 +02:00
</div>
{useCustomTip && (
2020-06-15 16:04:44 +02:00
<div className="section">
<FormField
autoFocus
name="tip-input"
label={
<React.Fragment>
{__('Custom support amount')}{' '}
2020-09-10 17:54:41 +02:00
<I18nMessage
tokens={{ lbc_balance: <CreditAmount precision={4} amount={balance} showLBC={false} /> }}
>
(%lbc_balance% Credits available)
</I18nMessage>
2020-06-15 16:04:44 +02:00
</React.Fragment>
}
className="form-field--price-amount"
error={tipError}
min="0"
step="any"
type="number"
placeholder="1.23"
value={customTipAmount}
onChange={(event) => handleCustomPriceChange(event)}
2020-06-15 16:04:44 +02:00
/>
</div>
)}
<div className="section__actions">
<Button
autoFocus
icon={isSupport ? ICONS.TRENDING : ICONS.SUPPORT}
2020-06-15 16:04:44 +02:00
button="primary"
type="submit"
disabled={fetchingChannels || isPending || tipError || !tipAmount}
label={
2020-06-15 16:04:44 +02:00
isSupport
? __('Boost This Content')
2020-09-02 22:08:37 +02:00
: __('Send a %amount% Tip', { amount: tipAmount ? `${tipAmount} Credit` : '' })
}
/>
2020-06-15 16:04:44 +02:00
{fetchingChannels && <span className="help">{__('Loading your channels...')}</span>}
</div>
<WalletSpendableBalanceHelp />
2020-06-15 16:04:44 +02:00
</>
)
}
/>
)}
2020-06-08 20:42:29 +02:00
</Form>
);
2017-09-17 22:33:52 +02:00
}
export default WalletSendTip;