2019-01-13 19:34:38 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { Text, View } from 'react-native';
|
|
|
|
|
|
|
|
import { getLocalizedDateFormatter, translate } from '../../../base/i18n';
|
|
|
|
import { Avatar } from '../../../base/participants';
|
2019-03-21 16:38:29 +00:00
|
|
|
import { connect } from '../../../base/redux';
|
2019-01-13 19:34:38 +00:00
|
|
|
|
|
|
|
import AbstractChatMessage, {
|
2019-03-20 21:23:19 +00:00
|
|
|
_mapStateToProps,
|
|
|
|
type Props
|
2019-01-13 19:34:38 +00:00
|
|
|
} from '../AbstractChatMessage';
|
|
|
|
import styles from './styles';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the rendered avatar in the message.
|
|
|
|
*/
|
|
|
|
const AVATAR_SIZE = 32;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formatter string to display the message timestamp.
|
|
|
|
*/
|
|
|
|
const TIMESTAMP_FORMAT = 'H:mm';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a single chat message.
|
|
|
|
*/
|
|
|
|
class ChatMessage extends AbstractChatMessage<Props> {
|
|
|
|
/**
|
|
|
|
* Implements {@code Component#render}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const { message } = this.props;
|
|
|
|
const timeStamp = getLocalizedDateFormatter(
|
2019-04-25 13:17:49 +00:00
|
|
|
new Date(message.timestamp)).format(TIMESTAMP_FORMAT);
|
2019-01-13 19:34:38 +00:00
|
|
|
const localMessage = message.messageType === 'local';
|
|
|
|
|
|
|
|
// Style arrays that need to be updated in various scenarios, such as
|
|
|
|
// error messages or others.
|
|
|
|
const detailsWrapperStyle = [
|
|
|
|
styles.detailsWrapper
|
|
|
|
];
|
|
|
|
const textWrapperStyle = [
|
|
|
|
styles.textWrapper
|
|
|
|
];
|
|
|
|
|
|
|
|
if (localMessage) {
|
|
|
|
// The wrapper needs to be aligned to the right.
|
|
|
|
detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
|
|
|
|
|
|
|
|
// The bubble needs to be differently styled.
|
|
|
|
textWrapperStyle.push(styles.ownTextWrapper);
|
2019-04-25 13:17:49 +00:00
|
|
|
} else if (message.messageType === 'error') {
|
2019-01-13 19:34:38 +00:00
|
|
|
// The bubble needs to be differently styled.
|
|
|
|
textWrapperStyle.push(styles.systemTextWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style = { styles.messageWrapper } >
|
|
|
|
{
|
|
|
|
|
|
|
|
// Avatar is only rendered for remote messages.
|
|
|
|
!localMessage && this._renderAvatar()
|
|
|
|
}
|
|
|
|
<View style = { detailsWrapperStyle }>
|
|
|
|
<View style = { textWrapperStyle } >
|
|
|
|
{
|
|
|
|
|
|
|
|
// Display name is only rendered for remote
|
|
|
|
// messages.
|
|
|
|
!localMessage && this._renderDisplayName()
|
|
|
|
}
|
|
|
|
<Text style = { styles.messageText }>
|
2019-04-25 13:17:49 +00:00
|
|
|
{ message.messageType === 'error'
|
|
|
|
? this.props.t('chat.error', {
|
|
|
|
error: message.error,
|
|
|
|
originalText: message.message
|
|
|
|
})
|
|
|
|
: message.message }
|
2019-01-13 19:34:38 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
2019-03-20 21:23:19 +00:00
|
|
|
<Text style = { styles.timeText }>
|
2019-01-13 19:34:38 +00:00
|
|
|
{ timeStamp }
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the avatar of the sender.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderAvatar() {
|
|
|
|
const { _avatarURL } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style = { styles.avatarWrapper }>
|
|
|
|
<Avatar
|
|
|
|
size = { AVATAR_SIZE }
|
|
|
|
uri = { _avatarURL } />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the display name of the sender.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderDisplayName() {
|
|
|
|
const { message } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Text style = { styles.displayName }>
|
2019-04-25 13:17:49 +00:00
|
|
|
{ message.displayName }
|
2019-01-13 19:34:38 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(connect(_mapStateToProps)(ChatMessage));
|