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 = () => {}; /** * A PostComment is the visual representation for a comment within a tree of comments. * Each comment may have a different displayType. It will be one of: * 1. full: display the full content. no max-height. * 2. excerpt: show 3 lines. max-height clipping is involved. if the content overflows * then show a "Read More" button that will the comment to full * 3. singleLine: show only 1 line of the comment and then show a Read More if the content overflows. * 4. hidden: do not show at all. this is implied by a lack of displayType. * * - An individual PostComment determines its displayType by looking at the prop commentsToShow. * displayType = commentsToShow[ commentId ] || hidden; * - If a PostComment has caterpillars enabled, it will show a caterpillar if it has hidden children. * * As of the time of this comment writing, ReaderFullPost uses exclusively 'full' comments, whereas * conversations tool uses a mix depending on the situation. */ class PostComment extends PureComponent { static propTypes = { commentsTree: PropTypes.object.isRequired, commentId: PropTypes.oneOfType( [ PropTypes.string, // can be 'placeholder-123' 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, /** * If commentsToShow is not provided then it is assumed that all child comments should be displayed. * If it is provided then it should have the following shape: * { * [ commentId ]: POST_COMMENT_DISPLAY_TYPE // (full, excerpt, singleLine, etc.) * } * - it specifies exactly which comments to display and with which displayType. * - if a comment's id is not in the object it is assumed that it should be hidden * */ commentsToShow: PropTypes.object, enableCaterpillar: PropTypes.bool, // connect()ed props: 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 ) { // Redirect to create account page when not logged in and the login window component is not enabled const { pathname } = getUrlParts( window.location.href ); if ( isReaderTagEmbedPage( window.location ) ) { return window.open( createAccountUrl( { redirectTo: pathname, ref: 'reader-lp' } ), '_blank' ); } // Do not redirect to create account page when not logged in and the login window component is enabled 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 } ); // show the comments when replying }; 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 ) ) ); }; // has hidden child --> true shouldRenderCaterpillar = () => { const { enableCaterpillar, commentsToShow, commentId } = this.props; const childIds = this.getAllChildrenIds( commentId ); return ( enableCaterpillar && commentsToShow && some( childIds, ( id ) => ! commentsToShow[ id ] ) ); }; // has visisble child --> true 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' ] ); // Hide children if more than maxChildrenToShow, but not if replying const exceedsMaxChildrenToShow = commentChildrenIds && commentChildrenIds.length < maxChildrenToShow; const showReplies = this.state.showReplies || exceedsMaxChildrenToShow || enableCaterpillar; const childDepth = ! commentsToShow || commentsToShow[ commentId ] ? depth + 1 : depth; // No children to show 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 (
{ translate( 'Your comment is awaiting moderation.' ) }
) : null }