File size: 815 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import page from '@automattic/calypso-router';
import PropTypes from 'prop-types';
import { PureComponent } from 'react';

export default class CommentLink extends PureComponent {
	static propTypes = {
		commentId: PropTypes.number,
		href: PropTypes.string,
	};

	handleClick = ( event ) => {
		event.preventDefault();
		window.scrollTo( 0, 0 );

		const { commentId, href } = this.props;
		const { path } = window.history.state;

		const newPath = path.includes( '#' )
			? path.replace( /[#].*/, `#comment-${ commentId }` )
			: `${ path }#comment-${ commentId }`;

		window.history.replaceState( { ...window.history.state, path: newPath }, null );

		page( href );
	};

	render() {
		const { href, commentId, ...props } = this.props;
		return <a href={ href } { ...props } onClick={ this.handleClick } />;
	}
}