2019-04-18 20:57:35 +00:00
|
|
|
import React from 'react';
|
2019-10-07 20:56:30 +00:00
|
|
|
import { shortnameToUnicode } from 'emoji-toolkit';
|
2019-04-18 20:57:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import styles from './styles';
|
2019-10-07 20:56:30 +00:00
|
|
|
import Markdown from '../../containers/markdown';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
const formatMsg = ({
|
|
|
|
lastMessage, type, showLastMessage, username
|
|
|
|
}) => {
|
|
|
|
if (!showLastMessage) {
|
|
|
|
return '';
|
|
|
|
}
|
2019-09-03 19:27:57 +00:00
|
|
|
if (!lastMessage || lastMessage.pinned) {
|
2019-04-18 20:57:35 +00:00
|
|
|
return I18n.t('No_Message');
|
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
if (lastMessage.t === 'jitsi_call_started') {
|
|
|
|
const { u } = lastMessage;
|
|
|
|
return I18n.t('Started_call', { userBy: u.username });
|
|
|
|
}
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
let prefix = '';
|
|
|
|
const isLastMessageSentByMe = lastMessage.u.username === username;
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
if (!lastMessage.msg && lastMessage.attachments && Object.keys(lastMessage.attachments).length) {
|
2019-04-18 20:57:35 +00:00
|
|
|
const user = isLastMessageSentByMe ? I18n.t('You') : lastMessage.u.username;
|
|
|
|
return I18n.t('User_sent_an_attachment', { user });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLastMessageSentByMe) {
|
|
|
|
prefix = I18n.t('You_colon');
|
|
|
|
} else if (type !== 'd') {
|
|
|
|
prefix = `${ lastMessage.u.username }: `;
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = `${ prefix }${ lastMessage.msg.replace(/[\n\t\r]/igm, '') }`;
|
|
|
|
if (msg) {
|
2019-10-07 20:56:30 +00:00
|
|
|
msg = shortnameToUnicode(msg);
|
2019-04-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
const arePropsEqual = (oldProps, newProps) => _.isEqual(oldProps, newProps);
|
|
|
|
|
|
|
|
const LastMessage = React.memo(({
|
2019-04-24 18:36:29 +00:00
|
|
|
lastMessage, type, showLastMessage, username, alert
|
2019-04-18 20:57:35 +00:00
|
|
|
}) => (
|
2019-10-07 20:56:30 +00:00
|
|
|
<Markdown
|
|
|
|
msg={formatMsg({
|
2019-04-18 20:57:35 +00:00
|
|
|
lastMessage, type, showLastMessage, username
|
|
|
|
})}
|
2019-10-07 20:56:30 +00:00
|
|
|
style={[styles.markdownText, alert && styles.markdownTextAlert]}
|
|
|
|
customEmojis={false}
|
|
|
|
numberOfLines={2}
|
|
|
|
preview
|
|
|
|
/>
|
2019-04-18 20:57:35 +00:00
|
|
|
), arePropsEqual);
|
|
|
|
|
|
|
|
LastMessage.propTypes = {
|
|
|
|
lastMessage: PropTypes.object,
|
|
|
|
type: PropTypes.string,
|
|
|
|
showLastMessage: PropTypes.bool,
|
2019-04-24 18:36:29 +00:00
|
|
|
username: PropTypes.string,
|
|
|
|
alert: PropTypes.bool
|
2019-04-18 20:57:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default LastMessage;
|