2018-05-09 18:21:54 -06:00
|
|
|
// @flow
|
2018-10-29 13:23:53 -04:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2018-05-09 20:47:08 -06:00
|
|
|
import * as React from 'react';
|
2019-06-12 22:18:35 -06:00
|
|
|
import { isURIValid } from 'lbry-redux';
|
2018-05-09 18:21:54 -06:00
|
|
|
import Button from 'component/button';
|
2019-06-09 00:57:51 -06:00
|
|
|
import ClaimLink from 'component/claimLink';
|
2018-05-09 18:21:54 -06:00
|
|
|
|
|
|
|
type Props = {
|
2018-05-10 23:40:24 -06:00
|
|
|
href: string,
|
2018-05-09 18:21:54 -06:00
|
|
|
title?: string,
|
2019-06-13 01:18:30 -06:00
|
|
|
embed?: boolean,
|
2018-05-09 18:21:54 -06:00
|
|
|
children: React.Node,
|
2018-10-29 13:23:53 -04:00
|
|
|
openModal: (id: string, { uri: string }) => void,
|
2018-05-09 18:21:54 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
class ExternalLink extends React.PureComponent<Props> {
|
|
|
|
static defaultProps = {
|
|
|
|
href: null,
|
|
|
|
title: null,
|
2019-06-13 01:18:30 -06:00
|
|
|
embed: false,
|
2018-05-09 18:21:54 -06:00
|
|
|
};
|
2018-05-10 23:40:24 -06:00
|
|
|
|
|
|
|
createLink() {
|
2019-06-13 01:18:30 -06:00
|
|
|
const { href, title, embed, children, openModal } = this.props;
|
2018-05-10 23:40:24 -06:00
|
|
|
// Regex for url protocol
|
2019-05-29 17:21:20 -06:00
|
|
|
const protocolRegex = new RegExp('^(https?|lbry|mailto)+:', 'i');
|
2018-05-10 23:40:24 -06: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-29 17:21:20 -06:00
|
|
|
if (protocol && (protocol[0] === 'http:' || protocol[0] === 'https:' || protocol[0] === 'mailto:')) {
|
2018-05-10 23:40:24 -06:00
|
|
|
element = (
|
|
|
|
<Button
|
|
|
|
button="link"
|
2019-01-22 15:36:28 -05:00
|
|
|
iconRight={ICONS.EXTERNAL}
|
2018-05-11 15:07:26 -06:00
|
|
|
title={title || href}
|
2018-06-20 19:33:00 -06:00
|
|
|
label={children}
|
2019-02-13 12:27:20 -04:00
|
|
|
className="button--external-link"
|
2019-07-04 12:23:22 +01:00
|
|
|
onClick={() => {
|
2019-12-03 12:32:38 -05:00
|
|
|
openModal(MODALS.CONFIRM_EXTERNAL_RESOURCE, { uri: href, isTrusted: false });
|
2019-07-04 12:23:22 +01:00
|
|
|
}}
|
2018-06-20 19:33:00 -06:00
|
|
|
/>
|
2018-05-10 23:40:24 -06:00
|
|
|
);
|
|
|
|
}
|
2018-05-11 01:41:22 -06:00
|
|
|
// Return local link if protocol is lbry uri
|
2018-05-11 09:14:08 -06:00
|
|
|
if (protocol && protocol[0] === 'lbry:' && isURIValid(href)) {
|
2019-06-12 22:18:35 -06:00
|
|
|
element = (
|
2019-06-24 10:56:37 -06:00
|
|
|
<ClaimLink uri={href} autoEmbed={embed}>
|
2019-06-12 22:18:35 -06:00
|
|
|
{children}
|
|
|
|
</ClaimLink>
|
|
|
|
);
|
2018-05-11 01:41:22 -06:00
|
|
|
}
|
2018-05-11 00:29:53 -06:00
|
|
|
|
2018-05-10 23:40:24 -06:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const RenderLink = () => this.createLink();
|
|
|
|
return <RenderLink />;
|
2018-05-09 18:21:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ExternalLink;
|