File size: 7,940 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
import { Card, FoldableCard } from '@automattic/components';
import { isWithinBreakpoint } from '@automattic/viewport';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { debounce, get, isEqual } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import ReactDom from 'react-dom';
import { connect } from 'react-redux';
import QueryComment from 'calypso/components/data/query-comment';
import scrollTo from 'calypso/lib/scroll-to';
import CommentActions from 'calypso/my-sites/comments/comment/comment-actions';
import CommentContent from 'calypso/my-sites/comments/comment/comment-content';
import CommentEdit from 'calypso/my-sites/comments/comment/comment-edit';
import CommentHeader from 'calypso/my-sites/comments/comment/comment-header';
import CommentReply from 'calypso/my-sites/comments/comment/comment-reply';
import { getMinimumComment } from 'calypso/my-sites/comments/comment/utils';
import { getSiteComment } from 'calypso/state/comments/selectors';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';

import './style.scss';

export class Comment extends Component {
	static propTypes = {
		siteId: PropTypes.number,
		postId: PropTypes.number,
		commentId: PropTypes.number,
		commentsListQuery: PropTypes.object,
		isAtMaxDepth: PropTypes.bool,
		isBulkMode: PropTypes.bool,
		isPostView: PropTypes.bool,
		isSelected: PropTypes.bool,
		redirect: PropTypes.func,
		refreshCommentData: PropTypes.bool,
		isSingularEditMode: PropTypes.bool,
		toggleSelected: PropTypes.func,
		updateLastUndo: PropTypes.func,
	};

	constructor( props ) {
		super( props );

		this.state = {
			isReplyVisible: false,
			offsetTop: 0,
		};
	}

	// @TODO: Please update https://github.com/Automattic/wp-calypso/issues/58453 if you are refactoring away from UNSAFE_* lifecycle methods!
	UNSAFE_componentWillMount() {
		this.debounceScrollToOffset = debounce( this.scrollToOffset, 100 );
	}

	// @TODO: Please update https://github.com/Automattic/wp-calypso/issues/58453 if you are refactoring away from UNSAFE_* lifecycle methods!
	UNSAFE_componentWillReceiveProps( nextProps ) {
		const { isBulkMode: wasBulkMode, isPostView: wasPostView } = this.props;
		const { isBulkMode, isPostView } = nextProps;

		const offsetTop =
			wasPostView !== isPostView || `#comment-${ this.props.commentId }` !== window.location.hash
				? 0
				: this.getCommentOffsetTop();

		this.setState( ( { isReplyVisible } ) => ( {
			isReplyVisible: wasBulkMode !== isBulkMode ? false : isReplyVisible,
			offsetTop,
		} ) );
	}

	componentDidUpdate( prevProps, prevState ) {
		if ( prevState.offsetTop !== this.state.offsetTop ) {
			this.debounceScrollToOffset( this.state.offsetTop );
		}
	}

	shouldComponentUpdate = ( nextProps, nextState ) =>
		! isEqual( this.props, nextProps ) || ! isEqual( this.state, nextState );

	storeCardRef = ( card ) => ( this.commentCard = card );

	keyDownHandler = ( event ) => {
		const { isBulkMode } = this.props;
		const commentHasFocus =
			document &&
			this.commentCard &&
			document.activeElement === ReactDom.findDOMNode( this.commentCard );

		if ( ! isBulkMode || ! commentHasFocus ) {
			return;
		}

		switch ( event.keyCode ) {
			case 13: // enter
			case 32: // space
				event.preventDefault();
				return this.toggleSelected();
		}
	};

	getCommentOffsetTop = () => {
		if ( ! window ) {
			return 0;
		}

		const { isPostView } = this.props;
		const { offsetTop } = this.state;

		// On >660px, adjust the comment card `offsetTop` to avoid being covered by the masterbar.
		// 56px = 48px (masterbar height) + 8px (comment card vertical margin)
		// 66px = 58px (post view sticky header) + 8px (comment card vertical margin)
		const offsetAdjustment = ~~isWithinBreakpoint( '>660px' ) && 56 - ( isPostView && 66 );

		const commentNode = ReactDom.findDOMNode( this.commentCard );
		const newOffsetTop = commentNode.offsetTop;

		return newOffsetTop > offsetAdjustment && newOffsetTop > offsetTop
			? newOffsetTop - offsetAdjustment
			: offsetTop;
	};

	scrollToOffset = () => {
		if ( ! window || `#comment-${ this.props.commentId }` !== window.location.hash ) {
			return;
		}
		const { offsetTop } = this.state;
		scrollTo( { x: 0, y: offsetTop } );
	};

	toggleEditMode = () => {
		this.props.onToggleEditMode( this.props.commentId );
	};

	toggleReply = () =>
		this.setState( ( { isReplyVisible } ) => ( { isReplyVisible: ! isReplyVisible } ) );

	toggleSelected = () => this.props.toggleSelected( this.props.minimumComment );

	renderComment() {
		const {
			siteId,
			postId,
			commentId,
			commentsListQuery,
			isBulkMode,
			isLoading,
			isPostView,
			isSelected,
			isSingularEditMode,
			redirect,
			refreshCommentData,
			updateLastUndo,
		} = this.props;

		const { isReplyVisible } = this.state;

		const isEditMode = isSingularEditMode && ! isBulkMode;

		return (
			<>
				{ refreshCommentData && (
					<QueryComment commentId={ commentId } siteId={ siteId } forceWpcom />
				) }

				{ ( ! isEditMode || isLoading ) && (
					<div className="comment__detail">
						<CommentHeader { ...{ commentId, isBulkMode, isEditMode, isPostView, isSelected } } />

						<CommentContent { ...{ commentId, isBulkMode, isPostView } } />

						{ ! isBulkMode && (
							<CommentActions
								{ ...{ siteId, postId, commentId, commentsListQuery, redirect, updateLastUndo } }
								getCommentOffsetTop={ this.getCommentOffsetTop }
								toggleEditMode={ this.toggleEditMode }
								toggleReply={ this.toggleReply }
							/>
						) }

						{ ! isBulkMode && (
							<CommentReply { ...{ commentId, commentsListQuery, isReplyVisible } } />
						) }
					</div>
				) }

				{ isEditMode && ! isLoading && (
					<CommentEdit { ...{ commentId } } toggleEditMode={ this.toggleEditMode } />
				) }
			</>
		);
	}

	render() {
		const {
			commentHasNoReply,
			commentId,
			commentIsPending,
			isAtMaxDepth,
			isBulkMode,
			isLoading,
			isSingularEditMode,
			isOwnComment,
			filterUnreplied,
			translate,
		} = this.props;

		const { isReplyVisible } = this.state;

		const isEditMode = isSingularEditMode && ! isBulkMode;

		const classes = clsx( 'comment', {
			'is-at-max-depth': isAtMaxDepth,
			'is-bulk-mode': isBulkMode,
			'is-edit-mode': isEditMode,
			'is-placeholder': isLoading,
			'is-pending': commentIsPending,
			'is-reply-visible': isReplyVisible,
		} );

		if ( filterUnreplied && ! isBulkMode && ( ! commentHasNoReply || isOwnComment ) ) {
			return (
				<FoldableCard
					className={ classes }
					compact
					header={
						isOwnComment
							? translate( 'This is your own comment' )
							: translate( "You've already replied to this comment" )
					}
					id={ `comment-${ commentId }` }
					onClick={ isBulkMode ? this.toggleSelected : undefined }
					onKeyDown={ this.keyDownHandler }
					ref={ this.storeCardRef }
				>
					{ this.renderComment() }
				</FoldableCard>
			);
		}

		return (
			<Card
				className={ classes }
				id={ `comment-${ commentId }` }
				onClick={ isBulkMode ? this.toggleSelected : undefined }
				onKeyDown={ this.keyDownHandler }
				ref={ this.storeCardRef }
			>
				{ this.renderComment() }
			</Card>
		);
	}
}

const mapStateToProps = ( state, { commentId } ) => {
	const siteId = getSelectedSiteId( state );
	const comment = getSiteComment( state, siteId, commentId );
	const commentStatus = get( comment, 'status' );
	const currentUserId = getCurrentUserId( state );
	return {
		siteId,
		postId: get( comment, 'post.ID' ),
		commentIsPending: 'unapproved' === commentStatus,
		commentHasNoReply: ! get( comment, 'i_replied' ),
		isLoading: typeof comment === 'undefined',
		isOwnComment: get( comment, 'author.ID' ) === currentUserId,
		minimumComment: getMinimumComment( comment ),
	};
};

export default connect( mapStateToProps )( localize( Comment ) );