File size: 1,846 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
65
66
67
68
69
70
71
72
73
74
75
76
import { Popover } from '@automattic/components';
import clsx from 'clsx';
import { omit } from 'lodash';
import { connect } from 'react-redux';
import { countPostLikes } from 'calypso/state/posts/selectors/count-post-likes';
import { getPostLikes } from 'calypso/state/posts/selectors/get-post-likes';
import PostLikes from './index';

import './popover.scss';

function PostLikesPopover( props ) {
	const {
		className,
		siteId,
		postId,
		showDisplayNames,
		context,
		onClose,
		likes,
		likeCount,
		onMouseEnter,
		onMouseLeave,
	} = props;

	// Whenever our `Popover` content changes size (loading, complete, siteId,
	// or postId, for example), we need to force the `Popover` to re-render and
	// re-compute its position.  The `Popover` component is not really designed
	// for this, so the easiest way is to force it to unmount and remount.
	// This is achieved by setting a different `key` whenever our data changes.
	const popoverKey = [
		'likes',
		siteId,
		postId,
		showDisplayNames ? 'large' : 'small',
		likes ? likes.length : 0,
		likeCount,
	].join( '-' );

	const popoverProps = omit(
		props,
		'className',
		'siteId',
		'postId',
		'showDisplayNames',
		'context',
		'onClose',
		'likes',
		'onMouseEnter',
		'onMouseLeave'
	);
	const classes = clsx( 'post-likes-popover', className );
	const postLikesProps = { siteId, postId, showDisplayNames, onMouseEnter, onMouseLeave };

	return (
		<Popover
			{ ...popoverProps }
			className={ classes }
			isVisible
			context={ context }
			onClose={ onClose }
			key={ popoverKey }
		>
			<PostLikes { ...postLikesProps } />
		</Popover>
	);
}

export default connect( ( state, ownProps ) => {
	const { siteId, postId } = ownProps;

	return {
		likeCount: countPostLikes( state, siteId, postId ),
		likes: getPostLikes( state, siteId, postId ),
	};
} )( PostLikesPopover );