File size: 1,044 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
import { getStateKey } from 'calypso/state/comments/utils';

import 'calypso/state/comments/init';

/**
 * Get total number of comments in state at a given date and time
 * @param {Object} state redux state
 * @param {number} siteId site identification
 * @param {number} postId site identification
 * @param {Date} date Date to count comments for
 * @returns {number} total comments count in state
 */
export function getPostCommentsCountAtDate( state, siteId, postId, date ) {
	// Check the provided date
	if ( ! ( date instanceof Date && ! isNaN( date ) ) ) {
		return 0;
	}

	const stateKey = getStateKey( siteId, postId );
	const postComments = state.comments.items?.[ stateKey ];

	if ( ! Array.isArray( postComments ) || ! postComments.length ) {
		return 0;
	}

	// Count post comments with the specified date
	const dateTimestamp = date.getTime() / 1000;
	const postCommentsAtDate = postComments.filter( ( postComment ) => {
		return Date.parse( postComment.date ) / 1000 === dateTimestamp;
	} );

	return postCommentsAtDate.length;
}