File size: 4,307 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
import { localize } from 'i18n-calypso';
import { Component } from 'react';
import { html } from '../indices-to-html';
import { bumpStat } from '../rest-client/bump-stat';
import { wpcom } from '../rest-client/wpcom';
import NoteActions from './actions';
import Comment from './block-comment';
import Post from './block-post';
import PromptBlock from './block-prompt';
import User from './block-user';
import { p, zipWithSignature } from './functions';
import NotePreface from './preface';

/* eslint-disable wpcalypso/jsx-classname-namespace */

function ReplyBlock( { block } ) {
	// explicitly send className of '' here so we don't get the default of "paragraph"
	const replyText = p( html( block ), '' );

	return <div className="wpnc__reply">{ replyText }</div>;
}

export class NoteBody extends Component {
	state = {
		reply: null,
	};

	isMounted = false;

	componentDidMount() {
		this.isMounted = true;

		const { note } = this.props;
		bumpStat( 'notes-click-type', note.type );

		if ( note.meta && note.meta.ids.reply_comment ) {
			const hasReplyBlock = note.body.some(
				( block ) =>
					block.ranges &&
					block.ranges.length > 1 &&
					block.ranges[ 1 ].id === note.meta.ids.reply_comment
			);

			if ( ! hasReplyBlock ) {
				wpcom()
					.site( note.meta.ids.site )
					.comment( note.meta.ids.reply_comment )
					.get( ( error, data ) => {
						if ( error || ! this.isMounted ) {
							return;
						}

						this.setState( { reply: data } );
					} );
			}
		}
	}

	componentWillUnmount() {
		this.isMounted = false;
	}

	render() {
		let blocks = zipWithSignature( this.props.note.body, this.props.note );
		let actions = '';
		let preface = '';
		let replyBlock = null;
		let replyMessage;
		let firstNonTextIndex;
		let i;

		for ( i = 0; i < blocks.length; i++ ) {
			if ( 'text' !== blocks[ i ].signature.type ) {
				firstNonTextIndex = i;
				break;
			}
		}

		if ( firstNonTextIndex ) {
			preface = <NotePreface blocks={ this.props.note.body.slice( 0, i ) } />;
			blocks = blocks.slice( i );
		}

		const body = [];
		for ( i = 0; i < blocks.length; i++ ) {
			const block = blocks[ i ];
			const blockKey = 'block-' + this.props.note.id + '-' + i;

			if ( block.block.actions && 'user' !== block.signature.type ) {
				actions = (
					<NoteActions
						note={ this.props.note }
						block={ block.block }
						global={ this.props.global }
					/>
				);
			}

			switch ( block.signature.type ) {
				case 'user':
					body.push(
						<User
							key={ blockKey }
							block={ block.block }
							noteType={ this.props.note.type }
							note={ this.props.note }
							timestamp={ this.props.note.timestamp }
							url={ this.props.note.url }
						/>
					);
					break;
				case 'comment':
					body.push(
						<Comment key={ blockKey } block={ block.block } meta={ this.props.note.meta } />
					);
					break;
				case 'post':
					body.push(
						<Post key={ blockKey } block={ block.block } meta={ this.props.note.meta } />
					);
					break;
				case 'reply':
					replyBlock = <ReplyBlock key={ blockKey } block={ block.block } />;
					break;
				case 'prompt':
					body.push(
						<PromptBlock key={ blockKey } block={ block.block } meta={ this.props.note.meta } />
					);
					break;
				default:
					body.push( <div key={ blockKey }>{ p( html( block.block ) ) }</div> );
					break;
			}
		}

		if ( this.state.reply && this.state.reply.URL ) {
			if ( this.props.note.meta.ids.comment ) {
				replyMessage = this.props.translate( 'You {{a}}replied{{/a}} to this comment.', {
					components: {
						a: <a href={ this.state.reply.URL } target="_blank" rel="noopener noreferrer" />,
					},
				} );
			} else {
				replyMessage = this.props.translate( 'You {{a}}replied{{/a}} to this post.', {
					components: {
						a: <a href={ this.state.reply.URL } target="_blank" rel="noopener noreferrer" />,
					},
				} );
			}

			replyBlock = (
				<div className="wpnc__reply">
					<span className="wpnc__gridicon"></span>
					{ replyMessage }
				</div>
			);
		}

		return (
			<div className="wpnc__body">
				{ preface }
				<div className="wpnc__body-content">{ body }</div>
				{ replyBlock }
				{ actions }
			</div>
		);
	}
}

/* eslint-enable wpcalypso/jsx-classname-namespace */

export default localize( NoteBody );