import * as React from 'react' ; import { connect } from 'react-redux' ; import Tooltip = require ( 'rc-tooltip' ) ; import Tweet , { TwitterUser } from '../../item/tweet' ; import { showMessage } from '../../actions/message' ; import { openEditor , openEditorForReply , } from '../../actions/editor' ; import TwitterRestApi from '../../twitter/rest_api' ; import { Dispatch } from '../../store' ; const electron = global . require ( 'electron' ) ; const InDebugMode = global . process . env . NODE_ENV === 'development' ; interface ConnectedProps { readonly status : Tweet ; readonly owner ? : TwitterUser ; } interface DispatchProps { readonly onDeleteTweet : ( e : React . MouseEvent ) => void ; readonly onUrlOpen : ( e : React . MouseEvent ) => void ; readonly onStatusOpen : ( e : React . MouseEvent ) => void ; readonly onCopyUrl : ( e : React . MouseEvent ) => void ; readonly onCopyJson : ( e : React . MouseEvent ) => void ; readonly onCorrectTweet : ( e : React . MouseEvent ) => void ; } type OtherActionsButtonProps = ConnectedProps & DispatchProps & React . Props < any > ; function statusUrlToClipboard ( status , dispatch ) { const url = status . getMainStatus ( ) . statusPageUrl ( ) ; electron . clipboard . write ( { text : url } ) ; dispatch ( showMessage ( 'Copied status URL to clipboard.' , 'info' ) ) ; } function copyJson ( status , dispatch ) { const json = JSON . stringify ( status . json , null , 2 ) ; electron . clipboard . write ( { text : json } ) ; dispatch ( showMessage ( 'Copied status JSON to clipboard.' , 'info' ) ) ; } function renderDeleteThisTweet ( props ) { const isMyTweet = props . owner && props . status . user . id === props . owner . id ; if ( ! isMyTweet ) { return undefined ; } return ( < div className = "tweet-actions__others-menu-item" onClick = { props . onDeleteTweet } > Delete this tweet < /div>); } function correctThisTweet ( status , owner , dispatch ) { TwitterRestApi . destroyStatus ( status . id ) . then ( ( ) => { if ( status . in_reply_to_status !== null ) { dispatch ( openEditorForReply ( status . in_reply_to_status , owner , status . text ) ) ; } else { dispatch ( openEditor ( status . text ) ) ; if ( status . hasInReplyTo ( ) ) { log . warn ( 'Corrected status has in_reply_to_status_id but does not have in_reply_to_status.  Fallback into normal tweet:' , status ) ; } } } ) ; } function renderCorrectThisTweet ( props ) { const isMyTweet = props . owner && props . status . user . id === props . owner . id ; if ( ! isMyTweet ) { return undefined ; } return ( < div className = "tweet-actions__others-menu-item" onClick = { props . onCorrectTweet } > Correct this tweet < /div>); } function renderCopyJson ( props ) { if ( ! InDebugMode ) { return undefined ; } return ( < div className = "tweet-actions__others-menu-item" onClick = { props . onCopyJson } > Copy tweet JSON < /div>); } function doNotPropagateEvent ( e ) { e . stopPropagation ( ) ; } const OtherActionsButton = ( props ) => { const overlay = < div className = "tweet-actions__others-menu" > { renderDeleteThisTweet ( props ) } < div className = "tweet-actions__others-menu-item" onClick = { props . onUrlOpen } > Open URLs in tweet < /div> < div className = "tweet-actions__others-menu-item" onClick = { props . onStatusOpen } > Open tweet page < /div> < div className = "tweet-actions__others-menu-item" onClick = { props . onCopyUrl } > Copy tweet URL < /div> { renderCopyJson ( props ) } { renderCorrectThisTweet ( props ) } < /div>; return ( < div onClick = { doNotPropagateEvent } > < Tooltip placement = "bottom" trigger = { [ 'click' ] } overlay = { overlay } destroyTooltipOnHide > < div className = "tweet-actions__others" > < i className = "fa fa-ellipsis-h" / > < /div> < /Tooltip> < /div>); } ; function mapDispatch ( dispatch , props ) { return { onDeleteTweet : e => { e . stopPropagation ( ) ; TwitterRestApi . destroyStatus ( props . status . id ) . then ( ( ) => dispatch ( showMessage ( 'Deleted tweet.' , 'info' ) ) ) ; } , onUrlOpen : e => { e . stopPropagation ( ) ; props . status . openAllLinksInBrowser ( ) ; } , onStatusOpen : e => { e . stopPropagation ( ) ; props . status . openStatusPageInBrowser ( ) ; } , onCopyUrl : e => { e . stopPropagation ( ) ; statusUrlToClipboard ( props . status , dispatch ) ; } , onCopyJson : e => { e . stopPropagation ( ) ; copyJson ( props . status , dispatch ) ; } , onCorrectTweet : e => { e . stopPropagation ( ) ; if ( props . owner ) { correctThisTweet ( props . status , props . owner , dispatch ) ; } } , } ; } export default connect ( null , mapDispatch ) ( OtherActionsButton ) ;