2015-08-04 08:19:07 +08:00
|
|
|
React = require 'react'
|
|
|
|
classNames = require 'classnames'
|
|
|
|
|
|
|
|
MessageItem = require './message-item'
|
|
|
|
|
2015-08-05 10:35:56 +08:00
|
|
|
{Utils,
|
|
|
|
DraftStore,
|
2015-08-04 08:19:07 +08:00
|
|
|
MessageStore} = require 'nylas-exports'
|
|
|
|
|
|
|
|
{InjectedComponent} = require 'nylas-component-kit'
|
|
|
|
|
|
|
|
class MessageItemContainer extends React.Component
|
|
|
|
@displayName = 'MessageItemContainer'
|
|
|
|
|
|
|
|
@propTypes =
|
|
|
|
thread: React.PropTypes.object.isRequired
|
|
|
|
message: React.PropTypes.object.isRequired
|
|
|
|
|
2015-08-04 08:24:31 +08:00
|
|
|
# The localId (in the case of draft's local ID) is a derived
|
2015-08-04 08:19:07 +08:00
|
|
|
# property that only the parent MessageList knows about.
|
2015-08-04 08:24:31 +08:00
|
|
|
localId: React.PropTypes.string
|
2015-08-04 08:19:07 +08:00
|
|
|
|
|
|
|
collapsed: React.PropTypes.bool
|
|
|
|
isLastMsg: React.PropTypes.bool
|
|
|
|
isBeforeReplyArea: React.PropTypes.bool
|
|
|
|
onRequestScrollTo: React.PropTypes.func
|
|
|
|
|
|
|
|
constructor: (@props) ->
|
|
|
|
@state = @_getStateFromStores()
|
|
|
|
|
|
|
|
componentWillReceiveProps: (newProps) ->
|
|
|
|
@setState(@_getStateFromStores())
|
|
|
|
|
|
|
|
componentDidMount: =>
|
|
|
|
if @props.message?.draft
|
|
|
|
@unlisten = DraftStore.listen @_onSendingStateChanged
|
|
|
|
|
2015-08-05 10:35:56 +08:00
|
|
|
shouldComponentUpdate: (nextProps, nextState) =>
|
|
|
|
not Utils.isEqualReact(nextProps, @props) or
|
|
|
|
not Utils.isEqualReact(nextState, @state)
|
|
|
|
|
2015-08-04 08:19:07 +08:00
|
|
|
componentWillUnmount: =>
|
|
|
|
@unlisten() if @unlisten
|
|
|
|
|
|
|
|
focus: => @refs.message.focus?()
|
|
|
|
|
|
|
|
render: =>
|
|
|
|
if @props.message.draft
|
|
|
|
if @state.isSending
|
2015-08-05 10:35:56 +08:00
|
|
|
@_renderMessage(pending: true)
|
2015-08-04 08:19:07 +08:00
|
|
|
else
|
|
|
|
@_renderComposer()
|
|
|
|
else
|
2015-08-05 10:35:56 +08:00
|
|
|
@_renderMessage(pending: false)
|
|
|
|
|
|
|
|
_renderMessage: ({pending}) =>
|
|
|
|
<MessageItem ref="message"
|
|
|
|
pending={pending}
|
|
|
|
thread={@props.thread}
|
|
|
|
message={@props.message}
|
|
|
|
className={@_classNames()}
|
|
|
|
collapsed={@props.collapsed}
|
|
|
|
isLastMsg={@props.isLastMsg} />
|
2015-08-04 08:19:07 +08:00
|
|
|
|
|
|
|
_renderComposer: =>
|
|
|
|
props =
|
|
|
|
mode: "inline"
|
2015-08-04 08:24:31 +08:00
|
|
|
localId: @props.localId
|
2015-08-04 08:19:07 +08:00
|
|
|
threadId: @props.thread.id
|
|
|
|
onRequestScrollTo: @props.onRequestScrollTo
|
|
|
|
|
|
|
|
<InjectedComponent ref="message"
|
|
|
|
matching={role: "Composer"}
|
|
|
|
className={@_classNames()}
|
|
|
|
exposedProps={props} />
|
|
|
|
|
|
|
|
_classNames: => classNames
|
|
|
|
"draft": @props.message.draft
|
|
|
|
"unread": @props.message.unread
|
|
|
|
"collapsed": @props.collapsed
|
|
|
|
"message-item-wrap": true
|
|
|
|
"before-reply-area": @props.isBeforeReplyArea
|
|
|
|
|
|
|
|
_onSendingStateChanged: (draftLocalId) =>
|
2015-08-04 08:24:31 +08:00
|
|
|
@setState(@_getStateFromStores()) if draftLocalId is @props.localId
|
2015-08-04 08:19:07 +08:00
|
|
|
|
|
|
|
_getStateFromStores: ->
|
2015-08-04 08:24:31 +08:00
|
|
|
isSending: DraftStore.isSendingDraft(@props.localId)
|
2015-08-04 08:19:07 +08:00
|
|
|
|
|
|
|
module.exports = MessageItemContainer
|