lbry-desktop/src/renderer/component/externalLink/view.jsx

64 lines
1.6 KiB
React
Raw Normal View History

// @flow
2018-05-10 04:47:08 +02:00
import * as React from 'react';
2018-05-11 17:14:08 +02:00
import { MODALS, isURIValid } from 'lbry-redux';
import Button from 'component/button';
type Props = {
2018-05-11 07:40:24 +02:00
href: string,
title?: string,
children: React.Node,
navigate: (string, ?{}) => void,
2018-05-11 07:40:24 +02:00
openModal: ({ id: string }, { uri: string }) => void,
};
class ExternalLink extends React.PureComponent<Props> {
static defaultProps = {
href: null,
title: null,
};
2018-05-11 07:40:24 +02:00
createLink() {
const { href, title, children, openModal, navigate } = this.props;
console.info(href);
2018-05-11 07:40:24 +02:00
// Regex for url protocol
const protocolRegex = new RegExp('^(https?|lbry)+:', 'i');
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
if (protocol && (protocol[0] === 'http:' || protocol[0] === 'https:')) {
element = (
<Button
button="link"
title={title}
className="btn--external-link"
onClick={() => openModal({ id: MODALS.CONFIRM_EXTERNAL_LINK }, { uri: href })}
>
{children}
</Button>
);
}
// Return local link if protocol is lbry uri
2018-05-11 17:14:08 +02:00
if (protocol && protocol[0] === 'lbry:' && isURIValid(href)) {
2018-05-11 08:29:53 +02:00
element = (
<Button button="link" title={title} onClick={() => navigate('/show', { uri: href })}>
2018-05-11 08:29:53 +02:00
{children}
</Button>
);
}
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 />;
}
}
export default ExternalLink;