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

54 lines
1.4 KiB
JavaScript
Raw Normal View History

import React, {PureComponent, PropTypes} from 'react';
import autobind from 'autobind-decorator';
import {shell} from 'electron';
import {trackEvent} from '../../../analytics/index';
import {getAppVersion, isDevelopment} from '../../../common/constants';
import * as querystring from '../../../common/querystring';
2016-07-07 20:10:55 +00:00
@autobind
class Link extends PureComponent {
_handleClick (e) {
e && e.preventDefault();
const {href, onClick} = this.props;
// Also call onClick that was passed to us if there was one
onClick && onClick(e);
if (href.match(/^http/i)) {
2016-11-23 23:12:32 +00:00
const appName = isDevelopment() ? 'Insomnia Dev' : 'Insomnia';
2016-12-05 22:42:40 +00:00
const qs = `utm_source=${appName}&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 () {
const {
onClick, // eslint-disable-line no-unused-vars
button,
href,
children,
...other
} = this.props;
return button
? <button onClick={this._handleClick} {...other}>{children}</button>
: <a href={href} onClick={this._handleClick} {...other}>{children}</a>;
2016-07-07 20:10:55 +00:00
}
}
Link.propTypes = {
2016-08-25 17:06:01 +00:00
href: PropTypes.string.isRequired,
// Optional
button: PropTypes.bool,
onClick: PropTypes.func,
children: PropTypes.node
2016-07-07 20:10:55 +00:00
};
export default Link;