React = require 'react'
classNames = require 'classnames'
_ = require 'underscore'
EmailFrame = require('./email-frame').default
MessageParticipants = require "./message-participants"
MessageItemBody = require "./message-item-body"
MessageTimestamp = require "./message-timestamp"
MessageControls = require './message-controls'
{Utils,
Actions,
MessageUtils,
AccountStore,
MessageStore,
MessageBodyProcessor,
QuotedHTMLTransformer,
ComponentRegistry,
FileDownloadStore} = require 'nylas-exports'
{RetinaImg,
InjectedComponentSet,
InjectedComponent} = require 'nylas-component-kit'
TransparentPixel = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGNikAQAACIAHF/uBd8AAAAASUVORK5CYII="
class MessageItem extends React.Component
@displayName = 'MessageItem'
@propTypes =
thread: React.PropTypes.object.isRequired
message: React.PropTypes.object.isRequired
collapsed: React.PropTypes.bool
constructor: (@props) ->
@state =
# Holds the downloadData (if any) for all of our files. It's a hash
# keyed by a fileId. The value is the downloadData.
downloads: FileDownloadStore.downloadDataForFiles(@props.message.fileIds())
detailedHeaders: false
componentDidMount: =>
@_storeUnlisten = FileDownloadStore.listen(@_onDownloadStoreChange)
componentWillUnmount: =>
@_storeUnlisten() if @_storeUnlisten
shouldComponentUpdate: (nextProps, nextState) =>
not Utils.isEqualReact(nextProps, @props) or
not Utils.isEqualReact(nextState, @state)
render: =>
if @props.collapsed
@_renderCollapsed()
else
@_renderFull()
_renderCollapsed: =>
attachmentIcon = []
if Utils.showIconForAttachments(@props.message.files)
attachmentIcon =
{@props.message.from?[0]?.displayName(compact: true)}
{@props.message.snippet}
{attachmentIcon}
_renderFull: =>
{@_renderHeader()}
{@_renderAttachments()}
_renderHeader: =>
classes = classNames
"message-header": true
"pending": @props.pending
{@_renderHeaderSideItems()}
{@_renderFromParticipants()}
{@_renderToParticipants()}
{@_renderFolder()}
{@_renderHeaderDetailToggle()}
_renderFromParticipants: =>
_renderToParticipants: =>
_renderFolder: =>
return [] unless @state.detailedHeaders
acct = AccountStore.accountForId(@props.message.accountId)
acctUsesFolders = acct and acct.usesFolders()
folder = @props.message.categories?[0]
return unless folder and acctUsesFolders
Folder:
{folder.displayName}
_onClickParticipants: (e) =>
el = e.target
while el isnt e.currentTarget
if "collapsed-participants" in el.classList
@setState(detailedHeaders: true)
e.stopPropagation()
return
el = el.parentElement
return
_onClickHeader: (e) =>
return if @state.detailedHeaders
el = e.target
while el isnt e.currentTarget
wl = ["message-header-right",
"collapsed-participants",
"header-toggle-control"]
if "message-header-right" in el.classList then return
if "collapsed-participants" in el.classList then return
el = el.parentElement
@_toggleCollapsed()
_onDownloadAll: =>
Actions.fetchAndSaveAllFiles(@props.message.files)
_renderDownloadAllButton: =>
{@props.message.files.length} attachments
-
Download all
_renderAttachments: =>
attachments = @_attachmentComponents()
if attachments.length > 0
{if attachments.length > 1 then @_renderDownloadAllButton()}
{attachments}
else
_renderHeaderSideItems: ->
styles =
position: "absolute"
marginTop: -2
_renderHeaderDetailToggle: =>
return null if @props.pending
if @state.detailedHeaders
@setState(detailedHeaders: false); e.stopPropagation()}>
else
@setState(detailedHeaders: true); e.stopPropagation()}>
_toggleCollapsed: =>
return if @props.isLastMsg
Actions.toggleMessageIdExpanded(@props.message.id)
_formatContacts: (contacts=[]) =>
_attachmentComponents: =>
imageAttachments = []
otherAttachments = []
for file in (@props.message.files ? [])
continue unless @_isRealFile(file)
if Utils.shouldDisplayAsImage(file)
imageAttachments.push(file)
else
otherAttachments.push(file)
otherAttachments = otherAttachments.map (file) =>
imageAttachments = imageAttachments.map (file) =>
props =
file: file
download: @state.downloads[file.id]
targetPath: FileDownloadStore.pathForFile(file)
return otherAttachments.concat(imageAttachments)
_isRealFile: (file) ->
hasCIDInBody = file.contentId? and @props.message.body?.indexOf(file.contentId) > 0
return not hasCIDInBody
_onDownloadStoreChange: =>
@setState
downloads: FileDownloadStore.downloadDataForFiles(@props.message.fileIds())
module.exports = MessageItem