lbry-desktop/ui/component/claimLink/view.jsx

83 lines
2 KiB
React
Raw Normal View History

2019-06-09 08:57:51 +02:00
// @flow
import * as React from 'react';
import PreviewLink from 'component/previewLink';
2019-10-15 01:05:19 +02:00
import UriIndicator from 'component/uriIndicator';
2019-06-09 08:57:51 +02:00
type Props = {
uri: string,
claim: StreamClaim,
children: React.Node,
autoEmbed: ?boolean,
description: ?string,
isResolvingUri: boolean,
resolveUri: string => void,
blackListedOutpoints: Array<{
txid: string,
nout: number,
}>,
};
2019-06-20 22:59:01 +02:00
class ClaimLink extends React.Component<Props> {
2019-06-09 08:57:51 +02:00
static defaultProps = {
href: null,
2019-06-21 03:00:52 +02:00
link: false,
thumbnail: null,
autoEmbed: false,
description: null,
isResolvingUri: false,
};
componentDidMount() {
this.resolve(this.props);
}
componentDidUpdate() {
this.resolve(this.props);
}
2019-06-09 08:57:51 +02:00
isClaimBlackListed() {
const { claim, blackListedOutpoints } = this.props;
const signingChannel = claim && claim.signing_channel;
2019-06-09 08:57:51 +02:00
if (claim && blackListedOutpoints) {
let blackListed = false;
blackListed = blackListedOutpoints.some(
outpoint =>
(signingChannel && outpoint.txid === signingChannel.txid && outpoint.nout === signingChannel.nout) ||
(outpoint.txid === claim.txid && outpoint.nout === claim.nout)
);
2019-06-09 08:57:51 +02:00
return blackListed;
}
}
resolve = (props: Props) => {
const { isResolvingUri, resolveUri, claim, uri } = props;
2019-06-09 08:57:51 +02:00
if (!isResolvingUri && claim === undefined && uri) {
2019-06-09 08:57:51 +02:00
resolveUri(uri);
}
};
2019-06-09 08:57:51 +02:00
render() {
const { uri, claim, autoEmbed, children, isResolvingUri } = this.props;
const isUnresolved = (!isResolvingUri && !claim) || !claim;
const isBlacklisted = this.isClaimBlackListed();
if (isBlacklisted || isUnresolved) {
return <span>{children}</span>;
}
const { value_type: valueType } = claim;
2019-10-15 01:05:19 +02:00
const isChannel = valueType === 'channel';
const showPreview = autoEmbed === true && !isUnresolved;
2019-06-20 22:59:01 +02:00
if (isChannel) {
return <UriIndicator uri={uri} link addTooltip />;
}
2019-10-15 01:05:19 +02:00
return <React.Fragment>{showPreview && <PreviewLink uri={uri} />}</React.Fragment>;
2019-06-09 08:57:51 +02:00
}
}
export default ClaimLink;