lbry-desktop/src/ui/component/claimPreview/view.jsx

210 lines
6.8 KiB
React
Raw Normal View History

2019-06-11 20:10:58 +02:00
// @flow
2019-07-21 23:31:22 +02:00
import React, { Fragment, useEffect } from 'react';
2019-06-11 20:10:58 +02:00
import classnames from 'classnames';
2019-06-27 08:18:45 +02:00
import { parseURI, convertToShareLink } from 'lbry-redux';
2019-06-11 20:10:58 +02:00
import { withRouter } from 'react-router-dom';
import { openCopyLinkMenu } from 'util/context-menu';
import { formatLbryUriForWeb } from 'util/uri';
import CardMedia from 'component/cardMedia';
import UriIndicator from 'component/uriIndicator';
import TruncatedText from 'component/common/truncated-text';
import DateTime from 'component/dateTime';
import FileProperties from 'component/fileProperties';
2019-06-19 07:05:43 +02:00
import ClaimTags from 'component/claimTags';
2019-06-11 20:10:58 +02:00
import SubscribeButton from 'component/subscribeButton';
import ChannelThumbnail from 'component/channelThumbnail';
import BlockButton from 'component/blockButton';
2019-07-21 23:31:22 +02:00
import Button from 'component/button';
2019-06-11 20:10:58 +02:00
type Props = {
uri: string,
claim: ?Claim,
obscureNsfw: boolean,
showUserBlocked: boolean,
2019-06-11 20:10:58 +02:00
claimIsMine: boolean,
pending?: boolean,
resolveUri: string => void,
isResolvingUri: boolean,
preventResolve: boolean,
history: { push: string => void },
thumbnail: string,
title: string,
nsfw: boolean,
2019-07-21 23:31:22 +02:00
placeholder: string,
2019-06-19 07:05:43 +02:00
type: string,
2019-07-11 20:06:25 +02:00
hasVisitedUri: boolean,
blackListedOutpoints: Array<{
txid: string,
nout: number,
}>,
filteredOutpoints: Array<{
txid: string,
nout: number,
}>,
blockedChannelUris: Array<string>,
channelIsBlocked: boolean,
isSubscribed: boolean,
2019-06-11 20:10:58 +02:00
};
function ClaimPreview(props: Props) {
2019-06-11 20:10:58 +02:00
const {
obscureNsfw,
claimIsMine,
pending,
history,
uri,
isResolvingUri,
thumbnail,
title,
nsfw,
resolveUri,
claim,
placeholder,
2019-06-19 07:05:43 +02:00
type,
blackListedOutpoints,
filteredOutpoints,
blockedChannelUris,
2019-07-11 20:06:25 +02:00
hasVisitedUri,
showUserBlocked,
channelIsBlocked,
isSubscribed,
2019-06-11 20:10:58 +02:00
} = props;
const haventFetched = claim === undefined;
2019-07-21 23:31:22 +02:00
const abandoned = !isResolvingUri && !claim;
2019-06-11 20:10:58 +02:00
const claimsInChannel = (claim && claim.meta.claims_in_channel) || 0;
2019-07-21 23:31:22 +02:00
const showPublishLink = abandoned && placeholder === 'publish';
2019-07-23 10:05:51 +02:00
const includeChannelTooltip = type !== 'inline' && type !== 'tooltip';
const hideActions = type === 'small' || type === 'tooltip';
2019-07-21 23:31:22 +02:00
2019-07-11 21:03:31 +02:00
let isValid;
try {
parseURI(uri);
isValid = true;
} catch (e) {
isValid = false;
}
2019-07-05 20:11:55 +02:00
2019-07-11 21:03:31 +02:00
const isChannel = isValid ? parseURI(uri).isChannel : false;
2019-07-29 16:12:53 +02:00
const signingChannel = claim && claim.signing_channel;
2019-07-21 23:31:22 +02:00
let shouldHide =
2019-07-24 02:12:34 +02:00
placeholder !== 'loading' && ((abandoned && !showPublishLink) || (!claimIsMine && obscureNsfw && nsfw));
2019-07-05 20:11:55 +02:00
// This will be replaced once blocking is done at the wallet server level
if (claim && !shouldHide && blackListedOutpoints) {
shouldHide = blackListedOutpoints.some(outpoint => outpoint.txid === claim.txid && outpoint.nout === claim.nout);
}
// We're checking to see if the stream outpoint
// or signing channel outpoint is in the filter list
if (claim && !shouldHide && filteredOutpoints) {
shouldHide = filteredOutpoints.some(
outpoint =>
(signingChannel && outpoint.txid === signingChannel.txid && outpoint.nout === signingChannel.nout) ||
(outpoint.txid === claim.txid && outpoint.nout === claim.nout)
);
2019-07-05 20:11:55 +02:00
}
// if showUserBlocked wasnt passed to claimPreview (for blocked page) hide user-blocked channels
if (claim && !shouldHide && !showUserBlocked && blockedChannelUris.length && signingChannel) {
shouldHide = blockedChannelUris.some(blockedUri => blockedUri === signingChannel.permanent_url);
}
2019-06-11 20:10:58 +02:00
function handleContextMenu(e) {
e.preventDefault();
e.stopPropagation();
if (claim) {
openCopyLinkMenu(convertToShareLink(claim.permanent_url), e);
}
}
function onClick(e) {
if ((isChannel || title) && !pending) {
history.push(formatLbryUriForWeb(uri));
}
}
useEffect(() => {
2019-07-11 21:03:31 +02:00
if (isValid && !isResolvingUri && haventFetched && uri) {
2019-06-11 20:10:58 +02:00
resolveUri(uri);
}
2019-07-17 22:49:06 +02:00
}, [isValid, isResolvingUri, uri, resolveUri, haventFetched]);
2019-06-11 20:10:58 +02:00
if (shouldHide) {
return null;
}
2019-07-21 23:31:22 +02:00
if (placeholder === 'loading' || (isResolvingUri && !claim)) {
2019-06-11 20:10:58 +02:00
return (
2019-07-21 23:31:22 +02:00
<li className={classnames('claim-preview', { 'claim-preview--large': type === 'large' })} disabled>
2019-06-11 20:10:58 +02:00
<div className="placeholder media__thumb" />
<div className="placeholder__wrapper">
2019-06-27 08:18:45 +02:00
<div className="placeholder claim-preview-title" />
2019-06-11 20:10:58 +02:00
<div className="placeholder media__subtitle" />
</div>
</li>
);
}
return (
<li
role="link"
2019-07-21 23:31:22 +02:00
onClick={pending || type === 'inline' ? undefined : onClick}
2019-06-11 20:10:58 +02:00
onContextMenu={handleContextMenu}
2019-06-27 08:18:45 +02:00
className={classnames('claim-preview', {
2019-07-23 10:05:51 +02:00
'claim-preview--small': type === 'small' || type === 'tooltip',
2019-06-27 08:18:45 +02:00
'claim-preview--large': type === 'large',
2019-07-21 23:31:22 +02:00
'claim-preview--inline': type === 'inline',
2019-07-23 10:05:51 +02:00
'claim-preview--tooltip': type === 'tooltip',
'claim-preview--visited': !isChannel && !claimIsMine && hasVisitedUri,
2019-07-11 20:06:25 +02:00
'claim-preview--pending': pending,
2019-06-11 20:10:58 +02:00
})}
>
{isChannel ? <ChannelThumbnail uri={uri} /> : <CardMedia thumbnail={thumbnail} />}
2019-06-27 08:18:45 +02:00
<div className="claim-preview-metadata">
<div className="claim-preview-info">
<div className="claim-preview-title">
2019-07-21 23:31:22 +02:00
{claim ? <TruncatedText text={title || claim.name} lines={1} /> : <span>{__('Nothing here')}</span>}
2019-06-11 20:10:58 +02:00
</div>
2019-07-23 10:05:51 +02:00
{!hideActions && (
2019-06-11 20:10:58 +02:00
<div>
{isChannel && !channelIsBlocked && (
<SubscribeButton uri={uri.startsWith('lbry://') ? uri : `lbry://${uri}`} />
)}
{isChannel && !isSubscribed && <BlockButton uri={uri.startsWith('lbry://') ? uri : `lbry://${uri}`} />}
2019-06-17 22:32:38 +02:00
{!isChannel && <FileProperties uri={uri} />}
2019-06-11 20:10:58 +02:00
</div>
)}
</div>
2019-06-27 08:18:45 +02:00
<div className="claim-preview-properties">
2019-06-11 20:10:58 +02:00
<div className="media__subtitle">
{pending && <div>Pending...</div>}
2019-07-21 23:31:22 +02:00
{!isResolvingUri && (
<div>
{claim ? (
2019-07-23 10:05:51 +02:00
<UriIndicator uri={uri} link addTooltip={includeChannelTooltip} />
2019-07-21 23:31:22 +02:00
) : (
<Fragment>
<div>{__('Publish something and claim this spot!')}</div>
<div className="card__actions">
<Button button="primary" label={`${__('Publish to')} ${uri}`} />
</div>
</Fragment>
)}
<div>
{isChannel ? (
type !== 'inline' && `${claimsInChannel} ${__('publishes')}`
) : (
<DateTime timeAgo uri={uri} />
)}
</div>
</div>
)}
2019-06-11 20:10:58 +02:00
</div>
2019-06-19 07:05:43 +02:00
<ClaimTags uri={uri} type={type} />
2019-06-11 20:10:58 +02:00
</div>
</div>
</li>
);
}
export default withRouter(ClaimPreview);