_ = require 'underscore'
path = require 'path'
React = require 'react'
{RetinaImg} = require 'nylas-component-kit'
{Actions, Utils} = require 'nylas-exports'
# Passed in as props from MessageItem and FileDownloadStore
# This is empty if the attachment isn't downloading.
# @props.download is a FileDownloadStore.Download object
# @props.file is a File object
class AttachmentComponent extends React.Component
@displayName: 'AttachmentComponent'
@propTypes:
file: React.PropTypes.object.isRequired,
download: React.PropTypes.object
removable: React.PropTypes.bool
targetPath: React.PropTypes.string
messageLocalId: React.PropTypes.string
constructor: (@props) ->
@state = progressPercent: 0
render: =>
{@_renderFileActions()}
{@props.file.filename ? "Unnamed Attachment"}
_renderFileActions: =>
if @props.removable
{@_renderRemoveIcon()}
else if @_isDownloading() and @_canAbortDownload()
{@_renderRemoveIcon()}
else
{@_renderDownloadButton()}
_downloadProgressStyle: =>
width: "#{@props.download?.percent ? 0}%"
_onClickRemove: =>
Actions.removeFile
file: @props.file
messageLocalId: @props.messageLocalId
_canAbortDownload: -> true
_renderRemoveIcon: ->
_renderDownloadButton: ->
_onClickView: => Actions.fetchAndOpenFile(@props.file) if @_canClickToView()
_onClickDownload: => Actions.fetchAndSaveFile(@props.file)
_onClickAbort: => Actions.abortDownload(@props.file, @props.download)
_canClickToView: => not @props.removable and not @_isDownloading()
_isDownloading: => @props.download?.state is "downloading"
_extension: -> @props.file.filename.split('.').pop()
module.exports = AttachmentComponent