2017-04-21 18:05:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2024-01-14 12:23:13 +00:00
|
|
|
import { useCallback } from 'react';
|
2016-12-02 14:05:50 +00:00
|
|
|
|
2024-01-14 12:23:13 +00:00
|
|
|
import { useAppHistory } from './router';
|
2016-12-02 14:05:50 +00:00
|
|
|
|
2024-01-14 12:23:13 +00:00
|
|
|
const Permalink = ({ className, href, to, children, onInterceptClick, ...props }) => {
|
|
|
|
const history = useAppHistory();
|
2017-05-12 12:44:10 +00:00
|
|
|
|
2024-01-14 12:23:13 +00:00
|
|
|
const handleClick = useCallback((e) => {
|
2018-10-02 14:01:28 +00:00
|
|
|
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
|
2024-01-14 12:23:13 +00:00
|
|
|
if (onInterceptClick && onInterceptClick()) {
|
2018-10-02 14:01:28 +00:00
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-14 12:23:13 +00:00
|
|
|
if (history) {
|
2018-10-02 14:01:28 +00:00
|
|
|
e.preventDefault();
|
2024-01-14 12:23:13 +00:00
|
|
|
history.push(to);
|
2018-10-02 14:01:28 +00:00
|
|
|
}
|
2016-12-02 14:05:50 +00:00
|
|
|
}
|
2024-01-14 12:23:13 +00:00
|
|
|
}, [onInterceptClick, history, to]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a target='_blank' href={href} onClick={handleClick} className={`permalink${className ? ' ' + className : ''}`} {...props}>
|
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Permalink.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
href: PropTypes.string.isRequired,
|
|
|
|
to: PropTypes.string.isRequired,
|
|
|
|
children: PropTypes.node,
|
|
|
|
onInterceptClick: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Permalink;
|