2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2021-10-28 22:25:34 +02:00
|
|
|
import { Form } from 'component/common/form';
|
|
|
|
import { Lbryio } from 'lbryinc';
|
|
|
|
import { parseURI } from 'util/lbryURI';
|
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';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Button from 'component/button';
|
2020-06-08 20:42:29 +02:00
|
|
|
import Card from 'component/common/card';
|
2021-02-09 17:05:56 +01:00
|
|
|
import ChannelSelector from 'component/channelSelector';
|
2021-10-28 22:25:34 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
2020-09-02 22:08:37 +02:00
|
|
|
import LbcSymbol from 'component/common/lbc-symbol';
|
2021-10-28 22:25:34 +02:00
|
|
|
import React from 'react';
|
2020-06-10 00:09:24 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2021-10-28 22:25:34 +02:00
|
|
|
import WalletTipAmountSelector from 'component/walletTipAmountSelector';
|
2021-09-09 18:52:03 +02:00
|
|
|
|
2021-07-03 19:19:23 +02:00
|
|
|
const TAB_BOOST = 'TabBoost';
|
|
|
|
const TAB_LBC = 'TabLBC';
|
2021-10-28 22:25:34 +02:00
|
|
|
const TAB_FIAT = 'TabFiat';
|
|
|
|
|
2020-06-15 16:04:44 +02:00
|
|
|
type SupportParams = { amount: number, claim_id: string, channel_id?: string };
|
2021-10-28 22:25:34 +02:00
|
|
|
// REMOVE (fiat only)
|
|
|
|
// type TipParams = { tipAmount: number, tipChannelName: string, channelClaimId: string };
|
|
|
|
// type UserParams = { activeChannelName: ?string, activeChannelId: ?string };
|
2020-06-15 16:04:44 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
2021-10-28 22:25:34 +02:00
|
|
|
activeChannelClaim: ?ChannelClaim,
|
2018-05-14 18:19:07 +02:00
|
|
|
balance: number,
|
2021-10-28 22:25:34 +02:00
|
|
|
claim: StreamClaim,
|
|
|
|
claimIsMine: boolean,
|
2020-06-15 16:04:44 +02:00
|
|
|
fetchingChannels: boolean,
|
2021-10-28 22:25:34 +02:00
|
|
|
incognito: boolean,
|
2020-04-25 01:53:57 +02:00
|
|
|
instantTipEnabled: boolean,
|
|
|
|
instantTipMax: { amount: number, currency: string },
|
2021-10-28 22:25:34 +02:00
|
|
|
isPending: boolean,
|
|
|
|
isSupport: boolean,
|
|
|
|
title: string,
|
|
|
|
uri: string,
|
|
|
|
doHideModal: () => void,
|
|
|
|
doSendTip: (SupportParams, boolean) => void,
|
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 {
|
2021-10-28 22:25:34 +02:00
|
|
|
activeChannelClaim,
|
2020-04-25 01:53:57 +02:00
|
|
|
balance,
|
2020-06-08 20:42:29 +02:00
|
|
|
claim = {},
|
2021-10-28 22:25:34 +02:00
|
|
|
claimIsMine,
|
2020-06-15 16:04:44 +02:00
|
|
|
fetchingChannels,
|
2021-02-09 17:05:56 +01:00
|
|
|
incognito,
|
2021-10-28 22:25:34 +02:00
|
|
|
instantTipEnabled,
|
|
|
|
instantTipMax,
|
|
|
|
isPending,
|
|
|
|
title,
|
|
|
|
uri,
|
|
|
|
doHideModal,
|
|
|
|
doSendTip,
|
2020-04-25 01:53:57 +02:00
|
|
|
} = props;
|
2021-09-09 18:52:03 +02:00
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
/** STATE **/
|
2021-09-09 18:52:03 +02:00
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
const [tipAmount, setTipAmount] = usePersistedState('comment-support:customTip', 1.0);
|
|
|
|
const [isOnConfirmationPage, setConfirmationPage] = React.useState(false);
|
2021-09-09 18:52:03 +02:00
|
|
|
const [tipError, setTipError] = React.useState();
|
|
|
|
const [activeTab, setActiveTab] = usePersistedState(TAB_BOOST);
|
2021-10-28 22:25:34 +02:00
|
|
|
const [disableSubmitButton, setDisableSubmitButton] = React.useState();
|
2021-09-09 18:52:03 +02:00
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
/** CONSTS **/
|
2021-09-09 18:52:03 +02:00
|
|
|
|
2021-08-18 00:34:16 +02:00
|
|
|
const claimTypeText = getClaimTypeText();
|
2021-10-28 22:25:34 +02:00
|
|
|
const isSupport = claimIsMine || activeTab === TAB_BOOST;
|
|
|
|
const titleText = claimIsMine
|
|
|
|
? __('Boost Your %claimTypeText%', { claimTypeText })
|
|
|
|
: __('Boost This %claimTypeText%', { claimTypeText });
|
|
|
|
const { claim_id: claimId } = claim;
|
|
|
|
let channelName;
|
|
|
|
try {
|
|
|
|
({ channelName } = parseURI(uri));
|
|
|
|
} catch (e) {}
|
|
|
|
// don't need this - fiat only, for reference REMOVE
|
|
|
|
// const activeChannelName = activeChannelClaim && activeChannelClaim.name;
|
|
|
|
// const activeChannelId = activeChannelClaim && activeChannelClaim.claim_id;
|
|
|
|
//
|
|
|
|
// // setup variables for backend tip API
|
|
|
|
// const channelClaimId = claim.signing_channel ? claim.signing_channel.claim_id : claim.claim_id;
|
|
|
|
// const tipChannelName = claim.signing_channel ? claim.signing_channel.name : claim.name;
|
2021-07-15 14:55:09 +02:00
|
|
|
|
2021-09-09 18:52:03 +02:00
|
|
|
// icon to use or explainer text to show per tab
|
2021-10-28 22:25:34 +02:00
|
|
|
let explainerText = '',
|
|
|
|
confirmLabel = '';
|
|
|
|
switch (activeTab) {
|
|
|
|
case TAB_BOOST:
|
|
|
|
explainerText = __(
|
|
|
|
'This refundable boost will improve the discoverability of this %claimTypeText% while active. ',
|
|
|
|
{ claimTypeText }
|
|
|
|
);
|
|
|
|
confirmLabel = __('Boosting');
|
|
|
|
break;
|
|
|
|
case TAB_FIAT:
|
|
|
|
explainerText = __('Show this channel your appreciation by sending a donation in USD. ');
|
|
|
|
confirmLabel = __('Tipping Fiat (USD)');
|
|
|
|
break;
|
|
|
|
case TAB_LBC:
|
|
|
|
explainerText = __('Show this channel your appreciation by sending a donation of Credits. ');
|
|
|
|
confirmLabel = __('Tipping Credit');
|
|
|
|
break;
|
2021-07-03 19:19:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
/** FUNCTIONS **/
|
2021-07-22 18:44:30 +02:00
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
function getClaimTypeText() {
|
|
|
|
switch (claim.value_type) {
|
|
|
|
case 'stream':
|
|
|
|
return __('Content');
|
|
|
|
case 'channel':
|
|
|
|
return __('Channel');
|
|
|
|
case 'repost':
|
|
|
|
return __('Repost');
|
|
|
|
case 'collection':
|
|
|
|
return __('List');
|
|
|
|
default:
|
|
|
|
return __('Claim');
|
2020-06-10 00:09:24 +02:00
|
|
|
}
|
2021-10-28 22:25:34 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2021-09-09 18:52:03 +02:00
|
|
|
// make call to the backend to send lbc or fiat
|
2020-04-25 01:53:57 +02:00
|
|
|
function sendSupportOrConfirm(instantTipMaxAmount = null) {
|
2021-10-28 22:25:34 +02:00
|
|
|
if (!isOnConfirmationPage && (!instantTipMaxAmount || !instantTipEnabled || tipAmount > instantTipMaxAmount)) {
|
|
|
|
setConfirmationPage(true);
|
2020-04-25 01:53:57 +02:00
|
|
|
} else {
|
2021-10-28 22:25:34 +02:00
|
|
|
const supportParams: SupportParams = {
|
|
|
|
amount: tipAmount,
|
|
|
|
claim_id: claimId,
|
|
|
|
channel_id: activeChannelClaim && !incognito ? activeChannelClaim.claim_id : undefined,
|
|
|
|
};
|
2021-07-03 19:19:23 +02:00
|
|
|
|
|
|
|
// send tip/boost
|
2021-10-28 22:25:34 +02:00
|
|
|
doSendTip(supportParams, isSupport);
|
|
|
|
doHideModal();
|
2020-04-25 01:53:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:44:18 +02:00
|
|
|
|
2021-07-03 19:19:23 +02:00
|
|
|
// when the form button is clicked
|
2020-04-25 01:53:57 +02:00
|
|
|
function handleSubmit() {
|
2021-10-28 22:25:34 +02:00
|
|
|
if (!tipAmount || !claimId) return;
|
|
|
|
|
|
|
|
// send an instant tip (no need to go to an exchange first)
|
|
|
|
if (instantTipEnabled && activeTab !== TAB_FIAT) {
|
|
|
|
if (instantTipMax.currency === 'LBC') {
|
|
|
|
sendSupportOrConfirm(instantTipMax.amount);
|
2020-04-23 18:44:18 +02:00
|
|
|
} else {
|
2021-10-28 22:25:34 +02:00
|
|
|
// 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
|
|
|
}
|
2021-10-28 22:25:34 +02:00
|
|
|
} else {
|
|
|
|
sendSupportOrConfirm();
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 19:19:23 +02:00
|
|
|
function buildButtonText() {
|
|
|
|
// test if frontend will show up as isNan
|
|
|
|
function isNan(tipAmount) {
|
|
|
|
// testing for NaN ES5 style https://stackoverflow.com/a/35912757/3973137
|
|
|
|
// also sometimes it's returned as a string
|
2021-07-05 13:35:17 +02:00
|
|
|
// eslint-disable-next-line
|
2021-10-19 19:57:24 +02:00
|
|
|
return tipAmount !== tipAmount || tipAmount === 'NaN';
|
2021-07-03 19:19:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
function convertToTwoDecimals(number) {
|
|
|
|
return (Math.round(number * 100) / 100).toFixed(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
const amountToShow = activeTab === TAB_FIAT ? convertToTwoDecimals(tipAmount) : tipAmount;
|
|
|
|
|
2021-07-03 19:19:23 +02:00
|
|
|
// if it's a valid number display it, otherwise do an empty string
|
2021-10-28 22:25:34 +02:00
|
|
|
const displayAmount = !isNan(tipAmount) ? amountToShow : '';
|
2021-07-03 19:19:23 +02:00
|
|
|
|
2021-09-09 18:52:03 +02:00
|
|
|
// build button text based on tab
|
2021-10-28 22:25:34 +02:00
|
|
|
switch (activeTab) {
|
|
|
|
case TAB_BOOST:
|
|
|
|
return titleText;
|
|
|
|
// case TAB_FIAT:
|
|
|
|
// return __('Send a $%displayAmount% Tip', { displayAmount });
|
|
|
|
case TAB_LBC:
|
|
|
|
return __('Send a %displayAmount% Credit Tip', { displayAmount });
|
2021-07-03 19:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
/** RENDER **/
|
|
|
|
|
|
|
|
const getTabButton = (tabIcon: string, tabLabel: string, tabName: string) => (
|
|
|
|
<Button
|
|
|
|
key={tabName}
|
|
|
|
icon={tabIcon}
|
|
|
|
label={tabLabel}
|
|
|
|
button="alt"
|
|
|
|
onClick={() => {
|
|
|
|
const tipInputElement = document.getElementById('tip-input');
|
|
|
|
if (tipInputElement) tipInputElement.focus();
|
|
|
|
if (!isOnConfirmationPage) setActiveTab(tabName);
|
|
|
|
}}
|
|
|
|
className={classnames('button-toggle', { 'button-toggle--active': activeTab === tabName })}
|
|
|
|
/>
|
|
|
|
);
|
2021-07-03 19:19:23 +02:00
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
return (
|
2020-06-08 20:42:29 +02:00
|
|
|
<Form onSubmit={handleSubmit}>
|
2021-07-03 19:19:23 +02:00
|
|
|
{/* if there is no LBC balance, show user frontend to get credits */}
|
2021-08-11 22:58:55 +02:00
|
|
|
{/* if there is lbc, the main tip/boost gui with the 3 tabs at the top */}
|
|
|
|
<Card
|
2021-10-28 22:25:34 +02:00
|
|
|
title={<LbcSymbol postfix={titleText} size={22} />}
|
2021-08-11 22:58:55 +02:00
|
|
|
subtitle={
|
2021-10-28 22:25:34 +02:00
|
|
|
<>
|
2021-08-11 22:58:55 +02:00
|
|
|
{!claimIsMine && (
|
|
|
|
<div className="section">
|
|
|
|
{/* tip LBC tab button */}
|
2021-10-28 22:25:34 +02:00
|
|
|
{getTabButton(ICONS.LBC, __('Tip'), TAB_LBC)}
|
|
|
|
|
|
|
|
{/* support LBC tab button */}
|
|
|
|
{getTabButton(ICONS.TRENDING, __('Boost'), TAB_BOOST)}
|
2021-08-11 22:58:55 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* short explainer under the button */}
|
|
|
|
<div className="section__subtitle">
|
2021-10-28 22:25:34 +02:00
|
|
|
{explainerText}
|
2021-08-11 22:58:55 +02:00
|
|
|
{/* {activeTab === TAB_FIAT && !hasCardSaved && <Button navigate={`/$/${PAGES.SETTINGS_STRIPE_CARD}`} label={__('Add A Card')} button="link" />} */}
|
|
|
|
{<Button label={__('Learn more')} button="link" href="https://lbry.com/faq/tipping" />}
|
2020-06-10 00:09:24 +02:00
|
|
|
</div>
|
2021-10-28 22:25:34 +02:00
|
|
|
</>
|
2021-08-11 22:58:55 +02:00
|
|
|
}
|
|
|
|
actions={
|
|
|
|
// confirmation modal, allow user to confirm or cancel transaction
|
2021-10-28 22:25:34 +02:00
|
|
|
isOnConfirmationPage ? (
|
2021-08-11 22:58:55 +02:00
|
|
|
<>
|
|
|
|
<div className="section section--padded card--inline confirm__wrapper">
|
2021-03-19 07:47:29 +01:00
|
|
|
<div className="section">
|
2021-08-11 22:58:55 +02:00
|
|
|
<div className="confirm__label">{__('To --[the tip recipient]--')}</div>
|
|
|
|
<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>
|
2021-10-28 22:25:34 +02:00
|
|
|
<div className="confirm__label">{confirmLabel}</div>
|
2021-08-11 22:58:55 +02:00
|
|
|
<div className="confirm__value">
|
2021-10-19 19:57:24 +02:00
|
|
|
<LbcSymbol postfix={tipAmount} size={22} />
|
2020-06-15 16:04:44 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-08-11 22:58:55 +02:00
|
|
|
</div>
|
|
|
|
<div className="section__actions">
|
2021-08-18 00:34:16 +02:00
|
|
|
<Button autoFocus onClick={handleSubmit} button="primary" disabled={isPending} label={__('Confirm')} />
|
2021-10-28 22:25:34 +02:00
|
|
|
<Button button="link" label={__('Cancel')} onClick={() => setConfirmationPage(false)} />
|
2021-08-11 22:58:55 +02:00
|
|
|
</div>
|
|
|
|
</>
|
2021-10-28 22:25:34 +02:00
|
|
|
) : !((activeTab === TAB_LBC || activeTab === TAB_BOOST) && balance === 0) ? (
|
2021-08-18 00:34:16 +02:00
|
|
|
<>
|
2021-10-28 22:25:34 +02:00
|
|
|
<ChannelSelector />
|
2021-07-03 19:19:23 +02:00
|
|
|
|
2021-08-11 22:58:55 +02:00
|
|
|
{/* section to pick tip/boost amount */}
|
2021-10-28 22:25:34 +02:00
|
|
|
<WalletTipAmountSelector
|
|
|
|
setTipError={setTipError}
|
|
|
|
tipError={tipError}
|
|
|
|
claim={claim}
|
|
|
|
activeTab={TAB_LBC} // active tab
|
|
|
|
amount={tipAmount}
|
|
|
|
onChange={(amount) => setTipAmount(amount)}
|
|
|
|
setDisableSubmitButton={setDisableSubmitButton}
|
|
|
|
/>
|
2021-08-11 22:58:55 +02:00
|
|
|
|
|
|
|
{/* send tip/boost button */}
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
autoFocus
|
|
|
|
icon={isSupport ? ICONS.TRENDING : ICONS.SUPPORT}
|
|
|
|
button="primary"
|
|
|
|
type="submit"
|
2021-10-28 22:25:34 +02:00
|
|
|
disabled={fetchingChannels || isPending || tipError || !tipAmount || disableSubmitButton}
|
2021-08-11 22:58:55 +02:00
|
|
|
label={buildButtonText()}
|
|
|
|
/>
|
|
|
|
{fetchingChannels && <span className="help">{__('Loading your channels...')}</span>}
|
|
|
|
</div>
|
|
|
|
</>
|
2021-08-18 00:34:16 +02:00
|
|
|
) : (
|
|
|
|
// if it's LBC and there is no balance, you can prompt to purchase LBC
|
|
|
|
<Card
|
|
|
|
title={
|
2021-10-28 22:25:34 +02:00
|
|
|
<I18nMessage tokens={{ lbc: <LbcSymbol size={22} /> }}>
|
|
|
|
{__('Supporting content requires %lbc%')}
|
|
|
|
</I18nMessage>
|
2021-08-18 00:34:16 +02:00
|
|
|
}
|
|
|
|
subtitle={
|
|
|
|
<I18nMessage tokens={{ lbc: <LbcSymbol /> }}>
|
2021-10-28 22:25:34 +02:00
|
|
|
{__(
|
|
|
|
'With %lbc%, you can send tips to your favorite creators, or help boost their content for more people to see.'
|
|
|
|
)}
|
2021-08-18 00:34:16 +02:00
|
|
|
</I18nMessage>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
icon={ICONS.REWARDS}
|
|
|
|
button="primary"
|
|
|
|
label={__('Earn Rewards')}
|
|
|
|
navigate={`/$/${PAGES.REWARDS}`}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
icon={ICONS.BUY}
|
|
|
|
button="secondary"
|
|
|
|
label={__('Buy/Swap Credits')}
|
|
|
|
navigate={`/$/${PAGES.BUY}`}
|
|
|
|
/>
|
2021-10-28 22:25:34 +02:00
|
|
|
<Button button="link" label={__('Nevermind')} onClick={doHideModal} />
|
2021-08-18 00:34:16 +02:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
2021-08-11 22:58:55 +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;
|