jiti-meet/react/features/chat/components/native/ChatMessage.js

216 lines
6.5 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { Text, View } from 'react-native';
2019-06-26 14:08:23 +00:00
import { Avatar } from '../../../base/avatar';
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';
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';
import AbstractChatMessage, { type Props } from '../AbstractChatMessage';
2019-08-23 14:41:10 +00:00
import GifMessage from './GifMessage';
import PrivateMessageButton from './PrivateMessageButton';
import styles from './styles';
2019-10-15 14:08:23 +00:00
/**
* Renders a single chat message.
*/
class ChatMessage extends AbstractChatMessage<Props> {
/**
* Implements {@code Component#render}.
*
* @inheritdoc
*/
render() {
const { message, knocking } = this.props;
2019-10-15 14:08:23 +00:00
const localMessage = message.messageType === MESSAGE_TYPE_LOCAL;
const { privateMessage, lobbyChat } = message;
// 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
];
if (localMessage) {
2019-10-15 14:08:23 +00:00
// This is a message sent by the local participant.
// 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
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
messageBubbleStyle.push(styles.remoteMessageBubble);
2019-10-15 14:08:23 +00:00
}
if (privateMessage) {
messageBubbleStyle.push(styles.privateMessageBubble);
}
if (lobbyChat && !knocking) {
messageBubbleStyle.push(styles.lobbyMessageBubble);
}
const messageText = replaceNonUnicodeEmojis(this._getMessageText());
return (
<View style = { styles.messageWrapper } >
{ this._renderAvatar() }
<View style = { detailsWrapperStyle }>
2019-10-15 14:08:23 +00:00
<View style = { messageBubbleStyle }>
<View style = { styles.textWrapper } >
{ this._renderDisplayName() }
{ isGifMessage(messageText)
? <GifMessage message = { messageText } />
: (
<Linkify
linkStyle = { styles.chatLink }
style = { styles.chatMessage }>
{ messageText }
</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() }
</View>
2019-10-15 14:08:23 +00:00
{ this._renderTimestamp() }
</View>
</View>
);
}
_getFormattedTimestamp: () => string;
2019-10-09 08:34:01 +00:00
_getMessageText: () => string;
2019-10-07 12:35:04 +00:00
_getPrivateNoticeMessage: () => string;
/**
* Renders the avatar of the sender.
*
* @returns {React$Element<*>}
*/
_renderAvatar() {
const { message } = this.props;
return (
<View style = { styles.avatarWrapper }>
{ this.props.showAvatar && <Avatar
displayName = { message.displayName }
participantId = { message.id }
2019-06-26 14:08:23 +00:00
size = { styles.avatarWrapper.width } />
}
</View>
);
}
/**
2019-10-15 14:08:23 +00:00
* Renders the display name of the sender if necessary.
*
2019-10-15 14:08:23 +00:00
* @returns {React$Element<*> | null}
*/
_renderDisplayName() {
const { message, showDisplayName } = this.props;
2019-10-15 14:08:23 +00:00
if (!showDisplayName) {
return null;
}
return (
<Text style = { styles.senderDisplayName }>
2019-10-15 14:08:23 +00:00
{ message.displayName }
</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() {
const { message, knocking } = this.props;
2019-10-15 14:08:23 +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 (
<Text style = { message.lobbyChat ? styles.lobbyMsgNotice : styles.privateNotice }>
2019-10-07 12:35:04 +00:00
{ this._getPrivateNoticeMessage() }
</Text>
);
}
/**
2019-10-15 14:08:23 +00:00
* Renders the private reply button, if necessary.
*
2019-10-15 14:08:23 +00:00
* @returns {React$Element<*> | null}
*/
_renderPrivateReplyButton() {
const { message, knocking } = this.props;
const { messageType, privateMessage, lobbyChat } = message;
2019-10-15 14:08:23 +00:00
if (!(privateMessage || lobbyChat) || messageType === MESSAGE_TYPE_LOCAL || knocking) {
2019-10-15 14:08:23 +00:00
return null;
}
return (
<View style = { styles.replyContainer }>
2019-10-15 14:08:23 +00:00
<PrivateMessageButton
isLobbyMessage = { lobbyChat }
2019-10-15 14:08:23 +00:00
participantID = { message.id }
reply = { true }
showLabel = { false }
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}
*/
_renderTimestamp() {
2019-10-15 14:08:23 +00:00
if (!this.props.showTimestamp) {
return null;
}
return (
<Text style = { styles.timeText }>
{ this._getFormattedTimestamp() }
</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 {
knocking: state['features/lobby'].knocking
2019-10-15 14:08:23 +00:00
};
}
export default translate(connect(_mapStateToProps)(ChatMessage));