2018-08-29 17:24:25 +00:00
|
|
|
// @flow
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
import React from 'react';
|
2018-12-03 18:01:40 +00:00
|
|
|
import { toArray } from 'react-emoji-render';
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
import { translate } from '../../../base/i18n';
|
2019-08-05 10:46:10 +00:00
|
|
|
import { Linkify } from '../../../base/react';
|
2018-08-29 17:24:25 +00:00
|
|
|
|
2019-01-13 19:34:38 +00:00
|
|
|
import AbstractChatMessage, {
|
|
|
|
type Props
|
|
|
|
} from '../AbstractChatMessage';
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-13 19:34:38 +00:00
|
|
|
* Renders a single chat message.
|
2018-08-29 17:24:25 +00:00
|
|
|
*/
|
2019-01-13 19:34:38 +00:00
|
|
|
class ChatMessage extends AbstractChatMessage<Props> {
|
2018-08-29 17:24:25 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const { message } = this.props;
|
2019-05-03 19:41:09 +00:00
|
|
|
const messageToDisplay = message.messageType === 'error'
|
|
|
|
? this.props.t('chat.error', {
|
2018-08-29 17:24:25 +00:00
|
|
|
error: message.error,
|
2019-05-03 19:41:09 +00:00
|
|
|
originalText: message.message
|
|
|
|
})
|
|
|
|
: message.message;
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
// replace links and smileys
|
|
|
|
// Strophe already escapes special symbols on sending,
|
|
|
|
// so we escape here only tags to avoid double &
|
2018-12-03 18:01:40 +00:00
|
|
|
const escMessage = messageToDisplay.replace(/</g, '<')
|
2018-08-29 17:24:25 +00:00
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/\n/g, '<br/>');
|
2018-12-03 18:01:40 +00:00
|
|
|
const processedMessage = [];
|
|
|
|
|
|
|
|
// content is an array of text and emoji components
|
|
|
|
const content = toArray(escMessage, { className: 'smiley' });
|
|
|
|
|
|
|
|
content.forEach(i => {
|
|
|
|
if (typeof i === 'string') {
|
2019-08-05 10:46:10 +00:00
|
|
|
processedMessage.push(<Linkify key = { i }>{ i }</Linkify>);
|
2018-12-03 18:01:40 +00:00
|
|
|
} else {
|
|
|
|
processedMessage.push(i);
|
|
|
|
}
|
|
|
|
});
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
return (
|
2019-05-09 15:43:11 +00:00
|
|
|
<div className = 'chatmessage-wrapper'>
|
2019-05-06 19:30:38 +00:00
|
|
|
<div className = 'chatmessage'>
|
|
|
|
{ this.props.showDisplayName && this._renderDisplayName() }
|
|
|
|
<div className = 'usermessage'>
|
|
|
|
{ processedMessage }
|
|
|
|
</div>
|
2018-12-03 18:01:40 +00:00
|
|
|
</div>
|
2019-05-06 19:30:38 +00:00
|
|
|
{ this.props.showTimestamp && this._renderTimestamp() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getFormattedTimestamp: () => string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the display name of the sender.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderDisplayName() {
|
|
|
|
return (
|
|
|
|
<div className = 'display-name'>
|
|
|
|
{ this.props.message.displayName }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the time at which the message was sent.
|
|
|
|
*
|
|
|
|
* @returns {React$Element<*>}
|
|
|
|
*/
|
|
|
|
_renderTimestamp() {
|
|
|
|
return (
|
|
|
|
<div className = 'timestamp'>
|
|
|
|
{ this._getFormattedTimestamp() }
|
2018-08-29 17:24:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 10:10:25 +00:00
|
|
|
export default translate(ChatMessage);
|