File size: 1,772 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Gridicon } from '@automattic/components';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { getPostTotalCommentsCount } from 'calypso/state/comments/selectors';

import './style.scss';

function CommentButton( {
	commentCount = 0,
	size = 24,
	tagName: TagName = 'li',
	onClick,
	href,
	target,
	icon,
	defaultLabel,
} ) {
	const translate = useTranslate();
	const showLabel = commentCount > 0 || defaultLabel;
	const label = commentCount || defaultLabel;
	// Show a tooltip only when we are showing the number of existing comments.
	const showTooltip = commentCount > 0;

	return (
		<TagName
			className={ clsx( 'comment-button', {
				tooltip: showTooltip,
			} ) }
			data-tooltip={ showTooltip ? translate( 'Comment' ) : undefined }
			onClick={ onClick }
			href={ 'a' === TagName ? href : undefined }
			target={ 'a' === TagName ? target : undefined }
		>
			{ icon || <Gridicon icon="comment" size={ size } className="comment-button__icon" /> }
			<span className="comment-button__label">
				{ showLabel && <span className="comment-button__label-count">{ label }</span> }
			</span>
		</TagName>
	);
}

CommentButton.propTypes = {
	commentCount: PropTypes.number,
	href: PropTypes.string,
	onClick: PropTypes.func,
	post: PropTypes.object,
	tagName: PropTypes.string,
	target: PropTypes.string,
	icon: PropTypes.object,
	defaultLabel: PropTypes.string,
};

const mapStateToProps = ( state, ownProps ) => {
	const { post: { site_ID: siteId, ID: postId } = {}, commentCount } = ownProps;

	return {
		commentCount: getPostTotalCommentsCount( state, siteId, postId ) || commentCount,
	};
};

export default connect( mapStateToProps )( CommentButton );