<commit_before>kd                   = require 'kd'
React                = require 'kd-react'
ReactDOM             = require 'react-dom'
Avatar               = require 'app/components/profile/avatar'
Link                 = require 'app/components/common/link'
TimeAgo              = require 'app/components/common/timeago'
immutable            = require 'immutable'
classnames           = require 'classnames'
MessageLink          = require 'activity/components/messagelink'
MessageBody          = require 'activity/components/common/messagebody'
ProfileText          = require 'app/components/profile/profiletext'
MessageLikeSummary   = require 'activity/components/common/messagelikesummary'
ProfileLinkContainer = require 'app/components/profile/profilelinkcontainer'
AutoSizeTextarea     = require 'app/components/common/autosizetextarea'
toImmutable          = require 'app/util/toImmutable'
ActivityLikeLink     = require 'activity/components/chatlistitem/activitylikelink'
ActivityFlux         = require 'activity/flux'
CommentList          = require 'activity/components/commentlist'
fetchAccount         = require 'app/util/fetchAccount'
whoami               = require 'app/util/whoami'
ActivitySharePopup   = require 'activity/components/activitysharepopup'

module.exports = class FeedItem extends React.Component

  @defaultProps =
    message: immutable.Map()

  constructor: (props) ->

    super props

    @state =
      comment      : ''
      focusOnInput : no
      isPopupOpen  : no


  shouldComponentUpdate: (nextProps, nextState) ->

    return @props.message isnt nextProps.message or @state isnt nextState


  sendComment: (event) ->

    kd.utils.stopDOMEvent event
    textInput = ReactDOM.findDOMNode @refs.textInput
    @setState hasValue: yes  if textInput.value.trim()
    textInput.focus()


  postComment: (event) ->

    kd.utils.stopDOMEvent event

    input = ReactDOM.findDOMNode @refs.textInput
    body  = input.value

    ActivityFlux.actions.message.createComment @props.message.get('id'), body
      .then => @setState { comment: '', hasValue: no }


  toggleSharePopupVisibility: (event) ->

    kd.utils.stopDOMEvent event

    @setState { isPopupOpen: not @state.isPopupOpen }


  getPostButtonClassNames: -> classnames
    'FeedItem-postComment': yes
    'green' : @state.hasValue
    'hidden': not @state.focusOnInput and not @state.hasValue


  onFocus: (event) -> @setState focusOnInput: yes


  onBlur: (event) -> @setState focusOnInput: no


  handleCommentInputChange: (event) ->

    hasValue = no
    value    = event.target.value.trim()
    hasValue = yes  if value
    @setState { hasValue: hasValue, comment: value }


  renderFeedItemSummary: (message) ->

    actorsCount   = message.getIn ['interactions', 'like', 'actorsCount']

    return null  unless actorsCount

    <div className="FeedItem-summary">
      <MessageLikeSummary message={message} />
    </div>


  onMentionClick: (reply) ->

    kd.utils.stopDOMEvent event

    account = reply.get 'account'
    textInput = ReactDOM.findDOMNode @refs.textInput

    fetchAccount account.toJS(), (err, account) =>
      return  unless account

      inputValue = if textInput.value then "#{textInput.value} " else ""
      value      = "#{inputValue}@#{account.profile.nickname} "

      @setState { comment: value, hasValue: yes }, ->
        textInput.focus()


  renderCommentForm: ->

    <div className='FeedItem-commentForm'>
      <AutoSizeTextarea
        ref         = 'textInput'
        onFocus     = { @bound 'onFocus' }
        onBlur      = { @bound 'onBlur' }
        placeholder = 'Type your comment'
        onChange    = { @bound 'handleCommentInputChange' }
        value       = {@state.comment}
        className   = 'FeedItem-commentInput'/>
      <button
        className={@getPostButtonClassNames()}
        onClick={ @bound 'postComment' }>SEND</button>
    </div><commit_msg>Client: Activity: Formatting and renamed comment state to commentValue<commit_after>kd                   = require 'kd'
React                = require 'kd-react'
ReactDOM             = require 'react-dom'
Avatar               = require 'app/components/profile/avatar'
Link                 = require 'app/components/common/link'
TimeAgo              = require 'app/components/common/timeago'
immutable            = require 'immutable'
classnames           = require 'classnames'
MessageLink          = require 'activity/components/messagelink'
MessageBody          = require 'activity/components/common/messagebody'
ProfileText          = require 'app/components/profile/profiletext'
MessageLikeSummary   = require 'activity/components/common/messagelikesummary'
ProfileLinkContainer = require 'app/components/profile/profilelinkcontainer'
AutoSizeTextarea     = require 'app/components/common/autosizetextarea'
toImmutable          = require 'app/util/toImmutable'
ActivityLikeLink     = require 'activity/components/chatlistitem/activitylikelink'
ActivityFlux         = require 'activity/flux'
CommentList          = require 'activity/components/commentlist'
fetchAccount         = require 'app/util/fetchAccount'
whoami               = require 'app/util/whoami'
ActivitySharePopup   = require 'activity/components/activitysharepopup'

module.exports = class FeedItem extends React.Component

  @defaultProps =
    message: immutable.Map()

  constructor: (props) ->

    super props

    @state =
      hasValue     : no
      commentValue : ''
      focusOnInput : no
      isPopupOpen  : no


  shouldComponentUpdate: (nextProps, nextState) ->

    return @props.message isnt nextProps.message or @state isnt nextState


  sendComment: (event) ->

    kd.utils.stopDOMEvent event
    textInput = ReactDOM.findDOMNode @refs.textInput
    @setState hasValue: yes  if textInput.value.trim()
    textInput.focus()


  postComment: (event) ->

    kd.utils.stopDOMEvent event

    input = ReactDOM.findDOMNode @refs.textInput
    body  = input.value

    ActivityFlux.actions.message.createComment @props.message.get('id'), body
      .then => @setState { commentValue: '', hasValue: no }


  toggleSharePopupVisibility: (event) ->

    kd.utils.stopDOMEvent event

    @setState { isPopupOpen: not @state.isPopupOpen }


  getPostButtonClassNames: -> classnames
    'FeedItem-postComment' : yes
    'green'                : @state.hasValue
    'hidden'               : not @state.focusOnInput and not @state.hasValue


  onFocus: (event) -> @setState focusOnInput: yes


  onBlur: (event) -> @setState focusOnInput: no


  handleCommentInputChange: (event) ->

    hasValue = no
    value    = event.target.value.trim()
    hasValue = yes  if value
    @setState { hasValue: hasValue, commentValue: value }


  renderFeedItemSummary: (message) ->

    actorsCount = message.getIn ['interactions', 'like', 'actorsCount']

    return null  unless actorsCount

    <div className="FeedItem-summary">
      <MessageLikeSummary message={message} />
    </div>


  onMentionClick: (reply) ->

    kd.utils.stopDOMEvent event

    account = reply.get 'account'
    textInput = ReactDOM.findDOMNode @refs.textInput

    fetchAccount account.toJS(), (err, account) =>
      return  unless account

      inputValue = if textInput.value then "#{textInput.value} " else ""
      value      = "#{inputValue}@#{account.profile.nickname} "

      @setState { commentValue: value, hasValue: yes }, ->
        textInput.focus()


  renderCommentForm: ->

    <div className='FeedItem-commentForm'>
      <AutoSizeTextarea
        ref         = 'textInput'
        onFocus     = { @bound 'onFocus' }
        onBlur      = { @bound 'onBlur' }
        placeholder = 'Type your comment'
        onChange    = { @bound 'handleCommentInputChange' }
        value       = {@state.commentValue}
        className   = 'FeedItem-commentInput'/>
      <button
        className={@getPostButtonClassNames()}
        onClick={ @bound 'postComment' }>SEND</button>
    </div>
{'fraction_non_alphanumeric': 0.10439698492462311, 'fraction_numerical': 0.0, 'mean_word_length': 2.823727185398655, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6641839', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2239, 'n_words': 497}