import { Button, Gridicon } from '@automattic/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import { get, includes, isEqual } from 'lodash'; import PropTypes from 'prop-types'; import { Component } from 'react'; import { connect } from 'react-redux'; import scrollTo from 'calypso/lib/scroll-to'; import { getMinimumComment } from 'calypso/my-sites/comments/comment/utils'; import { requestAdminMenu } from 'calypso/state/admin-menu/actions'; import { bumpStat, composeAnalytics, recordTracksEvent, withAnalytics, } from 'calypso/state/analytics/actions'; import { changeCommentStatus, deleteComment, likeComment, unlikeComment, } from 'calypso/state/comments/actions'; import { getSiteComment } from 'calypso/state/comments/selectors'; import { removeNotice, successNotice } from 'calypso/state/notices/actions'; const commentActions = { unapproved: [ 'like', 'approve', 'edit', 'reply', 'spam', 'trash' ], approved: [ 'like', 'approve', 'edit', 'reply', 'spam', 'trash', 'view' ], spam: [ 'approve', 'delete' ], trash: [ 'approve', 'spam', 'delete' ], }; const noop = () => {}; export class CommentActions extends Component { static propTypes = { siteId: PropTypes.number, postId: PropTypes.number, commentId: PropTypes.number, canModerateComment: PropTypes.bool, commentsListQuery: PropTypes.object, getCommentOffsetTop: PropTypes.func, redirect: PropTypes.func, toggleEditMode: PropTypes.func, toggleReply: PropTypes.func, updateLastUndo: PropTypes.func, }; static defaultProps = { updateLastUndo: noop, }; shouldComponentUpdate = ( nextProps ) => ! isEqual( this.props, nextProps ); delete = () => { if ( typeof window === 'undefined' || window.confirm( this.props.translate( 'Delete this comment permanently?' ) ) ) { this.props.deletePermanently(); this.props.redirect?.(); } }; hasAction = ( action ) => includes( commentActions[ this.props.commentStatus ], action ); setSpam = () => this.setStatus( 'spam' ); setStatus = ( status ) => { const { changeStatus, commentIsLiked, commentStatus, unlike, updateLastUndo } = this.props; const alsoUnlike = commentIsLiked && 'approved' !== status; updateLastUndo( null ); changeStatus( status, { alsoUnlike, previousStatus: commentStatus, } ); if ( alsoUnlike ) { unlike(); } this.showNotice( status ); // Refresh the admin menu on update of status to ensure count shown is not stale. this.props.refreshAdminMenu(); }; setTrash = () => this.setStatus( 'trash' ); showNotice = ( status ) => { const { minimumComment, translate } = this.props; this.props.removeNotice( 'comment-notice' ); const message = get( { approved: translate( 'Comment approved.' ), unapproved: translate( 'Comment unapproved.' ), spam: translate( 'Comment marked as spam.' ), trash: translate( 'Comment moved to trash.' ), }, status ); const noticeOptions = { button: translate( 'Undo' ), id: 'comment-notice', isPersistent: true, onClick: this.undo( status, minimumComment ), }; this.props.successNotice( message, noticeOptions ); }; undo = ( status, previousCommentData ) => () => { const { changeStatus, commentId, like, unlike, updateLastUndo } = this.props; const { isLiked: wasLiked, status: previousStatus } = previousCommentData; const alsoApprove = 'approved' !== status && 'approved' === previousStatus; const alsoUnlike = ! wasLiked && 'approved' !== previousStatus; updateLastUndo( commentId ); changeStatus( previousStatus, { alsoUnlike, isUndo: true, previousStatus: status, } ); if ( wasLiked ) { like( { alsoApprove } ); } else if ( alsoUnlike ) { unlike(); } this.props.removeNotice( 'comment-notice' ); }; toggleApproved = () => this.setStatus( this.props.commentIsApproved ? 'unapproved' : 'approved' ); toggleEditMode = () => { this.props.toggleEditMode(); scrollTo( { x: 0, y: this.props.getCommentOffsetTop() } ); }; toggleLike = () => { const { commentIsLiked, commentStatus, like, unlike } = this.props; if ( commentIsLiked ) { return unlike(); } const alsoApprove = 'unapproved' === commentStatus; like( { alsoApprove } ); if ( alsoApprove ) { this.setStatus( 'approved' ); } }; render() { const { canModerateComment, commentIsApproved, commentIsLiked, commentURL, toggleReply, translate, } = this.props; return (