verdnatura-chat/app/components/Message.js

120 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-08-09 01:40:55 +00:00
import React from 'react';
import PropTypes from 'prop-types';
2017-08-09 19:02:09 +00:00
import { View, Text, StyleSheet } from 'react-native';
import { CachedImage } from 'react-native-img-cache';
2017-08-10 14:27:28 +00:00
import { emojify } from 'react-emojione';
import Markdown from 'react-native-easy-markdown';
import moment from 'moment';
2017-08-13 03:19:14 +00:00
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
2017-08-09 01:40:55 +00:00
const styles = StyleSheet.create({
message: {
2017-08-10 15:16:47 +00:00
padding: 12,
paddingTop: 6,
paddingBottom: 6,
2017-08-09 01:40:55 +00:00
flexDirection: 'row',
transform: [{ scaleY: -1 }]
2017-08-09 02:20:34 +00:00
},
2017-08-09 19:02:09 +00:00
avatarContainer: {
2017-08-10 15:16:47 +00:00
backgroundColor: '#eee',
2017-08-09 02:20:34 +00:00
width: 40,
height: 40,
marginRight: 10,
borderRadius: 5
},
2017-08-09 19:02:09 +00:00
avatar: {
width: 40,
height: 40,
2017-08-10 15:26:14 +00:00
borderRadius: 5,
position: 'absolute'
},
avatarInitials: {
margin: 2,
textAlign: 'center',
lineHeight: 36,
fontSize: 22,
color: '#ffffff'
2017-08-09 19:02:09 +00:00
},
2017-08-09 02:20:34 +00:00
texts: {
flex: 1
},
msg: {
flex: 1
},
username: {
fontWeight: 'bold'
},
usernameView: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 2
},
alias: {
fontSize: 10,
color: '#888',
paddingLeft: 5
},
time: {
fontSize: 10,
color: '#888',
paddingLeft: 5
2017-08-09 01:40:55 +00:00
}
});
export default class Message extends React.PureComponent {
static propTypes = {
item: PropTypes.object.isRequired,
baseUrl: PropTypes.string.isRequired,
Message_TimeFormat: PropTypes.string.isRequired
2017-08-09 01:40:55 +00:00
}
render() {
const { item } = this.props;
2017-08-09 01:40:55 +00:00
const extraStyle = {};
if (item.temp) {
2017-08-09 01:40:55 +00:00
extraStyle.opacity = 0.3;
}
const msg = emojify(item.msg, { output: 'unicode' });
2017-08-10 14:27:28 +00:00
const username = item.alias || item.u.username;
2017-08-13 23:45:47 +00:00
let { initials, color } = avatarInitialsAndColor(username);
const avatar = item.avatar || `${ this.props.baseUrl }/avatar/${ item.u.username }`;
if (item.avatar) {
2017-08-13 23:45:47 +00:00
initials = '';
color = 'transparent';
}
2017-08-10 15:26:14 +00:00
let aliasUsername;
if (item.alias) {
aliasUsername = <Text style={styles.alias}>@{item.u.username}</Text>;
}
const time = moment(item.ts).format(this.props.Message_TimeFormat);
2017-08-09 01:40:55 +00:00
return (
<View style={[styles.message, extraStyle]}>
2017-08-10 15:26:14 +00:00
<View style={[styles.avatarContainer, { backgroundColor: color }]}>
<Text style={styles.avatarInitials}>{initials}</Text>
2017-08-13 23:45:47 +00:00
<CachedImage style={styles.avatar} source={{ uri: avatar }} />
2017-08-09 19:02:09 +00:00
</View>
2017-08-09 01:40:55 +00:00
<View style={styles.texts}>
<View style={styles.usernameView}>
<Text onPress={this._onPress} style={styles.username}>
{username}
</Text>
{aliasUsername}<Text style={styles.time}>{time}</Text>
</View>
2017-08-10 14:27:28 +00:00
<Markdown>
{msg}
</Markdown>
2017-08-09 01:40:55 +00:00
</View>
</View>
);
}
}