2019-01-13 19:34:38 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { Text, View } from 'react-native';
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
import { Avatar } from '../../../base/avatar';
|
2019-05-06 19:30:38 +00:00
|
|
|
import { translate } from '../../../base/i18n';
|
2019-01-13 19:34:38 +00:00
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
import AbstractChatMessage, { type Props } from '../AbstractChatMessage';
|
2019-01-13 19:34:38 +00:00
|
|
|
import styles from './styles';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a single chat message.
|
|
|
|
*/
|
|
|
|
class ChatMessage extends AbstractChatMessage<Props> {
|
|
|
|
/**
|
|
|
|
* Implements {@code Component#render}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const { message } = this.props;
|
|
|
|
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 } >
|
2019-05-06 18:21:41 +00:00
|
|
|
{ this._renderAvatar() }
|
2019-01-13 19:34:38 +00:00
|
|
|
<View style = { detailsWrapperStyle }>
|
|
|
|
<View style = { textWrapperStyle } >
|
|
|
|
{
|
2019-05-06 18:21:41 +00:00
|
|
|
this.props.showDisplayName
|
|
|
|
&& this._renderDisplayName()
|
2019-01-13 19:34:38 +00:00
|
|
|
}
|
|
|
|
<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-05-06 18:21:41 +00:00
|
|
|
{ this.props.showTimestamp && this._renderTimestamp() }
|
2019-01-13 19:34:38 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:30:38 +00:00
|
|
|
_getFormattedTimestamp: () => string;
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
/**
|
|
|
|
* Renders the avatar of the sender.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderAvatar() {
|
|
|
|
return (
|
|
|
|
<View style = { styles.avatarWrapper }>
|
2019-05-06 18:21:41 +00:00
|
|
|
{ this.props.showAvatar && <Avatar
|
2019-06-26 14:08:23 +00:00
|
|
|
participantId = { this.props.message.id }
|
|
|
|
size = { styles.avatarWrapper.width } />
|
2019-05-06 18:21:41 +00:00
|
|
|
}
|
2019-01-13 19:34:38 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the display name of the sender.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderDisplayName() {
|
|
|
|
return (
|
|
|
|
<Text style = { styles.displayName }>
|
2019-05-06 18:21:41 +00:00
|
|
|
{ this.props.message.displayName }
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the time at which the message was sent.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderTimestamp() {
|
|
|
|
return (
|
|
|
|
<Text style = { styles.timeText }>
|
2019-05-06 19:30:38 +00:00
|
|
|
{ this._getFormattedTimestamp() }
|
2019-01-13 19:34:38 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
export default translate(ChatMessage);
|