2018-05-10 02:21:54 +02:00
|
|
|
// @flow
|
2018-10-29 18:23:53 +01:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2018-05-10 04:47:08 +02:00
|
|
|
import * as React from 'react';
|
2019-06-09 08:57:51 +02:00
|
|
|
import { isURIValid, parseURI } from 'lbry-redux';
|
2018-05-10 02:21:54 +02:00
|
|
|
import Button from 'component/button';
|
2019-06-09 08:57:51 +02:00
|
|
|
import ClaimLink from 'component/claimLink';
|
2019-06-05 08:07:53 +02:00
|
|
|
import ChannelLink from 'component/channelLink';
|
2018-05-10 02:21:54 +02:00
|
|
|
|
|
|
|
type Props = {
|
2018-05-11 07:40:24 +02:00
|
|
|
href: string,
|
2018-05-10 02:21:54 +02:00
|
|
|
title?: string,
|
|
|
|
children: React.Node,
|
2018-10-29 18:23:53 +01:00
|
|
|
openModal: (id: string, { uri: string }) => void,
|
2019-06-09 08:57:51 +02:00
|
|
|
'data-preview'?: boolean,
|
2018-05-10 02:21:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ExternalLink extends React.PureComponent<Props> {
|
|
|
|
static defaultProps = {
|
|
|
|
href: null,
|
|
|
|
title: null,
|
|
|
|
};
|
2018-05-11 07:40:24 +02:00
|
|
|
|
|
|
|
createLink() {
|
2019-04-04 23:05:23 +02:00
|
|
|
const { href, title, children, openModal } = this.props;
|
2018-05-11 07:40:24 +02:00
|
|
|
// Regex for url protocol
|
2019-05-30 01:21:20 +02:00
|
|
|
const protocolRegex = new RegExp('^(https?|lbry|mailto)+:', 'i');
|
2018-05-11 07:40:24 +02:00
|
|
|
const protocol = href ? protocolRegex.exec(href) : null;
|
|
|
|
// Return plain text if no valid url
|
|
|
|
let element = <span>{children}</span>;
|
|
|
|
// Return external link if protocol is http or https
|
2019-05-30 01:21:20 +02:00
|
|
|
if (protocol && (protocol[0] === 'http:' || protocol[0] === 'https:' || protocol[0] === 'mailto:')) {
|
2018-05-11 07:40:24 +02:00
|
|
|
element = (
|
|
|
|
<Button
|
|
|
|
button="link"
|
2019-01-22 21:36:28 +01:00
|
|
|
iconRight={ICONS.EXTERNAL}
|
2018-05-11 23:07:26 +02:00
|
|
|
title={title || href}
|
2018-06-21 03:33:00 +02:00
|
|
|
label={children}
|
2019-02-13 17:27:20 +01:00
|
|
|
className="button--external-link"
|
2019-05-12 16:46:14 +02:00
|
|
|
onClick={() => openModal(MODALS.CONFIRM_EXTERNAL_RESOURCE, { uri: href })}
|
2018-06-21 03:33:00 +02:00
|
|
|
/>
|
2018-05-11 07:40:24 +02:00
|
|
|
);
|
|
|
|
}
|
2018-05-11 09:41:22 +02:00
|
|
|
// Return local link if protocol is lbry uri
|
2018-05-11 17:14:08 +02:00
|
|
|
if (protocol && protocol[0] === 'lbry:' && isURIValid(href)) {
|
2019-06-09 08:57:51 +02:00
|
|
|
try {
|
|
|
|
const uri = parseURI(href);
|
|
|
|
if (uri.isChannel && !uri.path) {
|
|
|
|
element = <ChannelLink uri={href}>{children}</ChannelLink>;
|
|
|
|
} else if (uri) {
|
|
|
|
element = (
|
|
|
|
<ClaimLink uri={href} autoEmbed={this.props['data-preview']}>
|
|
|
|
{children}
|
|
|
|
</ClaimLink>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Silent error: console.error(err);
|
|
|
|
}
|
2018-05-11 09:41:22 +02:00
|
|
|
}
|
2018-05-11 08:29:53 +02:00
|
|
|
|
2018-05-11 07:40:24 +02:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const RenderLink = () => this.createLink();
|
|
|
|
return <RenderLink />;
|
2018-05-10 02:21:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ExternalLink;
|