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

92 lines
2.3 KiB
React
Raw Normal View History

2019-06-09 08:57:51 +02:00
// @flow
import * as React from 'react';
import Button from 'component/button';
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,
title: ?string,
claim: StreamClaim,
children: React.Node,
2019-06-21 03:00:52 +02:00
className: ?string,
2019-06-09 08:57:51 +02:00
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,
2019-06-09 08:57:51 +02:00
title: null,
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() {
2019-06-24 18:56:37 +02:00
const { uri, claim, title, className, autoEmbed, children, isResolvingUri } = this.props;
const isUnresolved = (!isResolvingUri && !claim) || !claim;
const isBlacklisted = this.isClaimBlackListed();
if (isBlacklisted || isUnresolved) {
return <span>{children}</span>;
}
2019-10-15 01:05:19 +02:00
const { name: claimName, value_type: valueType } = claim;
const isChannel = valueType === 'channel';
const showPreview = autoEmbed === true && !isUnresolved;
2019-06-20 22:59:01 +02:00
2019-10-15 01:05:19 +02:00
if(isChannel){
return <UriIndicator uri={uri} link addTooltip />
}
2019-06-09 08:57:51 +02:00
return (
<React.Fragment>
2019-06-24 18:56:37 +02:00
<Button label={children} title={title || claimName} button={'link'} navigate={uri} className={className} />
{showPreview && <PreviewLink uri={uri} />}
2019-06-09 08:57:51 +02:00
</React.Fragment>
);
}
}
export default ClaimLink;