2020-03-25 04:15:05 +01:00
|
|
|
// @flow
|
|
|
|
import * as ICONS from 'constants/icons';
|
2022-07-24 00:33:00 +02:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2020-03-25 04:15:05 +01:00
|
|
|
import CreditAmount from 'component/common/credit-amount';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import { Form, FormField } from 'component/common/form';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
2020-04-13 21:42:15 +02:00
|
|
|
import ErrorText from 'component/common/error-text';
|
2020-03-25 04:15:05 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
balance: number,
|
|
|
|
totalBalance: number,
|
|
|
|
claimsBalance: number,
|
|
|
|
supportsBalance: number,
|
|
|
|
tipsBalance: number,
|
|
|
|
claim: any,
|
|
|
|
metaData: any,
|
|
|
|
handleClose: () => void,
|
|
|
|
abandonSupportForClaim: (string, string, boolean | string, boolean) => any,
|
|
|
|
abandonClaimError: ?string,
|
|
|
|
};
|
|
|
|
|
|
|
|
const SupportsLiquidate = (props: Props) => {
|
|
|
|
const { claim, abandonSupportForClaim, handleClose, abandonClaimError } = props;
|
|
|
|
const [previewBalance, setPreviewBalance] = useState(undefined);
|
2022-07-24 00:33:00 +02:00
|
|
|
const [defaultValueAssigned, setDefaultValueAssigned] = useState(false);
|
|
|
|
const [unlockTextAmount, setUnlockTextAmount] = useState('');
|
2020-03-25 04:15:05 +01:00
|
|
|
const [error, setError] = useState(false);
|
2020-04-02 19:03:13 +02:00
|
|
|
const initialMessage = __('How much would you like to unlock?');
|
2020-03-25 04:15:05 +01:00
|
|
|
const [message, setMessage] = useState(initialMessage);
|
2022-07-24 00:33:00 +02:00
|
|
|
const amount = Number(unlockTextAmount) || 0;
|
|
|
|
const defaultValue = previewBalance ? previewBalance * 0.25 : 0;
|
2020-03-25 04:15:05 +01:00
|
|
|
const keep =
|
2020-04-16 19:21:23 +02:00
|
|
|
amount >= 0
|
2022-07-24 00:33:00 +02:00
|
|
|
? Boolean(previewBalance) && Number.parseFloat(String(Number(previewBalance) - amount)).toFixed(8)
|
|
|
|
: Boolean(previewBalance) && Number.parseFloat(String(defaultValue * 3)).toFixed(8);
|
2020-03-25 04:15:05 +01:00
|
|
|
const claimId = claim && claim.claim_id;
|
|
|
|
const type = claim.value_type;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (claimId && abandonSupportForClaim) {
|
2022-07-24 00:33:00 +02:00
|
|
|
abandonSupportForClaim(claimId, type, false, true).then((r) => {
|
2020-03-25 04:15:05 +01:00
|
|
|
setPreviewBalance(r.total_input);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [abandonSupportForClaim, claimId, type, setPreviewBalance]);
|
|
|
|
|
|
|
|
function handleSubmit() {
|
2022-07-24 00:33:00 +02:00
|
|
|
abandonSupportForClaim(claimId, type, keep, false).then((r) => {
|
2020-03-25 04:15:05 +01:00
|
|
|
if (r) {
|
|
|
|
handleClose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-24 00:33:00 +02:00
|
|
|
const handleRangeChange = useCallback(
|
|
|
|
(newValue) => {
|
|
|
|
setUnlockTextAmount(String(newValue));
|
|
|
|
},
|
|
|
|
[setUnlockTextAmount]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleChangeUnlockText = useCallback(
|
|
|
|
(newValue) => {
|
|
|
|
// Get rid of all characters except digits, commas and periods.
|
|
|
|
const onlyValidAmount = newValue.replace(/[^0-9.,]+/, '');
|
|
|
|
setUnlockTextAmount(onlyValidAmount);
|
|
|
|
},
|
|
|
|
[setUnlockTextAmount]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleUnlockTextFocus = useCallback(() => {
|
|
|
|
// Get rid of empty zero when user starts typing (small ux improvement)
|
|
|
|
if (Number(unlockTextAmount) === 0) {
|
|
|
|
setUnlockTextAmount('');
|
|
|
|
}
|
|
|
|
}, [unlockTextAmount, setUnlockTextAmount]);
|
|
|
|
|
|
|
|
const handleUnlockTextBlur = useCallback(() => {
|
|
|
|
if (!unlockTextAmount || isNaN(Number(unlockTextAmount))) {
|
|
|
|
setUnlockTextAmount(previewBalance ? String(defaultValue) : '0');
|
|
|
|
}
|
|
|
|
}, [unlockTextAmount, setUnlockTextAmount, previewBalance, defaultValue]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (defaultValueAssigned || !previewBalance || unlockTextAmount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setUnlockTextAmount(String(defaultValue));
|
|
|
|
setDefaultValueAssigned(true);
|
|
|
|
}, [defaultValueAssigned, previewBalance, unlockTextAmount, setUnlockTextAmount, setDefaultValueAssigned]);
|
|
|
|
|
|
|
|
// Update message & error based on unlock amount.
|
|
|
|
useEffect(() => {
|
|
|
|
const unlockAmount = Number(unlockTextAmount);
|
|
|
|
const previewBalanceNumber = Number(previewBalance);
|
|
|
|
if (unlockTextAmount && isNaN(unlockAmount)) {
|
2020-03-25 04:15:05 +01:00
|
|
|
setMessage(__('Amount must be a number'));
|
|
|
|
setError(true);
|
2022-07-24 00:33:00 +02:00
|
|
|
} else if (unlockAmount > previewBalanceNumber) {
|
2020-03-25 04:15:05 +01:00
|
|
|
setMessage(__('Amount cannot be more than available'));
|
2022-07-24 00:33:00 +02:00
|
|
|
setError(true);
|
|
|
|
} else if (Math.abs(unlockAmount - previewBalanceNumber) <= Number.EPSILON) {
|
2020-03-25 04:15:05 +01:00
|
|
|
setMessage(__(`She's about to close up the library!`));
|
|
|
|
setError(false);
|
2022-07-24 00:33:00 +02:00
|
|
|
} else if (unlockAmount > previewBalanceNumber / 2) {
|
2020-03-25 04:15:05 +01:00
|
|
|
setMessage(__('Your content will do better with more staked on it'));
|
|
|
|
setError(false);
|
2022-07-24 00:33:00 +02:00
|
|
|
} else if (unlockAmount === 0) {
|
2020-03-25 04:15:05 +01:00
|
|
|
setMessage(__('Amount cannot be zero'));
|
|
|
|
setError(true);
|
2022-07-24 00:33:00 +02:00
|
|
|
} else if (unlockAmount <= previewBalanceNumber / 2) {
|
2020-04-02 19:03:13 +02:00
|
|
|
setMessage(__('A prudent choice'));
|
|
|
|
setError(false);
|
2020-03-25 04:15:05 +01:00
|
|
|
} else {
|
|
|
|
setMessage(initialMessage);
|
|
|
|
setError(false);
|
|
|
|
}
|
2022-07-24 00:33:00 +02:00
|
|
|
}, [unlockTextAmount, previewBalance, setMessage, setError]);
|
2020-03-25 04:15:05 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
|
|
|
icon={ICONS.UNLOCK}
|
2020-08-26 22:28:33 +02:00
|
|
|
title={__('Unlock tips')}
|
2020-03-25 04:15:05 +01:00
|
|
|
subtitle={
|
|
|
|
<>
|
|
|
|
<p>
|
2020-09-02 22:08:37 +02:00
|
|
|
{__('You can unlock all or some of these LBRY Credits at any time.')}{' '}
|
2020-03-25 04:15:05 +01:00
|
|
|
{__('Keeping it locked improves the trust and discoverability of your content.')}
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
learn_more: <Button button="link" label={__('Learn More')} href="https://lbry.com/faq/tipping" />,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
It's usually only worth unlocking what you intend to use immediately. %learn_more%
|
|
|
|
</I18nMessage>
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<React.Fragment>
|
2020-09-02 22:08:37 +02:00
|
|
|
{abandonClaimError ? (
|
|
|
|
<div className="error__wrapper--no-overflow">
|
|
|
|
<ErrorText>{abandonClaimError}</ErrorText>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="section">
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
amount: (
|
|
|
|
<strong>
|
|
|
|
<CreditAmount amount={Number(previewBalance)} precision={8} />
|
|
|
|
</strong>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
%amount% available to unlock
|
|
|
|
</I18nMessage>
|
|
|
|
</div>
|
|
|
|
<div className="section">
|
|
|
|
{previewBalance === 0 && <p>{__('No unlockable tips available')}</p>}
|
|
|
|
{previewBalance === undefined && <p>{__('Loading...')}</p>}
|
|
|
|
{previewBalance && (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<label htmlFor="supports_liquidate_range">{__('Amount to unlock')}</label>
|
|
|
|
<FormField
|
|
|
|
name="supports_liquidate_range"
|
|
|
|
type={'range'}
|
|
|
|
min={0}
|
|
|
|
step={0.01}
|
|
|
|
max={previewBalance}
|
2022-07-24 00:33:00 +02:00
|
|
|
value={amount}
|
|
|
|
onChange={(e) => handleRangeChange(e.target.value)}
|
2020-09-02 22:08:37 +02:00
|
|
|
/>
|
|
|
|
<label className="range__label">
|
|
|
|
<span>0</span>
|
|
|
|
<span>{previewBalance / 2}</span>
|
|
|
|
<span>{previewBalance}</span>
|
|
|
|
</label>
|
|
|
|
<FormField
|
|
|
|
type="text"
|
2022-07-24 00:33:00 +02:00
|
|
|
value={unlockTextAmount}
|
2020-09-02 22:08:37 +02:00
|
|
|
helper={message}
|
2022-07-24 00:33:00 +02:00
|
|
|
onFocus={handleUnlockTextFocus}
|
|
|
|
onChange={(e) => handleChangeUnlockText(e.target.value)}
|
|
|
|
onBlur={handleUnlockTextBlur}
|
2020-09-02 22:08:37 +02:00
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2020-04-13 21:42:15 +02:00
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
disabled={error}
|
|
|
|
button="primary"
|
|
|
|
onClick={abandonClaimError ? handleClose : handleSubmit}
|
|
|
|
label={abandonClaimError ? __('Done') : __('Unlock')}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-03-25 04:15:05 +01:00
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SupportsLiquidate;
|