2017-11-21 14:55:50 +00:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
2017-11-24 20:44:52 +00:00
import { View , StyleSheet , TouchableOpacity , Text } from 'react-native' ;
2017-11-21 14:55:50 +00:00
import { connect } from 'react-redux' ;
2017-12-11 20:37:33 +00:00
import moment from 'moment' ;
2017-11-21 14:55:50 +00:00
2017-11-24 20:44:52 +00:00
import { actionsShow } from '../../actions/messages' ;
2017-12-02 13:19:58 +00:00
import Image from './Image' ;
2017-11-21 14:55:50 +00:00
import User from './User' ;
import Avatar from '../Avatar' ;
2017-12-02 13:19:58 +00:00
import Audio from './Audio' ;
import Video from './Video' ;
import Markdown from './Markdown' ;
import Url from './Url' ;
import Reply from './Reply' ;
2017-11-21 14:55:50 +00:00
const styles = StyleSheet . create ( {
content : {
flexGrow : 1 ,
flexShrink : 1
} ,
message : {
padding : 12 ,
paddingTop : 6 ,
paddingBottom : 6 ,
flexDirection : 'row' ,
transform : [ { scaleY : - 1 } ]
} ,
textInfo : {
fontStyle : 'italic' ,
color : '#a0a0a0'
} ,
editing : {
backgroundColor : '#fff5df'
}
} ) ;
@ connect ( state => ( {
message : state . messages . message ,
2017-11-24 20:44:52 +00:00
editing : state . messages . editing
2017-11-21 14:55:50 +00:00
} ) , dispatch => ( {
2017-11-24 20:44:52 +00:00
actionsShow : actionMessage => dispatch ( actionsShow ( actionMessage ) )
2017-11-21 14:55:50 +00:00
} ) )
export default class Message extends React . Component {
static propTypes = {
item : PropTypes . object . isRequired ,
baseUrl : PropTypes . string . isRequired ,
Message _TimeFormat : PropTypes . string . isRequired ,
2017-11-24 20:44:52 +00:00
message : PropTypes . object . isRequired ,
2017-12-02 13:19:58 +00:00
user : PropTypes . object . isRequired ,
2017-11-24 20:44:52 +00:00
editing : PropTypes . bool ,
actionsShow : PropTypes . func
2017-11-21 14:55:50 +00:00
}
2017-11-24 20:44:52 +00:00
onLongPress ( ) {
const { item } = this . props ;
this . props . actionsShow ( JSON . parse ( JSON . stringify ( item ) ) ) ;
2017-11-21 14:55:50 +00:00
}
isDeleted ( ) {
2017-12-02 13:19:58 +00:00
return this . props . item . t === 'rm' ;
}
isPinned ( ) {
return this . props . item . t === 'message_pinned' ;
2017-11-21 14:55:50 +00:00
}
attachments ( ) {
2017-12-02 13:19:58 +00:00
if ( this . props . item . attachments . length === 0 ) {
return null ;
}
const file = this . props . item . attachments [ 0 ] ;
const { baseUrl , user } = this . props ;
if ( file . image _type ) {
return < Image file = { file } baseUrl = { baseUrl } user = { user } / > ;
} else if ( file . audio _type ) {
return < Audio file = { file } baseUrl = { baseUrl } user = { user } / > ;
} else if ( file . video _type ) {
return < Video file = { file } baseUrl = { baseUrl } user = { user } / > ;
}
return < Reply attachment = { file } timeFormat = { this . props . Message _TimeFormat } / > ;
2017-11-21 14:55:50 +00:00
}
renderMessageContent ( ) {
if ( this . isDeleted ( ) ) {
return < Text style = { styles . textInfo } > Message removed < / T e x t > ;
2017-12-02 13:19:58 +00:00
} else if ( this . isPinned ( ) ) {
return < Text style = { styles . textInfo } > Message pinned < / T e x t > ;
2017-11-21 14:55:50 +00:00
}
2017-12-02 13:19:58 +00:00
return < Markdown msg = { this . props . item . msg } / > ;
}
2017-11-21 14:55:50 +00:00
2017-12-02 13:19:58 +00:00
renderUrl ( ) {
if ( this . props . item . urls . length === 0 ) {
return null ;
}
return this . props . item . urls . map ( url => (
< Url url = { url } key = { url . _id } / >
) ) ;
2017-11-21 14:55:50 +00:00
}
render ( ) {
2017-11-24 20:44:52 +00:00
const {
item , message , editing
} = this . props ;
2017-11-21 14:55:50 +00:00
const extraStyle = { } ;
if ( item . temp ) {
extraStyle . opacity = 0.3 ;
}
const username = item . alias || item . u . username ;
2017-11-24 20:44:52 +00:00
const isEditing = message . _id === item . _id && editing ;
2017-11-21 14:55:50 +00:00
2017-12-11 20:37:33 +00:00
const accessibilityLabel = ` Message from ${ item . alias || item . u . username } at ${ moment ( item . ts ) . format ( this . props . Message _TimeFormat ) } , ${ this . props . item . msg } ` ;
2017-11-21 14:55:50 +00:00
return (
< TouchableOpacity
2017-11-24 20:44:52 +00:00
onLongPress = { ( ) => this . onLongPress ( ) }
2017-11-21 14:55:50 +00:00
disabled = { this . isDeleted ( ) }
2017-11-24 20:44:52 +00:00
style = { [ styles . message , extraStyle , isEditing ? styles . editing : null ] }
2017-12-11 20:37:33 +00:00
accessibilityLabel = { accessibilityLabel }
2017-11-21 14:55:50 +00:00
>
2017-11-24 20:44:52 +00:00
< Avatar
style = { { marginRight : 10 } }
text = { item . avatar ? '' : username }
size = { 40 }
baseUrl = { this . props . baseUrl }
avatar = { item . avatar }
/ >
< View style = { [ styles . content ] } >
< User
onPress = { this . _onPress }
item = { item }
Message _TimeFormat = { this . props . Message _TimeFormat }
2017-11-21 14:55:50 +00:00
baseUrl = { this . props . baseUrl }
/ >
2017-12-02 13:19:58 +00:00
{ this . renderMessageContent ( ) }
2017-11-24 20:44:52 +00:00
{ this . attachments ( ) }
2017-12-02 13:19:58 +00:00
{ this . renderUrl ( ) }
2017-11-21 14:55:50 +00:00
< / V i e w >
< / T o u c h a b l e O p a c i t y >
) ;
}
}