insomnia/app/ui/components/base/Link.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-07-07 20:10:55 +00:00
import React, {Component, PropTypes} from 'react';
import {shell} from 'electron';
import {trackEvent} from '../../../analytics/index';
import * as querystring from '../../../common/querystring';
import {getAppVersion} from '../../../common/constants';
2016-07-07 20:10:55 +00:00
class Link extends Component {
constructor (props) {
super(props);
this._boundHandleClick = this._handleClick.bind(this);
}
2016-11-23 22:55:09 +00:00
_handleClick (e) {
e && e.preventDefault();
const {href} = this.props;
if (href.match(/^http/i)) {
const qs = `utm_source=Insomnia&utm_medium=App&utm_campaign=v${getAppVersion()}`;
const attributedHref = querystring.joinUrl(href, qs);
shell.openExternal(attributedHref);
} else {
// Don't modify non-http urls
shell.openExternal(href);
}
trackEvent('Link', 'Click', href)
}
2016-11-23 22:55:09 +00:00
2016-07-07 20:10:55 +00:00
render () {
2016-11-23 20:10:53 +00:00
const {onClick, button, href, children, ...other} = this.props;
2016-08-25 17:06:01 +00:00
return button ? (
2016-11-23 22:55:09 +00:00
<button onClick={e => {
onClick();
this._boundHandleClick(e);
}} {...other}>
2016-08-25 17:06:01 +00:00
{children}
</button>
2016-11-23 22:55:09 +00:00
) : (
<a href={href} onClick={e => {
onClick();
this._boundHandleClick(e);
}} {...other}>
2016-07-07 20:10:55 +00:00
{children}
</a>
)
}
}
Link.propTypes = {
2016-08-25 17:06:01 +00:00
href: PropTypes.string.isRequired,
// Optional
button: PropTypes.bool
2016-07-07 20:10:55 +00:00
};
export default Link;