| import config from '@automattic/calypso-config'; |
| import { getUrlParts } from '@automattic/calypso-url'; |
| import { Gridicon, TimeSince } from '@automattic/components'; |
| import { isURL, getProtocol, getAuthority } from '@wordpress/url'; |
| import clsx from 'clsx'; |
| import { translate } from 'i18n-calypso'; |
| import { get, some, flatMap } from 'lodash'; |
| import PropTypes from 'prop-types'; |
| import { PureComponent } from 'react'; |
| import { connect } from 'react-redux'; |
| import ConversationCaterpillar from 'calypso/blocks/conversation-caterpillar'; |
| import GravatarWithHovercards from 'calypso/components/gravatar-with-hovercards'; |
| import { decodeEntities } from 'calypso/lib/formatting'; |
| import { navigate } from 'calypso/lib/navigate'; |
| import { createAccountUrl } from 'calypso/lib/paths'; |
| import isReaderTagEmbedPage from 'calypso/lib/reader/is-reader-tag-embed-page'; |
| import withDimensions from 'calypso/lib/with-dimensions'; |
| import { getStreamUrl } from 'calypso/reader/route'; |
| import { recordAction, recordGaEvent, recordPermalinkClick } from 'calypso/reader/stats'; |
| import { getUserProfileUrl } from 'calypso/reader/user-profile/user-profile.utils'; |
| import { expandComments } from 'calypso/state/comments/actions'; |
| import { PLACEHOLDER_STATE, POST_COMMENT_DISPLAY_TYPES } from 'calypso/state/comments/constants'; |
| import { getCurrentUser, isUserLoggedIn } from 'calypso/state/current-user/selectors'; |
| import { recordReaderTracksEvent } from 'calypso/state/reader/analytics/actions'; |
| import { registerLastActionRequiresLogin } from 'calypso/state/reader-ui/actions'; |
| import CommentActions from './comment-actions'; |
| import PostCommentForm from './form'; |
| import PostCommentContent from './post-comment-content'; |
| import PostCommentWithError from './post-comment-with-error'; |
| import PostTrackback from './post-trackback'; |
|
|
| import './post-comment.scss'; |
|
|
| const noop = () => {}; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| class PostComment extends PureComponent { |
| static propTypes = { |
| commentsTree: PropTypes.object.isRequired, |
| commentId: PropTypes.oneOfType( [ |
| PropTypes.string, |
| PropTypes.number, |
| ] ).isRequired, |
| onReplyClick: PropTypes.func, |
| depth: PropTypes.number, |
| post: PropTypes.object, |
| maxChildrenToShow: PropTypes.number, |
| onCommentSubmit: PropTypes.func, |
| maxDepth: PropTypes.number, |
| showNestingReplyArrow: PropTypes.bool, |
| showReadMoreInActions: PropTypes.bool, |
| hidePingbacksAndTrackbacks: PropTypes.bool, |
| isInlineComment: PropTypes.bool, |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| commentsToShow: PropTypes.object, |
|
|
| enableCaterpillar: PropTypes.bool, |
|
|
| |
| currentUser: PropTypes.object, |
| shouldHighlightNew: PropTypes.bool, |
| }; |
|
|
| static defaultProps = { |
| onReplyClick: noop, |
| errors: [], |
| depth: 1, |
| maxDepth: Infinity, |
| maxChildrenToShow: 5, |
| onCommentSubmit: noop, |
| showNestingReplyArrow: false, |
| showReadMoreInActions: false, |
| hidePingbacksAndTrackbacks: false, |
| shouldHighlightNew: false, |
| }; |
|
|
| state = { |
| showReplies: false, |
| showFull: false, |
| }; |
|
|
| handleToggleRepliesClick = () => { |
| this.setState( { showReplies: ! this.state.showReplies } ); |
| }; |
|
|
| onLikeToggle = () => { |
| if ( ! this.props.isLoggedIn ) { |
| |
| const { pathname } = getUrlParts( window.location.href ); |
| if ( isReaderTagEmbedPage( window.location ) ) { |
| return window.open( |
| createAccountUrl( { redirectTo: pathname, ref: 'reader-lp' } ), |
| '_blank' |
| ); |
| } |
| |
| if ( ! config.isEnabled( 'reader/login-window' ) ) { |
| return navigate( createAccountUrl( { redirectTo: pathname, ref: 'reader-lp' } ) ); |
| } |
| } |
| }; |
|
|
| handleReply = () => { |
| if ( ! this.props.isLoggedIn ) { |
| return this.props.registerLastActionRequiresLogin( { |
| type: 'reply', |
| siteId: this.props.post.site_ID, |
| postId: this.props.post.ID, |
| commentId: this.props.commentId, |
| } ); |
| } |
| this.props.onReplyClick( this.props.commentId ); |
| this.setState( { showReplies: true } ); |
| }; |
|
|
| handleAuthorClick = ( event ) => { |
| recordAction( 'comment_author_click' ); |
| recordGaEvent( 'Clicked Author Name' ); |
| this.props.recordReaderTracksEvent( |
| 'calypso_reader_comment_author_click', |
| { |
| comment_id: this.props.commentId, |
| author_url: event.target.href, |
| }, |
| { post: this.props.post } |
| ); |
| }; |
|
|
| handleCommentPermalinkClick = ( event ) => { |
| recordPermalinkClick( |
| 'timestamp_comment', |
| {}, |
| { |
| blog_id: this.props.post.site_ID, |
| post_id: this.props.post.ID, |
| comment_id: this.props.commentId, |
| author_url: event.target.href, |
| } |
| ); |
| }; |
|
|
| getAllChildrenIds = ( id ) => { |
| const { commentsTree } = this.props; |
|
|
| if ( ! id ) { |
| return []; |
| } |
|
|
| const immediateChildren = get( commentsTree, [ id, 'children' ], [] ); |
| return immediateChildren.concat( |
| flatMap( immediateChildren, ( childId ) => this.getAllChildrenIds( childId ) ) |
| ); |
| }; |
|
|
| |
| shouldRenderCaterpillar = () => { |
| const { enableCaterpillar, commentsToShow, commentId } = this.props; |
| const childIds = this.getAllChildrenIds( commentId ); |
|
|
| return ( |
| enableCaterpillar && commentsToShow && some( childIds, ( id ) => ! commentsToShow[ id ] ) |
| ); |
| }; |
|
|
| |
| shouldRenderReplies = () => { |
| const { commentsToShow, commentId } = this.props; |
| const childIds = this.getAllChildrenIds( commentId ); |
|
|
| return commentsToShow && some( childIds, ( id ) => commentsToShow[ id ] ); |
| }; |
|
|
| renderRepliesList() { |
| const { |
| commentsToShow, |
| depth, |
| commentId, |
| commentsTree, |
| maxChildrenToShow, |
| enableCaterpillar, |
| post, |
| maxDepth, |
| } = this.props; |
|
|
| const commentChildrenIds = get( commentsTree, [ commentId, 'children' ] ); |
| |
| const exceedsMaxChildrenToShow = |
| commentChildrenIds && commentChildrenIds.length < maxChildrenToShow; |
| const showReplies = this.state.showReplies || exceedsMaxChildrenToShow || enableCaterpillar; |
| const childDepth = ! commentsToShow || commentsToShow[ commentId ] ? depth + 1 : depth; |
|
|
| |
| if ( ! commentChildrenIds || commentChildrenIds.length < 1 ) { |
| return null; |
| } |
|
|
| const showRepliesText = translate( |
| 'show %(numOfReplies)d reply', |
| 'show %(numOfReplies)d replies', |
| { |
| count: commentChildrenIds.length, |
| args: { numOfReplies: commentChildrenIds.length }, |
| } |
| ); |
|
|
| const hideRepliesText = translate( |
| 'hide %(numOfReplies)d reply', |
| 'hide %(numOfReplies)d replies', |
| { |
| count: commentChildrenIds.length, |
| args: { numOfReplies: commentChildrenIds.length }, |
| } |
| ); |
|
|
| let replyVisibilityText = null; |
| if ( ! exceedsMaxChildrenToShow && ! enableCaterpillar ) { |
| replyVisibilityText = this.state.showReplies ? hideRepliesText : showRepliesText; |
| } |
|
|
| return ( |
| <div> |
| { !! replyVisibilityText && ( |
| <button className="comments__view-replies-btn" onClick={ this.handleToggleRepliesClick }> |
| <Gridicon icon="reply" size={ 18 } /> { replyVisibilityText } |
| </button> |
| ) } |
| { showReplies && ( |
| <ol className="comments__list"> |
| { commentChildrenIds.map( ( childId ) => ( |
| // eslint-disable-next-line no-use-before-define |
| <ConnectedPostComment |
| showNestingReplyArrow={ this.props.showNestingReplyArrow } |
| showReadMoreInActions={ this.props.showReadMoreInActions } |
| enableCaterpillar={ enableCaterpillar } |
| depth={ childDepth } |
| maxDepth={ maxDepth } |
| key={ childId } |
| commentId={ childId } |
| commentsTree={ commentsTree } |
| commentsToShow={ commentsToShow } |
| post={ post } |
| onReplyClick={ this.props.onReplyClick } |
| onReplyCancel={ this.props.onReplyCancel } |
| activeReplyCommentId={ this.props.activeReplyCommentId } |
| commentText={ this.props.commentText } |
| onUpdateCommentText={ this.props.onUpdateCommentText } |
| onCommentSubmit={ this.props.onCommentSubmit } |
| shouldHighlightNew={ this.props.shouldHighlightNew } |
| isInlineComment={ this.props.isInlineComment } |
| /> |
| ) ) } |
| </ol> |
| ) } |
| </div> |
| ); |
| } |
|
|
| renderCommentForm() { |
| if ( this.props.activeReplyCommentId !== this.props.commentId ) { |
| return null; |
| } |
|
|
| |
| const placeholderState = get( this.props.commentsTree, [ |
| this.props.commentId, |
| 'data', |
| 'placeholderState', |
| ] ); |
| if ( placeholderState === PLACEHOLDER_STATE.PENDING ) { |
| return null; |
| } |
|
|
| return ( |
| <PostCommentForm |
| post={ this.props.post } |
| parentCommentId={ this.props.commentId } |
| commentText={ this.props.commentText } |
| onUpdateCommentText={ this.props.onUpdateCommentText } |
| onCommentSubmit={ this.props.onCommentSubmit } |
| isInlineComment={ this.props.isInlineComment } |
| /> |
| ); |
| } |
|
|
| getAuthorDetails = ( commentId ) => { |
| const comment = get( this.props.commentsTree, [ commentId, 'data' ], {} ); |
| const commentAuthor = get( comment, 'author', {} ); |
| const commentAuthorName = decodeEntities( commentAuthor.name ); |
|
|
| let commentAuthorUrl; |
| if ( commentAuthor.wpcom_login ) { |
| commentAuthorUrl = getUserProfileUrl( commentAuthor.wpcom_login ); |
| } else if ( commentAuthor.site_ID ) { |
| commentAuthorUrl = getStreamUrl( null, commentAuthor.site_ID ); |
| } else { |
| const urlToCheck = commentAuthor?.URL; |
| if ( urlToCheck && isURL( urlToCheck ) ) { |
| const protocol = getProtocol( urlToCheck ); |
| const domain = getAuthority( urlToCheck ); |
| |
| if ( protocol === 'https:' && ! domain.includes( '%' ) ) { |
| commentAuthorUrl = urlToCheck; |
| } |
| } |
| } |
|
|
| return { comment, commentAuthor, commentAuthorUrl, commentAuthorName }; |
| }; |
|
|
| renderAuthorTag = ( { authorName, authorUrl, commentId, className } ) => { |
| return authorUrl ? ( |
| <a |
| href={ authorUrl } |
| className={ className } |
| onClick={ this.handleAuthorClick } |
| id={ `comment-${ commentId }` } |
| > |
| { authorName } |
| </a> |
| ) : ( |
| <strong className={ className } id={ `comment-${ commentId }` }> |
| { authorName } |
| </strong> |
| ); |
| }; |
|
|
| onReadMore = () => { |
| this.setState( { showFull: true } ); |
| this.props.post && |
| this.props.expandComments( { |
| siteId: this.props.post.site_ID, |
| commentIds: [ this.props.commentId ], |
| postId: this.props.post.ID, |
| displayType: POST_COMMENT_DISPLAY_TYPES.full, |
| } ); |
| recordAction( 'comment_read_more_click' ); |
| recordGaEvent( 'Clicked Comment Read More' ); |
| this.props.recordReaderTracksEvent( |
| 'calypso_reader_comment_read_more_click', |
| { |
| comment_id: this.props.commentId, |
| }, |
| { |
| post: this.props.post, |
| } |
| ); |
| }; |
|
|
| render() { |
| const { |
| commentsTree, |
| commentId, |
| depth, |
| enableCaterpillar, |
| maxDepth, |
| post, |
| commentsToShow, |
| overflowY, |
| showReadMoreInActions, |
| hidePingbacksAndTrackbacks, |
| shouldHighlightNew, |
| } = this.props; |
|
|
| const comment = get( commentsTree, [ commentId, 'data' ] ); |
| const isPingbackOrTrackback = comment.type === 'trackback' || comment.type === 'pingback'; |
|
|
| if ( ! comment || ( hidePingbacksAndTrackbacks && isPingbackOrTrackback ) ) { |
| return null; |
| } else if ( commentsToShow && ! commentsToShow[ commentId ] ) { |
| |
| return this.shouldRenderReplies() && <div>{ this.renderRepliesList() }</div>; |
| } |
|
|
| const displayType = |
| this.state.showFull || ! enableCaterpillar |
| ? POST_COMMENT_DISPLAY_TYPES.full |
| : commentsToShow[ commentId ]; |
|
|
| |
| const haveReplyWithError = some( |
| get( commentsTree, [ this.props.commentId, 'children' ] ), |
| ( childId ) => |
| get( commentsTree, [ childId, 'data', 'placeholderState' ] ) === PLACEHOLDER_STATE.ERROR |
| ); |
|
|
| |
| if ( comment.isPlaceholder ) { |
| comment.author = this.props.currentUser; |
| comment.author.name = this.props.currentUser?.display_name; |
| } else { |
| comment.author.name = decodeEntities( comment.author.name ); |
| } |
|
|
| |
| if ( comment.isPlaceholder && comment.placeholderState === PLACEHOLDER_STATE.ERROR ) { |
| return <PostCommentWithError { ...this.props } repliesList={ this.renderRepliesList() } />; |
| } |
|
|
| |
| if ( isPingbackOrTrackback ) { |
| return <PostTrackback { ...this.props } />; |
| } |
|
|
| |
| const parentCommentId = get( comment, 'parent.ID' ); |
| const { commentAuthorUrl, commentAuthorName } = this.getAuthorDetails( commentId ); |
| const { commentAuthorUrl: parentAuthorUrl, commentAuthorName: parentAuthorName } = |
| this.getAuthorDetails( parentCommentId ); |
|
|
| |
| const isHighlighted = |
| shouldHighlightNew && new Date().getTime() - new Date( comment.date ).getTime() < 10000; |
|
|
| const postCommentClassnames = clsx( 'comments__comment', { |
| [ 'depth-' + depth ]: depth <= maxDepth && depth <= 3, |
| 'is-highlighted': isHighlighted, |
| } ); |
|
|
| |
| return ( |
| <li className={ postCommentClassnames }> |
| <div className="comments__comment-author"> |
| { commentAuthorUrl ? ( |
| <a href={ commentAuthorUrl } onClick={ this.handleAuthorClick } tabIndex={ -1 }> |
| <GravatarWithHovercards user={ comment.author } /> |
| </a> |
| ) : ( |
| <GravatarWithHovercards user={ comment.author } /> |
| ) } |
| |
| { this.renderAuthorTag( { |
| authorUrl: commentAuthorUrl, |
| authorName: commentAuthorName, |
| commentId, |
| className: 'comments__comment-username', |
| } ) } |
| { this.props.showNestingReplyArrow && parentAuthorName && ( |
| <span className="comments__comment-respondee"> |
| <Gridicon icon="chevron-right" size={ 16 } /> |
| { this.renderAuthorTag( { |
| className: 'comments__comment-respondee-link', |
| authorName: parentAuthorName, |
| authorUrl: parentAuthorUrl, |
| commentId: parentCommentId, |
| } ) } |
| </span> |
| ) } |
| <div className="comments__comment-timestamp"> |
| <a |
| href={ comment.URL } |
| target="_blank" |
| rel="noopener noreferrer" |
| onClick={ this.handleCommentPermalinkClick } |
| > |
| <TimeSince date={ comment.date } /> |
| </a> |
| </div> |
| </div> |
| |
| { comment.status && comment.status === 'unapproved' ? ( |
| <p className="comments__comment-moderation"> |
| { translate( 'Your comment is awaiting moderation.' ) } |
| </p> |
| ) : null } |
| |
| <PostCommentContent |
| content={ comment.content } |
| setWithDimensionsRef={ this.props.setWithDimensionsRef } |
| isPlaceholder={ comment.isPlaceholder } |
| className={ displayType } |
| /> |
| |
| <CommentActions |
| post={ this.props.post || {} } |
| comment={ comment } |
| activeReplyCommentId={ this.props.activeReplyCommentId } |
| commentId={ this.props.commentId } |
| handleReply={ this.handleReply } |
| onLikeToggle={ this.onLikeToggle } |
| onReplyCancel={ this.props.onReplyCancel } |
| showReadMore={ overflowY && ! this.state.showFull && showReadMoreInActions } |
| onReadMore={ this.onReadMore } |
| /> |
| |
| { haveReplyWithError ? null : this.renderCommentForm() } |
| { this.shouldRenderCaterpillar() && ( |
| <ConversationCaterpillar |
| blogId={ post.site_ID } |
| postId={ post.ID } |
| parentCommentId={ commentId } |
| commentsToShow={ commentsToShow } |
| /> |
| ) } |
| { this.renderRepliesList() } |
| </li> |
| ); |
| } |
| } |
|
|
| const ConnectedPostComment = connect( |
| ( state ) => ( { |
| currentUser: getCurrentUser( state ), |
| isLoggedIn: isUserLoggedIn( state ), |
| } ), |
| { expandComments, recordReaderTracksEvent, registerLastActionRequiresLogin } |
| )( withDimensions( PostComment ) ); |
|
|
| export default ConnectedPostComment; |
|
|