2020-12-04 01:10:23 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as ICONS from 'constants/icons';
|
2021-02-09 17:05:56 +01:00
|
|
|
import { MINIMUM_PUBLISH_BID, INVALID_NAME_ERROR } from 'constants/claim';
|
2020-12-04 01:10:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { useHistory } from 'react-router';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import Button from 'component/button';
|
2021-02-09 17:05:56 +01:00
|
|
|
import ChannelSelector from 'component/channelSelector';
|
2020-12-04 01:10:23 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2021-10-08 05:47:39 +02:00
|
|
|
import { parseURI, isNameValid, isURIValid, normalizeURI } from 'util/lbryURI';
|
|
|
|
import { creditsToString } from 'util/format-credits';
|
2020-12-04 01:10:23 +01:00
|
|
|
import analytics from 'analytics';
|
|
|
|
import LbcSymbol from 'component/common/lbc-symbol';
|
|
|
|
import ClaimPreview from 'component/claimPreview';
|
|
|
|
import { URL as SITE_URL, URL_LOCAL, URL_DEV } from 'config';
|
|
|
|
import HelpLink from 'component/common/help-link';
|
2021-02-09 17:05:56 +01:00
|
|
|
import WalletSpendableBalanceHelp from 'component/walletSpendableBalanceHelp';
|
2021-08-06 23:01:04 +02:00
|
|
|
import BidHelpText from 'component/publishBid/bid-help-text';
|
2021-02-09 17:05:56 +01:00
|
|
|
import Spinner from 'component/spinner';
|
2020-12-04 01:10:23 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
doToast: ({ message: string }) => void,
|
|
|
|
doClearRepostError: () => void,
|
2021-07-05 23:45:20 +02:00
|
|
|
doRepost: (StreamRepostOptions) => Promise<*>,
|
2021-12-12 07:03:00 +01:00
|
|
|
doHideModal: () => void,
|
2020-12-16 16:28:57 +01:00
|
|
|
title: string,
|
2020-12-04 01:10:23 +01:00
|
|
|
claim?: StreamClaim,
|
|
|
|
enteredContentClaim?: StreamClaim,
|
|
|
|
balance: number,
|
|
|
|
channels: ?Array<ChannelClaim>,
|
2021-07-05 23:45:20 +02:00
|
|
|
doCheckPublishNameAvailability: (string) => Promise<*>,
|
2020-12-04 01:10:23 +01:00
|
|
|
error: ?string,
|
|
|
|
reposting: boolean,
|
|
|
|
uri: string,
|
|
|
|
name: string,
|
|
|
|
doCheckPendingClaims: () => void,
|
|
|
|
passedRepostAmount: number,
|
|
|
|
enteredRepostAmount: number,
|
|
|
|
isResolvingPassedRepost: boolean,
|
|
|
|
isResolvingEnteredRepost: boolean,
|
2021-02-09 17:05:56 +01:00
|
|
|
activeChannelClaim: ?ChannelClaim,
|
|
|
|
fetchingMyChannels: boolean,
|
2021-03-15 04:39:26 +01:00
|
|
|
incognito: boolean,
|
2021-12-12 07:03:00 +01:00
|
|
|
contentUri: string,
|
|
|
|
setContentUri: (string) => void,
|
|
|
|
repostUri: string,
|
|
|
|
setRepostUri: (string) => void,
|
|
|
|
isModal: boolean,
|
|
|
|
redirectUri?: string,
|
2020-12-04 01:10:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function RepostCreate(props: Props) {
|
|
|
|
const {
|
|
|
|
doToast,
|
|
|
|
doClearRepostError,
|
|
|
|
doRepost,
|
|
|
|
claim,
|
|
|
|
enteredContentClaim,
|
|
|
|
balance,
|
|
|
|
reposting,
|
|
|
|
doCheckPublishNameAvailability,
|
|
|
|
uri, // ?from
|
|
|
|
name, // ?to
|
|
|
|
doCheckPendingClaims,
|
|
|
|
enteredRepostAmount,
|
|
|
|
passedRepostAmount,
|
|
|
|
isResolvingPassedRepost,
|
|
|
|
isResolvingEnteredRepost,
|
2021-02-09 17:05:56 +01:00
|
|
|
activeChannelClaim,
|
|
|
|
fetchingMyChannels,
|
2021-03-15 04:39:26 +01:00
|
|
|
incognito,
|
2021-12-12 07:03:00 +01:00
|
|
|
doHideModal,
|
|
|
|
contentUri,
|
|
|
|
setContentUri,
|
|
|
|
setRepostUri,
|
|
|
|
isModal,
|
|
|
|
redirectUri,
|
2020-12-04 01:10:23 +01:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const defaultName = name || (claim && claim.name) || '';
|
|
|
|
const contentClaimId = claim && claim.claim_id;
|
|
|
|
const enteredClaimId = enteredContentClaim && enteredContentClaim.claim_id;
|
|
|
|
|
|
|
|
const [repostBid, setRepostBid] = React.useState(0.01);
|
|
|
|
const [repostBidError, setRepostBidError] = React.useState(undefined);
|
|
|
|
const [enteredRepostName, setEnteredRepostName] = React.useState(defaultName);
|
|
|
|
const [available, setAvailable] = React.useState(true);
|
|
|
|
const [enteredContent, setEnteredContentUri] = React.useState(undefined);
|
|
|
|
const [contentError, setContentError] = React.useState('');
|
|
|
|
const { replace, goBack } = useHistory();
|
2021-12-12 07:03:00 +01:00
|
|
|
|
2020-12-04 01:10:23 +01:00
|
|
|
const resolvingRepost = isResolvingEnteredRepost || isResolvingPassedRepost;
|
2021-04-09 12:45:52 +02:00
|
|
|
const repostUrlName = `lbry://${incognito || !activeChannelClaim ? '' : `${activeChannelClaim.name}/`}`;
|
2020-12-04 01:10:23 +01:00
|
|
|
|
|
|
|
const contentFirstRender = React.useRef(true);
|
2021-07-05 23:45:20 +02:00
|
|
|
const setAutoRepostBid = (amount) => {
|
2020-12-22 16:16:39 +01:00
|
|
|
if (balance && balance > 0.02) {
|
|
|
|
if (uri) {
|
|
|
|
setRepostBid(0.01);
|
|
|
|
} else if (balance > amount) {
|
2020-12-04 01:10:23 +01:00
|
|
|
setRepostBid(Number(amount.toFixed(2)));
|
|
|
|
} else {
|
2020-12-22 16:16:39 +01:00
|
|
|
setRepostBid(0.01);
|
2020-12-04 01:10:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function getSearchUri(value) {
|
|
|
|
const WEB_DEV_PREFIX = `${URL_DEV}/`;
|
|
|
|
const WEB_LOCAL_PREFIX = `${URL_LOCAL}/`;
|
|
|
|
const WEB_PROD_PREFIX = `${SITE_URL}/`;
|
|
|
|
const ODYSEE_PREFIX = `https://odysee.com/`;
|
|
|
|
const includesLbryTvProd = value.includes(WEB_PROD_PREFIX);
|
|
|
|
const includesOdysee = value.includes(ODYSEE_PREFIX);
|
|
|
|
const includesLbryTvLocal = value.includes(WEB_LOCAL_PREFIX);
|
|
|
|
const includesLbryTvDev = value.includes(WEB_DEV_PREFIX);
|
|
|
|
const wasCopiedFromWeb = includesLbryTvDev || includesLbryTvLocal || includesLbryTvProd || includesOdysee;
|
|
|
|
const isLbryUrl = value.startsWith('lbry://') && value !== 'lbry://';
|
|
|
|
const error = '';
|
|
|
|
|
2021-07-05 23:45:20 +02:00
|
|
|
const addLbryIfNot = (term) => {
|
2020-12-04 01:10:23 +01:00
|
|
|
return term.startsWith('lbry://') ? term : `lbry://${term}`;
|
|
|
|
};
|
|
|
|
if (wasCopiedFromWeb) {
|
|
|
|
let prefix = WEB_PROD_PREFIX;
|
|
|
|
if (includesLbryTvLocal) prefix = WEB_LOCAL_PREFIX;
|
|
|
|
if (includesLbryTvDev) prefix = WEB_DEV_PREFIX;
|
|
|
|
if (includesOdysee) prefix = ODYSEE_PREFIX;
|
|
|
|
|
|
|
|
let query = (value && value.slice(prefix.length).replace(/:/g, '#')) || '';
|
|
|
|
try {
|
|
|
|
const lbryUrl = `lbry://${query}`;
|
|
|
|
parseURI(lbryUrl);
|
|
|
|
return [lbryUrl, null];
|
|
|
|
} catch (e) {
|
|
|
|
return [query, 'error'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLbryUrl) {
|
|
|
|
if (value.startsWith('@')) {
|
|
|
|
if (isNameValid(value.slice(1))) {
|
|
|
|
return [value, null];
|
|
|
|
} else {
|
|
|
|
return [value, error];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [addLbryIfNot(value), null];
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const isValid = isURIValid(value);
|
|
|
|
if (isValid) {
|
|
|
|
let uri;
|
|
|
|
try {
|
|
|
|
uri = normalizeURI(value);
|
|
|
|
} catch (e) {
|
|
|
|
return [value, null];
|
|
|
|
}
|
|
|
|
return [uri, null];
|
|
|
|
} else {
|
|
|
|
return [value, null];
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return [value, 'error'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// repostName
|
|
|
|
let repostNameError;
|
|
|
|
if (!enteredRepostName) {
|
|
|
|
repostNameError = __('A name is required');
|
2021-10-13 15:30:50 +02:00
|
|
|
} else if (!isNameValid(enteredRepostName)) {
|
2020-12-04 01:10:23 +01:00
|
|
|
repostNameError = INVALID_NAME_ERROR;
|
|
|
|
} else if (!available) {
|
|
|
|
repostNameError = __('You already have a claim with this name.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// contentName
|
|
|
|
let contentNameError;
|
|
|
|
if (!enteredContent && enteredContent !== undefined) {
|
|
|
|
contentNameError = __('A name is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-10-13 15:30:50 +02:00
|
|
|
if (enteredRepostName && isNameValid(enteredRepostName)) {
|
2021-07-05 23:45:20 +02:00
|
|
|
doCheckPublishNameAvailability(enteredRepostName).then((r) => setAvailable(r));
|
2020-12-04 01:10:23 +01:00
|
|
|
}
|
|
|
|
}, [enteredRepostName, doCheckPublishNameAvailability]);
|
|
|
|
|
|
|
|
// takeover amount, bid suggestion
|
|
|
|
React.useEffect(() => {
|
|
|
|
const repostTakeoverAmount = Number(enteredRepostAmount)
|
|
|
|
? Number(enteredRepostAmount) + 0.01
|
|
|
|
: Number(passedRepostAmount) + 0.01;
|
|
|
|
|
|
|
|
if (repostTakeoverAmount) {
|
|
|
|
setAutoRepostBid(repostTakeoverAmount);
|
|
|
|
}
|
2021-08-06 23:01:04 +02:00
|
|
|
}, [enteredRepostAmount, passedRepostAmount]);
|
2020-12-04 01:10:23 +01:00
|
|
|
|
|
|
|
// repost bid error
|
|
|
|
React.useEffect(() => {
|
|
|
|
let rBidError;
|
|
|
|
if (repostBid === 0) {
|
|
|
|
rBidError = __('Deposit cannot be 0');
|
|
|
|
} else if (balance === repostBid) {
|
|
|
|
rBidError = __('Please decrease your deposit to account for transaction fees');
|
|
|
|
} else if (balance < repostBid) {
|
2021-01-12 16:25:51 +01:00
|
|
|
rBidError = __('Deposit cannot be higher than your available balance');
|
2020-12-04 01:10:23 +01:00
|
|
|
} else if (repostBid < MINIMUM_PUBLISH_BID) {
|
|
|
|
rBidError = __('Your deposit must be higher');
|
|
|
|
}
|
|
|
|
setRepostBidError(rBidError);
|
|
|
|
}, [setRepostBidError, repostBidError, repostBid]);
|
|
|
|
|
|
|
|
// setContentUri given enteredUri
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!enteredContent && !contentFirstRender.current) {
|
|
|
|
setContentError(__('A name is required'));
|
|
|
|
}
|
|
|
|
if (enteredContent) {
|
|
|
|
contentFirstRender.current = false;
|
|
|
|
const [searchContent, error] = getSearchUri(enteredContent);
|
|
|
|
if (error) {
|
|
|
|
setContentError(__('Something not quite right..'));
|
|
|
|
} else {
|
|
|
|
setContentError('');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const { streamName, channelName, isChannel } = parseURI(searchContent);
|
|
|
|
if (!isChannel && streamName && isNameValid(streamName)) {
|
|
|
|
// contentNameValid = true;
|
|
|
|
setContentUri(searchContent);
|
|
|
|
} else if (isChannel && channelName && isNameValid(channelName)) {
|
|
|
|
// contentNameValid = true;
|
|
|
|
setContentUri(searchContent);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (enteredContent !== '@') setContentError('');
|
|
|
|
setContentUri(``);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [enteredContent, setContentUri, setContentError, parseURI, isNameValid]);
|
|
|
|
|
|
|
|
// setRepostName
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (enteredRepostName) {
|
|
|
|
let isValid = false;
|
|
|
|
try {
|
|
|
|
parseURI(enteredRepostName);
|
|
|
|
isValid = true;
|
|
|
|
} catch (e) {
|
|
|
|
isValid = false;
|
|
|
|
}
|
|
|
|
if (isValid) {
|
|
|
|
setRepostUri(enteredRepostName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [enteredRepostName, setRepostUri, parseURI]);
|
|
|
|
|
|
|
|
const repostClaimId = contentClaimId || enteredClaimId;
|
|
|
|
|
|
|
|
const getRedirect = (entered, passed, redirect) => {
|
|
|
|
if (redirect) {
|
|
|
|
return redirect;
|
|
|
|
} else if (entered) {
|
|
|
|
try {
|
|
|
|
const { claimName } = parseURI(entered);
|
2021-10-13 15:30:50 +02:00
|
|
|
return claimName ? `/${claimName}` : '/';
|
2020-12-04 01:10:23 +01:00
|
|
|
} catch (e) {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
} else if (passed) {
|
|
|
|
try {
|
|
|
|
const { claimName } = parseURI(passed);
|
2021-10-13 15:30:50 +02:00
|
|
|
return claimName ? `/${claimName}` : '/';
|
2020-12-04 01:10:23 +01:00
|
|
|
} catch (e) {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
if (enteredRepostName && repostBid && repostClaimId) {
|
|
|
|
doRepost({
|
|
|
|
name: enteredRepostName,
|
|
|
|
bid: creditsToString(repostBid),
|
2021-03-15 04:39:26 +01:00
|
|
|
channel_id: activeChannelClaim && !incognito ? activeChannelClaim.claim_id : undefined,
|
2020-12-04 01:10:23 +01:00
|
|
|
claim_id: repostClaimId,
|
|
|
|
}).then((repostClaim: StreamClaim) => {
|
|
|
|
doCheckPendingClaims();
|
|
|
|
analytics.apiLogPublish(repostClaim);
|
2021-06-17 19:57:04 +02:00
|
|
|
doToast({
|
|
|
|
message: __('Woohoo! Successfully reposted this claim.'),
|
|
|
|
linkText: __('Uploads'),
|
|
|
|
linkTarget: '/uploads',
|
|
|
|
});
|
2021-12-12 07:03:00 +01:00
|
|
|
if (isModal) {
|
|
|
|
doHideModal();
|
|
|
|
} else {
|
|
|
|
replace(getRedirect(contentUri, uri, redirectUri));
|
|
|
|
}
|
2020-12-04 01:10:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelIt() {
|
|
|
|
doClearRepostError();
|
2021-12-12 07:03:00 +01:00
|
|
|
if (isModal) {
|
|
|
|
doHideModal();
|
|
|
|
} else {
|
|
|
|
goBack();
|
|
|
|
}
|
2020-12-04 01:10:23 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
if (fetchingMyChannels) {
|
|
|
|
return (
|
|
|
|
<div className="main--empty">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:10:23 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Card
|
|
|
|
actions={
|
|
|
|
<div>
|
2021-12-13 00:14:07 +01:00
|
|
|
<ChannelSelector />
|
2020-12-04 01:10:23 +01:00
|
|
|
{uri && (
|
|
|
|
<fieldset-section>
|
2021-07-05 23:45:20 +02:00
|
|
|
<ClaimPreview key={uri} uri={uri} actions={''} showNullPlaceholder />
|
2020-12-04 01:10:23 +01:00
|
|
|
</fieldset-section>
|
|
|
|
)}
|
|
|
|
{!uri && name && (
|
|
|
|
<>
|
|
|
|
<FormField
|
2020-12-21 14:02:21 +01:00
|
|
|
label={__('Content to repost')}
|
2020-12-04 01:10:23 +01:00
|
|
|
type="text"
|
|
|
|
name="content_url"
|
|
|
|
value={enteredContent}
|
|
|
|
error={contentError}
|
2021-07-05 23:45:20 +02:00
|
|
|
onChange={(event) => setEnteredContentUri(event.target.value)}
|
2020-12-04 01:10:23 +01:00
|
|
|
placeholder={__('Enter a name or %domain% URL', { domain: SITE_URL })}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2020-12-04 01:10:23 +01:00
|
|
|
{!uri && (
|
|
|
|
<fieldset-section>
|
|
|
|
<ClaimPreview key={contentUri} uri={contentUri} actions={''} type={'large'} showNullPlaceholder />
|
|
|
|
</fieldset-section>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<React.Fragment>
|
|
|
|
<fieldset-section>
|
|
|
|
<fieldset-group class="fieldset-group--smushed fieldset-group--disabled-prefix">
|
|
|
|
<fieldset-section>
|
|
|
|
<label htmlFor="auth_first_channel">
|
|
|
|
{repostNameError ? (
|
|
|
|
<span className="error__text">{repostNameError}</span>
|
|
|
|
) : (
|
|
|
|
<span>
|
|
|
|
{__('Repost URL')}
|
|
|
|
<HelpLink href="https://lbry.com/faq/naming" />
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</label>
|
|
|
|
<div className="form-field__prefix">{repostUrlName}</div>
|
|
|
|
</fieldset-section>
|
|
|
|
<FormField
|
|
|
|
type="text"
|
|
|
|
name="repost_name"
|
|
|
|
value={enteredRepostName}
|
2021-07-05 23:45:20 +02:00
|
|
|
onChange={(event) => setEnteredRepostName(event.target.value)}
|
2020-12-21 16:36:55 +01:00
|
|
|
placeholder={__('MyFunName')}
|
2020-12-04 01:10:23 +01:00
|
|
|
/>
|
|
|
|
</fieldset-group>
|
|
|
|
</fieldset-section>
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
type="number"
|
|
|
|
name="repost_bid"
|
|
|
|
min="0"
|
|
|
|
step="any"
|
|
|
|
placeholder="0.123"
|
|
|
|
className="form-field--price-amount"
|
2020-12-21 14:02:21 +01:00
|
|
|
label={<LbcSymbol postfix={__('Support --[button to support a claim]--')} size={14} />}
|
2020-12-04 01:10:23 +01:00
|
|
|
value={repostBid}
|
|
|
|
error={repostBidError}
|
2021-02-09 17:05:56 +01:00
|
|
|
helper={
|
|
|
|
<>
|
2021-08-06 23:01:04 +02:00
|
|
|
<BidHelpText
|
|
|
|
uri={'lbry://' + enteredRepostName}
|
|
|
|
amountNeededForTakeover={enteredRepostAmount}
|
|
|
|
isResolvingUri={isResolvingEnteredRepost}
|
|
|
|
/>
|
2021-02-09 17:05:56 +01:00
|
|
|
<WalletSpendableBalanceHelp inline />
|
|
|
|
</>
|
|
|
|
}
|
2020-12-04 01:10:23 +01:00
|
|
|
disabled={!enteredRepostName || resolvingRepost}
|
2021-07-05 23:45:20 +02:00
|
|
|
onChange={(event) => setRepostBid(event.target.value)}
|
|
|
|
onWheel={(e) => e.stopPropagation()}
|
2020-12-04 01:10:23 +01:00
|
|
|
/>
|
|
|
|
</React.Fragment>
|
|
|
|
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
icon={ICONS.REPOST}
|
|
|
|
disabled={
|
|
|
|
resolvingRepost ||
|
|
|
|
reposting ||
|
|
|
|
repostBidError ||
|
|
|
|
repostNameError ||
|
|
|
|
((!uri || enteredContent) && contentNameError) ||
|
|
|
|
(!uri && !enteredContentClaim)
|
|
|
|
}
|
|
|
|
button="primary"
|
|
|
|
label={reposting ? __('Reposting') : __('Repost')}
|
|
|
|
onClick={handleSubmit}
|
|
|
|
/>
|
|
|
|
<Button button="link" label={__('Cancel')} onClick={cancelIt} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RepostCreate;
|