File size: 10,050 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
import { map, size, filter, get, partition } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import PostCommentFormRoot from 'calypso/blocks/comments/form-root';
import PostComment from 'calypso/blocks/comments/post-comment';
import ConversationCaterpillar from 'calypso/blocks/conversation-caterpillar';
import { recordAction, recordGaEvent } from 'calypso/reader/stats';
import {
requestPostComments,
requestComment,
setActiveReply,
} from 'calypso/state/comments/actions';
import { POST_COMMENT_DISPLAY_TYPES } from 'calypso/state/comments/constants';
import {
commentsFetchingStatus,
getActiveReplyCommentId,
getCommentErrors,
getDateSortedPostComments,
getExpansionsForPost,
getHiddenCommentsForPost,
getPostCommentsTree,
} from 'calypso/state/comments/selectors';
import { getErrorKey } from 'calypso/state/comments/utils';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';
import { recordReaderTracksEvent } from 'calypso/state/reader/analytics/actions';
import './list.scss';
/**
* ConversationsCommentList is the component that represents all of the comments for a conversations-stream
* Some of it is boilerplate stolen from PostCommentList (all the activeXCommentId bits) but the special
* convos parts are related to:
* 1. caterpillars
* 2. commentsToShow
*
* As of the time of this writing, commentsToShow is constructing by merging two objects:
* 1. expansion state in the reducer for the specific post
* 2. commentIds handed from the api as seeds to start with as open. high watermark will replace this logic.
*
* So when a post is loaded, the api gives us 3 comments. This component creates an object that looks like:
* { [commentId1]: 'is-excerpt', [commentId2]: 'is-excerpt', [commentId3]: 'is-excerpt' } and then
* hands that down to all of the PostComments so they will know how to render.
*
* This component will also display a caterpillar if it has any children comments that are hidden.
* It can determine hidden state by seeing that the number of commentsToShow < totalCommentsForPost.
*/
const FETCH_NEW_COMMENTS_THRESHOLD = 20;
const noop = () => {};
export class ConversationCommentList extends Component {
static propTypes = {
post: PropTypes.object.isRequired, // required by PostComment
commentIds: PropTypes.array.isRequired,
shouldRequestComments: PropTypes.bool,
setActiveReply: PropTypes.func,
};
static defaultProps = {
enableCaterpillar: true,
shouldRequestComments: true,
setActiveReply: noop,
filterParents: true,
};
state = {
commentText: '',
};
onUpdateCommentText = ( commentText ) => this.setState( { commentText } );
onReplyClick = ( commentId ) => {
this.setActiveReplyComment( commentId );
recordAction( 'comment_reply_click' );
recordGaEvent( 'Clicked Reply to Comment' );
this.props.recordReaderTracksEvent(
'calypso_reader_comment_reply_click',
{
comment_id: commentId,
},
{ post: this.props.post }
);
};
onReplyCancel = () => {
this.setState( { commentText: '' } );
recordAction( 'comment_reply_cancel_click' );
recordGaEvent( 'Clicked Cancel Reply to Comment' );
this.props.recordReaderTracksEvent(
'calypso_reader_comment_reply_cancel_click',
{
comment_id: this.props.activeReplyCommentId,
},
{ post: this.props.post }
);
this.resetActiveReplyComment();
};
reqMoreComments = ( props = this.props ) => {
const { siteId, postId, enableCaterpillar, shouldRequestComments } = props;
if ( ! shouldRequestComments || ! props.commentsFetchingStatus ) {
return;
}
const { haveEarlierCommentsToFetch, haveLaterCommentsToFetch } = props.commentsFetchingStatus;
if ( enableCaterpillar && ( haveEarlierCommentsToFetch || haveLaterCommentsToFetch ) ) {
const direction = haveEarlierCommentsToFetch ? 'before' : 'after';
props.requestPostComments( { siteId, postId, direction } );
}
};
componentDidMount() {
this.resetActiveReplyComment();
this.reqMoreComments();
}
componentDidUpdate() {
const { hiddenComments, commentsTree, siteId, commentErrors } = this.props;
// if we are running low on comments to expand then fetch more
if ( size( hiddenComments ) < FETCH_NEW_COMMENTS_THRESHOLD ) {
this.reqMoreComments();
}
// if we are missing any comments in the hierarchy towards a comment that should be shown,
// then load them one at a time. This is not the most efficient method, ideally we could
// load a subtree
const inaccessible = this.getInaccessibleParentsIds(
commentsTree,
Object.keys( this.getCommentsToShow() )
);
inaccessible
.filter( ( commentId ) => ! commentErrors[ getErrorKey( siteId, commentId ) ] )
.forEach( ( commentId ) => {
this.props.requestComment( {
commentId,
siteId,
} );
} );
}
getParentId = ( commentsTree, childId ) =>
get( commentsTree, [ childId, 'data', 'parent', 'ID' ] );
commentHasParent = ( commentsTree, childId ) => !! this.getParentId( commentsTree, childId );
commentIsLoaded = ( commentsTree, commentId ) => !! get( commentsTree, commentId );
getInaccessibleParentsIds = ( commentsTree, commentIds ) => {
// base case
if ( size( commentIds ) === 0 ) {
return [];
}
const withParents = filter( commentIds, ( id ) => this.commentHasParent( commentsTree, id ) );
const parentIds = map( withParents, ( id ) => this.getParentId( commentsTree, id ) );
const [ accessible, inaccessible ] = partition( parentIds, ( id ) =>
this.commentIsLoaded( commentsTree, id )
);
return inaccessible.concat( this.getInaccessibleParentsIds( commentsTree, accessible ) );
};
// @todo: move all expanded comment set per commentId logic to memoized selectors
getCommentsToShow = () => {
const { commentIds, expansions, commentsTree, sortedComments, filterParents } = this.props;
const minId = Math.min( ...commentIds );
const startingCommentIds = ( sortedComments || [] )
.filter( ( comment ) => comment.ID >= minId || comment.isPlaceholder )
.map( ( comment ) => comment.ID );
let parentIds = startingCommentIds;
if ( filterParents ) {
parentIds = parentIds.map( ( id ) => this.getParentId( commentsTree, id ) ).filter( Boolean );
}
const startingExpanded = Object.fromEntries(
[ startingCommentIds, ...parentIds ].map( ( id ) => [
id,
POST_COMMENT_DISPLAY_TYPES.excerpt,
] )
);
return { ...startingExpanded, ...expansions };
};
setActiveReplyComment = ( commentId ) => {
const siteId = get( this.props, 'post.site_ID' );
const postId = get( this.props, 'post.ID' );
if ( ! siteId || ! postId ) {
return;
}
this.props.setActiveReply( {
siteId,
postId,
commentId,
} );
};
resetActiveReplyComment = () => {
this.setActiveReplyComment( null );
};
render() {
const { commentsTree, post, enableCaterpillar } = this.props;
if ( ! post ) {
return null;
}
const commentsToShow = this.getCommentsToShow();
const isDoneLoadingComments =
! this.props.commentsFetchingStatus.haveEarlierCommentsToFetch &&
! this.props.commentsFetchingStatus.haveLaterCommentsToFetch;
// if you have finished loading comments, then lets use the comments we have as the final comment count
// if we are still loading comments, then assume what the server initially told us is right
const commentCount = isDoneLoadingComments
? filter( commentsTree, ( comment ) => get( comment, 'data.type' ) === 'comment' ).length // filter out pingbacks/trackbacks
: post.discussion.comment_count;
const showCaterpillar = enableCaterpillar && size( commentsToShow ) < commentCount;
return (
<div className="conversations__comment-list">
<ul className="conversations__comment-list-ul">
{ showCaterpillar && (
<ConversationCaterpillar
blogId={ post.site_ID }
postId={ post.ID }
commentCount={ commentCount }
commentsToShow={ commentsToShow }
/>
) }
{ map( commentsTree.children, ( commentId ) => {
return (
<PostComment
showNestingReplyArrow
hidePingbacksAndTrackbacks
enableCaterpillar={ enableCaterpillar }
post={ post }
commentsTree={ commentsTree }
key={ commentId }
commentId={ commentId }
maxDepth={ 2 }
commentsToShow={ commentsToShow }
onReplyClick={ this.onReplyClick }
onReplyCancel={ this.onReplyCancel }
activeReplyCommentId={ this.props.activeReplyCommentId }
onUpdateCommentText={ this.onUpdateCommentText }
onCommentSubmit={ this.resetActiveReplyComment }
commentText={ this.state.commentText }
showReadMoreInActions
displayType={ POST_COMMENT_DISPLAY_TYPES.excerpt }
/>
);
} ) }
<PostCommentFormRoot
post={ this.props.post }
commentsTree={ this.props.commentsTree }
commentText={ this.state.commentText }
onUpdateCommentText={ this.onUpdateCommentText }
activeReplyCommentId={ this.props.activeReplyCommentId }
/>
</ul>
</div>
);
}
}
const ConnectedConversationCommentList = connect(
( state, ownProps ) => {
const { site_ID: siteId, ID: postId, discussion } = ownProps.post;
const authorId = getCurrentUserId( state );
return {
siteId,
postId,
sortedComments: getDateSortedPostComments( state, siteId, postId ),
commentsTree: getPostCommentsTree( state, siteId, postId, 'all', authorId ),
commentsFetchingStatus:
commentsFetchingStatus( state, siteId, postId, discussion.comment_count ) || {},
expansions: getExpansionsForPost( state, siteId, postId ),
hiddenComments: getHiddenCommentsForPost( state, siteId, postId ),
activeReplyCommentId: getActiveReplyCommentId( {
state,
siteId,
postId,
} ),
commentErrors: getCommentErrors( state ),
};
},
{ recordReaderTracksEvent, requestPostComments, requestComment, setActiveReply }
)( ConversationCommentList );
export default ConnectedConversationCommentList;
|