2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2020-06-08 20:42:29 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-06-10 00:09:24 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2017-12-21 22:08:54 +01:00
|
|
|
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';
|
2020-06-15 16:04:44 +02:00
|
|
|
import { MINIMUM_PUBLISH_BID, CHANNEL_ANONYMOUS } from 'constants/claim';
|
2020-03-13 20:53:53 +01:00
|
|
|
import useIsMobile from 'effects/use-is-mobile';
|
|
|
|
import CreditAmount from 'component/common/credit-amount';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
2020-04-25 01:53:57 +02:00
|
|
|
import { Lbryio } from 'lbryinc';
|
2020-06-08 20:42:29 +02:00
|
|
|
import Card from 'component/common/card';
|
|
|
|
import classnames from 'classnames';
|
2020-06-10 00:09:24 +02:00
|
|
|
import SelectChannel from 'component/selectChannel';
|
|
|
|
import { parseURI } from 'lbry-redux';
|
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-06-08 20:42:29 +02:00
|
|
|
|
2020-06-10 00:09:24 +02:00
|
|
|
const DEFAULT_TIP_AMOUNTS = [5, 25, 100, 1000];
|
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,
|
2020-06-10 00:09:24 +02:00
|
|
|
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,
|
2018-05-14 18:19:07 +02:00
|
|
|
balance: number,
|
2019-07-19 22:21:41 +02:00
|
|
|
isSupport: boolean,
|
2020-06-15 16:04:44 +02:00
|
|
|
fetchingChannels: boolean,
|
2020-04-25 01:53:57 +02:00
|
|
|
instantTipEnabled: boolean,
|
|
|
|
instantTipMax: { amount: number, currency: string },
|
2020-06-10 00:09:24 +02:00
|
|
|
channels: ?Array<ChannelClaim>,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
function WalletSendTip(props: Props) {
|
2020-04-25 01:53:57 +02:00
|
|
|
const {
|
2020-05-11 21:20:37 +02:00
|
|
|
uri,
|
2020-04-25 01:53:57 +02:00
|
|
|
title,
|
|
|
|
isPending,
|
2020-06-10 00:09:24 +02:00
|
|
|
claimIsMine,
|
2020-04-25 01:53:57 +02:00
|
|
|
balance,
|
2020-06-08 20:42:29 +02:00
|
|
|
claim = {},
|
2020-04-25 01:53:57 +02:00
|
|
|
instantTipEnabled,
|
|
|
|
instantTipMax,
|
|
|
|
sendSupport,
|
2020-04-30 00:02:53 +02:00
|
|
|
closeModal,
|
2020-06-10 00:09:24 +02:00
|
|
|
channels,
|
2020-06-15 16:04:44 +02:00
|
|
|
fetchingChannels,
|
2020-04-25 01:53:57 +02:00
|
|
|
} = props;
|
2020-06-17 09:11:57 +02:00
|
|
|
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);
|
2020-03-13 20:53:53 +01:00
|
|
|
const [tipError, setTipError] = React.useState();
|
2020-06-17 09:11:57 +02:00
|
|
|
const [sendAsTip, setSendAsTip] = usePersistedState('comment-support:sendAsTip', true);
|
2020-06-15 16:04:44 +02:00
|
|
|
const [isConfirming, setIsConfirming] = React.useState(false);
|
2020-03-13 20:53:53 +01:00
|
|
|
const isMobile = useIsMobile();
|
2020-06-10 00:09:24 +02:00
|
|
|
const [selectedChannel, setSelectedChannel] = usePersistedState('comment-support:channel');
|
|
|
|
const { claim_id: claimId } = claim;
|
2020-06-15 16:04:44 +02:00
|
|
|
const { channelName } = parseURI(uri);
|
|
|
|
|
2020-06-10 00:09:24 +02:00
|
|
|
const channelStrings = channels && channels.map(channel => channel.permanent_url).join(',');
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!selectedChannel && channelStrings) {
|
|
|
|
const channels = channelStrings.split(',');
|
|
|
|
const newChannelUrl = channels[0];
|
|
|
|
const { claimName } = parseURI(newChannelUrl);
|
|
|
|
setSelectedChannel(claimName);
|
|
|
|
}
|
|
|
|
}, [channelStrings]);
|
|
|
|
|
2020-06-17 09:11:57 +02:00
|
|
|
const tipAmount = useCustomTip ? customTipAmount : presetTipAmount;
|
|
|
|
const isSupport = claimIsMine ? true : !sendAsTip;
|
|
|
|
|
2020-06-10 00:09:24 +02:00
|
|
|
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) {
|
|
|
|
tipError = __('Not enough credits');
|
|
|
|
}
|
|
|
|
setTipError(tipError);
|
|
|
|
}, [tipAmount, balance, setTipError]);
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2020-04-25 01:53:57 +02:00
|
|
|
function sendSupportOrConfirm(instantTipMaxAmount = null) {
|
2020-06-15 16:04:44 +02:00
|
|
|
let selectedChannelId;
|
|
|
|
if (selectedChannel !== CHANNEL_ANONYMOUS) {
|
|
|
|
const selectedChannelClaim = channels && channels.find(channelClaim => channelClaim.name === selectedChannel);
|
|
|
|
|
|
|
|
if (selectedChannelClaim) {
|
|
|
|
selectedChannelId = selectedChannelClaim.claim_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!isSupport &&
|
|
|
|
!isConfirming &&
|
|
|
|
(!instantTipMaxAmount || !instantTipEnabled || tipAmount > instantTipMaxAmount)
|
|
|
|
) {
|
|
|
|
setIsConfirming(true);
|
2020-04-25 01:53:57 +02:00
|
|
|
} 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();
|
2020-04-25 01:53:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:44:18 +02:00
|
|
|
|
2020-04-25 01:53:57 +02:00
|
|
|
function handleSubmit() {
|
2020-04-23 10:18:17 +02:00
|
|
|
if (tipAmount && claimId) {
|
2020-04-25 01:53:57 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2020-04-23 18:44:18 +02:00
|
|
|
} else {
|
2020-04-25 01:53:57 +02:00
|
|
|
sendSupportOrConfirm();
|
2020-04-23 18:44:18 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:11:57 +02:00
|
|
|
function handleCustomPriceChange(event: SyntheticInputEvent<*>) {
|
2018-06-18 08:34:59 +02:00
|
|
|
const tipAmount = parseFloat(event.target.value);
|
2020-06-17 09:11:57 +02:00
|
|
|
setCustomTipAmount(tipAmount);
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
return (
|
2020-06-08 20:42:29 +02:00
|
|
|
<Form onSubmit={handleSubmit}>
|
2020-06-10 00:09:24 +02:00
|
|
|
{balance === 0 ? (
|
|
|
|
<Card
|
|
|
|
title={__('Supporting Content Requires LBC')}
|
|
|
|
subtitle={__(
|
|
|
|
'With LBC, you can send tips to your favorite creators, or help boost their content for more people to see.'
|
|
|
|
)}
|
|
|
|
actions={
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button icon={ICONS.BUY} button="primary" label={__('Buy LBC')} navigate={`/$/${PAGES.BUY}`} />
|
|
|
|
<Button button="link" label={__('Nevermind')} onClick={closeModal} />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Card
|
|
|
|
title={claimIsMine ? __('Boost Your Content') : __('Support This Content')}
|
|
|
|
subtitle={
|
|
|
|
<React.Fragment>
|
|
|
|
{__(
|
|
|
|
'This will increase the overall bid amount for this content, which will boost its ability to be discovered while active.'
|
|
|
|
)}{' '}
|
|
|
|
<Button label={__('Learn more')} button="link" href="https://lbry.com/faq/tipping" />.
|
|
|
|
</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')}</div>
|
|
|
|
<div className="confirm__value">{channelName || title}</div>
|
|
|
|
<div className="confirm__label">{__('From')}</div>
|
|
|
|
<div className="confirm__value">{selectedChannel}</div>
|
|
|
|
<div className="confirm__label">{__(isSupport ? 'Supporting' : 'Tipping')}</div>
|
|
|
|
<div className="confirm__value">{tipAmount} LBC</div>
|
|
|
|
</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">
|
|
|
|
<SelectChannel
|
2020-06-17 09:11:57 +02:00
|
|
|
label={__('Channel to show support as')}
|
2020-06-15 16:04:44 +02:00
|
|
|
channel={selectedChannel}
|
|
|
|
onChannelChange={newChannel => setSelectedChannel(newChannel)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="section">
|
|
|
|
{DEFAULT_TIP_AMOUNTS.map(amount => (
|
|
|
|
<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,
|
|
|
|
})}
|
|
|
|
label={`${amount} LBC`}
|
2020-06-17 09:11:57 +02:00
|
|
|
onClick={() => {
|
|
|
|
setPresetTipAmount(amount);
|
|
|
|
setUseCustomTip(false);
|
|
|
|
}}
|
2020-06-15 16:04:44 +02:00
|
|
|
/>
|
|
|
|
))}
|
2020-06-10 00:09:24 +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-06-10 00:09:24 +02:00
|
|
|
})}
|
2020-06-15 16:04:44 +02:00
|
|
|
label={__('Custom')}
|
2020-06-17 09:11:57 +02:00
|
|
|
onClick={() => setUseCustomTip(true)}
|
2020-06-10 00:09:24 +02:00
|
|
|
/>
|
2020-06-15 16:04:44 +02:00
|
|
|
</div>
|
|
|
|
|
2020-06-17 09:11:57 +02:00
|
|
|
{useCustomTip && (
|
2020-06-15 16:04:44 +02:00
|
|
|
<div className="section">
|
|
|
|
<FormField
|
|
|
|
autoFocus
|
|
|
|
name="tip-input"
|
|
|
|
label={
|
|
|
|
<React.Fragment>
|
2020-06-17 09:11:57 +02:00
|
|
|
{__('Custom support amount')}{' '}
|
2020-06-15 16:04:44 +02:00
|
|
|
{isMobile && (
|
|
|
|
<I18nMessage tokens={{ lbc_balance: <CreditAmount badge={false} amount={balance} /> }}>
|
|
|
|
(%lbc_balance% available)
|
|
|
|
</I18nMessage>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
className="form-field--price-amount"
|
|
|
|
error={tipError}
|
|
|
|
min="0"
|
|
|
|
step="any"
|
|
|
|
type="number"
|
|
|
|
placeholder="1.23"
|
2020-06-17 09:11:57 +02:00
|
|
|
value={customTipAmount}
|
|
|
|
onChange={event => handleCustomPriceChange(event)}
|
2020-06-15 16:04:44 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
2020-06-10 00:09:24 +02:00
|
|
|
autoFocus
|
2020-06-15 16:04:44 +02:00
|
|
|
icon={isSupport ? undefined : ICONS.SUPPORT}
|
|
|
|
button="primary"
|
|
|
|
type="submit"
|
|
|
|
disabled={fetchingChannels || isPending || tipError || !tipAmount}
|
2020-06-10 00:09:24 +02:00
|
|
|
label={
|
2020-06-15 16:04:44 +02:00
|
|
|
isSupport
|
2020-06-22 15:33:23 +02:00
|
|
|
? __('Send Revocable Support')
|
2020-06-15 16:04:44 +02:00
|
|
|
: __('Send a %amount% Tip', { amount: tipAmount ? `${tipAmount} LBC` : '' })
|
2020-06-10 00:09:24 +02:00
|
|
|
}
|
|
|
|
/>
|
2020-06-15 16:04:44 +02:00
|
|
|
{fetchingChannels && <span className="help">{__('Loading your channels...')}</span>}
|
|
|
|
{!claimIsMine && !fetchingChannels && (
|
|
|
|
<FormField
|
|
|
|
name="toggle-is-support"
|
|
|
|
type="checkbox"
|
|
|
|
label={__('Make this support permanent')}
|
2020-06-17 09:11:57 +02:00
|
|
|
checked={sendAsTip}
|
|
|
|
onChange={() => setSendAsTip(!sendAsTip)}
|
2020-06-15 16:04:44 +02:00
|
|
|
/>
|
|
|
|
)}
|
2020-06-10 00:09:24 +02:00
|
|
|
</div>
|
2020-06-15 16:04:44 +02:00
|
|
|
{DEFAULT_TIP_AMOUNTS.some(val => val > balance) && (
|
|
|
|
<div className="section">
|
2020-06-15 20:52:21 +02:00
|
|
|
<Button button="link" label={__('Buy More LBC')} navigate={`/$/${PAGES.BUY}`} />
|
2020-06-15 16:04:44 +02:00
|
|
|
</div>
|
2020-06-10 00:09:24 +02:00
|
|
|
)}
|
2020-06-15 16:04:44 +02:00
|
|
|
</>
|
|
|
|
)
|
2020-06-10 00:09:24 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
2020-06-08 20:42:29 +02:00
|
|
|
</Form>
|
2020-03-13 20:53:53 +01:00
|
|
|
);
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default WalletSendTip;
|