2019-06-09 08:57:51 +02:00
|
|
|
// @flow
|
2019-06-13 06:18:35 +02:00
|
|
|
import uniqid from 'uniqid';
|
2019-06-09 08:57:51 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { parseURI } from 'lbry-redux';
|
|
|
|
import Button from 'component/button';
|
2019-06-13 04:26:40 +02:00
|
|
|
import PreviewLink from 'component/previewLink';
|
2019-06-13 06:18:35 +02:00
|
|
|
import ChannelTooltip from 'component/common/channel-tooltip';
|
2019-06-09 08:57:51 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uri: string,
|
|
|
|
title: ?string,
|
|
|
|
claim: StreamClaim,
|
|
|
|
children: React.Node,
|
|
|
|
thumbnail: ?string,
|
|
|
|
autoEmbed: ?boolean,
|
|
|
|
description: ?string,
|
2019-06-13 06:18:35 +02:00
|
|
|
currentTheme: ?string,
|
2019-06-09 08:57:51 +02:00
|
|
|
isResolvingUri: boolean,
|
|
|
|
resolveUri: string => void,
|
|
|
|
blackListedOutpoints: Array<{
|
|
|
|
txid: string,
|
|
|
|
nout: number,
|
|
|
|
}>,
|
|
|
|
};
|
|
|
|
|
2019-06-13 06:18:35 +02:00
|
|
|
type State = {
|
|
|
|
isTooltipActive: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ClaimLink extends React.Component<Props, State> {
|
|
|
|
parentId: string;
|
|
|
|
|
2019-06-09 08:57:51 +02:00
|
|
|
static defaultProps = {
|
|
|
|
href: null,
|
|
|
|
title: null,
|
2019-06-13 06:18:35 +02:00
|
|
|
thumbnail: null,
|
|
|
|
autoEmbed: false,
|
|
|
|
description: null,
|
|
|
|
isResolvingUri: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { isTooltipActive: false };
|
|
|
|
|
|
|
|
// The tooltip component don't work very well with refs,
|
|
|
|
// so we need to use an unique id for each link:
|
|
|
|
// (this: any).buttonRef = React.createRef();
|
|
|
|
(this: any).parentId = uniqid.time('claim-link-');
|
|
|
|
}
|
|
|
|
|
|
|
|
showTooltip = () => {
|
|
|
|
this.setState({ isTooltipActive: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
hideTooltip = () => {
|
|
|
|
this.setState({ isTooltipActive: false });
|
2019-06-09 08:57:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
isClaimBlackListed() {
|
|
|
|
const { claim, blackListedOutpoints } = this.props;
|
|
|
|
|
|
|
|
if (claim && blackListedOutpoints) {
|
|
|
|
let blackListed = false;
|
|
|
|
|
|
|
|
for (let i = 0; i < blackListedOutpoints.length; i += 1) {
|
|
|
|
const outpoint = blackListedOutpoints[i];
|
|
|
|
if (outpoint.txid === claim.txid && outpoint.nout === claim.nout) {
|
|
|
|
blackListed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return blackListed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { isResolvingUri, resolveUri, uri, claim } = this.props;
|
|
|
|
if (!isResolvingUri && !claim) {
|
|
|
|
resolveUri(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { isResolvingUri, resolveUri, claim, uri } = this.props;
|
|
|
|
if (!isResolvingUri && uri && !claim) {
|
|
|
|
resolveUri(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-06-13 06:18:35 +02:00
|
|
|
const { uri, claim, title, description, thumbnail, currentTheme, autoEmbed, children, isResolvingUri } = this.props;
|
|
|
|
const isUnresolved = (!isResolvingUri && !claim) || !claim;
|
|
|
|
const isBlacklisted = this.isClaimBlackListed();
|
|
|
|
|
|
|
|
if (isBlacklisted || isUnresolved) {
|
|
|
|
return <span>{children}</span>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isChannel, path } = parseURI(uri);
|
|
|
|
const { claim_id: claimId, name: claimName } = claim;
|
|
|
|
const showPreview = autoEmbed === true && !isUnresolved;
|
|
|
|
const renderChannelTooltip = isChannel && !path;
|
2019-06-09 08:57:51 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2019-06-13 06:18:35 +02:00
|
|
|
<Button
|
|
|
|
id={this.parentId}
|
|
|
|
label={children}
|
|
|
|
title={title || claimName}
|
|
|
|
button={'link'}
|
|
|
|
navigate={uri}
|
|
|
|
className="button--uri-indicator"
|
|
|
|
onMouseEnter={this.showTooltip}
|
|
|
|
onMouseLeave={this.hideTooltip}
|
|
|
|
/>
|
2019-06-13 04:26:40 +02:00
|
|
|
{showPreview && <PreviewLink uri={uri} />}
|
2019-06-13 06:18:35 +02:00
|
|
|
{renderChannelTooltip && (
|
|
|
|
<ChannelTooltip
|
|
|
|
uri={uri}
|
|
|
|
title={title}
|
|
|
|
claimId={claimId}
|
|
|
|
channelName={claimName}
|
|
|
|
currentTheme={currentTheme}
|
|
|
|
thumbnail={thumbnail}
|
|
|
|
description={description}
|
|
|
|
active={this.state.isTooltipActive}
|
|
|
|
parent={`#${this.parentId}`}
|
|
|
|
group={'channel-tooltip'}
|
|
|
|
/>
|
|
|
|
)}
|
2019-06-09 08:57:51 +02:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClaimLink;
|