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
{ replyText }
; } 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 = ; 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 = ( ); } switch ( block.signature.type ) { case 'user': body.push( ); break; case 'comment': body.push( ); break; case 'post': body.push( ); break; case 'reply': replyBlock = ; break; case 'prompt': body.push( ); break; default: body.push(
{ p( html( block.block ) ) }
); 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: , }, } ); } else { replyMessage = this.props.translate( 'You {{a}}replied{{/a}} to this post.', { components: { a: , }, } ); } replyBlock = (
{ replyMessage }
); } return (
{ preface }
{ body }
{ replyBlock } { actions }
); } } /* eslint-enable wpcalypso/jsx-classname-namespace */ export default localize( NoteBody );