2019-01-13 19:34:38 +00:00
|
|
|
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-08-05 10:46:10 +00:00
|
|
|
import { Linkify } from '../../../base/react';
|
2019-10-15 14:08:23 +00:00
|
|
|
import { connect } from '../../../base/redux';
|
2022-03-30 13:54:03 +00:00
|
|
|
import { isGifMessage } from '../../../gifs/functions';
|
2019-10-15 14:08:23 +00:00
|
|
|
import { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../../constants';
|
2019-08-23 14:41:10 +00:00
|
|
|
import { replaceNonUnicodeEmojis } from '../../functions';
|
2022-11-18 12:46:54 +00:00
|
|
|
import AbstractChatMessage, { type Props } from '../AbstractChatMessage';
|
2019-08-23 14:41:10 +00:00
|
|
|
|
2022-03-30 13:54:03 +00:00
|
|
|
import GifMessage from './GifMessage';
|
2021-11-11 12:32:01 +00:00
|
|
|
import PrivateMessageButton from './PrivateMessageButton';
|
2019-01-13 19:34:38 +00:00
|
|
|
import styles from './styles';
|
|
|
|
|
2019-10-15 14:08:23 +00:00
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
/**
|
|
|
|
* Renders a single chat message.
|
|
|
|
*/
|
|
|
|
class ChatMessage extends AbstractChatMessage<Props> {
|
|
|
|
/**
|
|
|
|
* Implements {@code Component#render}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
2022-11-18 12:46:54 +00:00
|
|
|
const { message, knocking } = this.props;
|
2019-10-15 14:08:23 +00:00
|
|
|
const localMessage = message.messageType === MESSAGE_TYPE_LOCAL;
|
2022-03-03 17:29:38 +00:00
|
|
|
const { privateMessage, lobbyChat } = message;
|
2019-01-13 19:34:38 +00:00
|
|
|
|
|
|
|
// Style arrays that need to be updated in various scenarios, such as
|
|
|
|
// error messages or others.
|
|
|
|
const detailsWrapperStyle = [
|
|
|
|
styles.detailsWrapper
|
|
|
|
];
|
2019-10-15 14:08:23 +00:00
|
|
|
const messageBubbleStyle = [
|
|
|
|
styles.messageBubble
|
2019-01-13 19:34:38 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (localMessage) {
|
2019-10-15 14:08:23 +00:00
|
|
|
// This is a message sent by the local participant.
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
// The wrapper needs to be aligned to the right.
|
|
|
|
detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
|
|
|
|
|
2019-10-15 14:08:23 +00:00
|
|
|
// The bubble needs some additional styling
|
2022-11-18 12:46:54 +00:00
|
|
|
messageBubbleStyle.push(styles.localMessageBubble);
|
2019-10-15 14:08:23 +00:00
|
|
|
} else if (message.messageType === MESSAGE_TYPE_ERROR) {
|
|
|
|
// This is a system message.
|
|
|
|
|
|
|
|
// The bubble needs some additional styling
|
|
|
|
messageBubbleStyle.push(styles.systemMessageBubble);
|
|
|
|
} else {
|
|
|
|
// This is a remote message sent by a remote participant.
|
|
|
|
|
|
|
|
// The bubble needs some additional styling
|
2022-11-18 12:46:54 +00:00
|
|
|
messageBubbleStyle.push(styles.remoteMessageBubble);
|
2019-10-15 14:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (privateMessage) {
|
2022-11-18 12:46:54 +00:00
|
|
|
messageBubbleStyle.push(styles.privateMessageBubble);
|
2019-01-13 19:34:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 17:29:38 +00:00
|
|
|
if (lobbyChat && !knocking) {
|
2022-11-18 12:46:54 +00:00
|
|
|
messageBubbleStyle.push(styles.lobbyMessageBubble);
|
2022-03-03 17:29:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 13:54:03 +00:00
|
|
|
const messageText = replaceNonUnicodeEmojis(this._getMessageText());
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
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 }>
|
2019-10-15 14:08:23 +00:00
|
|
|
<View style = { messageBubbleStyle }>
|
|
|
|
<View style = { styles.textWrapper } >
|
|
|
|
{ this._renderDisplayName() }
|
2022-11-18 12:46:54 +00:00
|
|
|
{ isGifMessage(messageText)
|
2022-03-30 13:54:03 +00:00
|
|
|
? <GifMessage message = { messageText } />
|
|
|
|
: (
|
2022-11-18 12:46:54 +00:00
|
|
|
<Linkify
|
|
|
|
linkStyle = { styles.chatLink }
|
|
|
|
style = { styles.chatMessage }>
|
|
|
|
{ messageText }
|
2022-03-30 13:54:03 +00:00
|
|
|
</Linkify>
|
|
|
|
)}
|
2019-10-15 14:08:23 +00:00
|
|
|
{ this._renderPrivateNotice() }
|
2019-10-07 12:35:04 +00:00
|
|
|
</View>
|
2019-10-15 14:08:23 +00:00
|
|
|
{ this._renderPrivateReplyButton() }
|
2019-01-13 19:34:38 +00:00
|
|
|
</View>
|
2019-10-15 14:08:23 +00:00
|
|
|
{ this._renderTimestamp() }
|
2019-01-13 19:34:38 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:30:38 +00:00
|
|
|
_getFormattedTimestamp: () => string;
|
|
|
|
|
2019-10-09 08:34:01 +00:00
|
|
|
_getMessageText: () => string;
|
|
|
|
|
2019-10-07 12:35:04 +00:00
|
|
|
_getPrivateNoticeMessage: () => string;
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
/**
|
|
|
|
* Renders the avatar of the sender.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderAvatar() {
|
2019-07-31 07:54:32 +00:00
|
|
|
const { message } = this.props;
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
return (
|
|
|
|
<View style = { styles.avatarWrapper }>
|
2019-05-06 18:21:41 +00:00
|
|
|
{ this.props.showAvatar && <Avatar
|
2019-07-31 07:54:32 +00:00
|
|
|
displayName = { message.displayName }
|
|
|
|
participantId = { message.id }
|
2019-06-26 14:08:23 +00:00
|
|
|
size = { styles.avatarWrapper.width } />
|
2019-05-06 18:21:41 +00:00
|
|
|
}
|
2019-01-13 19:34:38 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-15 14:08:23 +00:00
|
|
|
* Renders the display name of the sender if necessary.
|
2019-01-13 19:34:38 +00:00
|
|
|
*
|
2019-10-15 14:08:23 +00:00
|
|
|
* @returns {React$Element<*> | null}
|
2019-01-13 19:34:38 +00:00
|
|
|
*/
|
|
|
|
_renderDisplayName() {
|
2022-11-18 12:46:54 +00:00
|
|
|
const { message, showDisplayName } = this.props;
|
2019-10-15 14:08:23 +00:00
|
|
|
|
|
|
|
if (!showDisplayName) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
return (
|
2022-11-18 12:46:54 +00:00
|
|
|
<Text style = { styles.senderDisplayName }>
|
2019-10-15 14:08:23 +00:00
|
|
|
{ message.displayName }
|
2019-05-06 18:21:41 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:35:04 +00:00
|
|
|
/**
|
2019-10-15 14:08:23 +00:00
|
|
|
* Renders the message privacy notice, if necessary.
|
2019-10-07 12:35:04 +00:00
|
|
|
*
|
2019-10-15 14:08:23 +00:00
|
|
|
* @returns {React$Element<*> | null}
|
2019-10-07 12:35:04 +00:00
|
|
|
*/
|
|
|
|
_renderPrivateNotice() {
|
2022-11-18 12:46:54 +00:00
|
|
|
const { message, knocking } = this.props;
|
2019-10-15 14:08:23 +00:00
|
|
|
|
2022-03-03 17:29:38 +00:00
|
|
|
if (!(message.privateMessage || (message.lobbyChat && !knocking))) {
|
2019-10-15 14:08:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:35:04 +00:00
|
|
|
return (
|
2022-11-18 12:46:54 +00:00
|
|
|
<Text style = { message.lobbyChat ? styles.lobbyMsgNotice : styles.privateNotice }>
|
2019-10-07 12:35:04 +00:00
|
|
|
{ this._getPrivateNoticeMessage() }
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-06 18:21:41 +00:00
|
|
|
/**
|
2019-10-15 14:08:23 +00:00
|
|
|
* Renders the private reply button, if necessary.
|
2019-05-06 18:21:41 +00:00
|
|
|
*
|
2019-10-15 14:08:23 +00:00
|
|
|
* @returns {React$Element<*> | null}
|
|
|
|
*/
|
|
|
|
_renderPrivateReplyButton() {
|
2022-11-18 12:46:54 +00:00
|
|
|
const { message, knocking } = this.props;
|
2022-03-03 17:29:38 +00:00
|
|
|
const { messageType, privateMessage, lobbyChat } = message;
|
2019-10-15 14:08:23 +00:00
|
|
|
|
2022-03-03 17:29:38 +00:00
|
|
|
if (!(privateMessage || lobbyChat) || messageType === MESSAGE_TYPE_LOCAL || knocking) {
|
2019-10-15 14:08:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-11-18 12:46:54 +00:00
|
|
|
<View style = { styles.replyContainer }>
|
2019-10-15 14:08:23 +00:00
|
|
|
<PrivateMessageButton
|
2022-03-03 17:29:38 +00:00
|
|
|
isLobbyMessage = { lobbyChat }
|
2019-10-15 14:08:23 +00:00
|
|
|
participantID = { message.id }
|
|
|
|
reply = { true }
|
|
|
|
showLabel = { false }
|
2022-11-18 12:46:54 +00:00
|
|
|
toggledStyles = { styles.replyStyles } />
|
2019-10-15 14:08:23 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the time at which the message was sent, if necessary.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*> | null}
|
2019-05-06 18:21:41 +00:00
|
|
|
*/
|
|
|
|
_renderTimestamp() {
|
2019-10-15 14:08:23 +00:00
|
|
|
if (!this.props.showTimestamp) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-06 18:21:41 +00:00
|
|
|
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-10-15 14:08:23 +00:00
|
|
|
/**
|
|
|
|
* Maps part of the redux state to the props of this component.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @returns {Props}
|
|
|
|
*/
|
|
|
|
function _mapStateToProps(state) {
|
|
|
|
return {
|
2022-03-03 17:29:38 +00:00
|
|
|
knocking: state['features/lobby'].knocking
|
2019-10-15 14:08:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(connect(_mapStateToProps)(ChatMessage));
|