2019-04-18 20:57:35 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-02-01 17:18:55 +00:00
|
|
|
import isEqual from 'lodash/isEqual';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import styles from './styles';
|
2019-10-02 12:41:51 +00:00
|
|
|
import Markdown from '../../containers/markdown';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../../lib/encryption/constants';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
const formatMsg = ({
|
2020-02-21 16:13:05 +00:00
|
|
|
lastMessage, type, showLastMessage, username, useRealName
|
2019-04-18 20:57:35 +00:00
|
|
|
}) => {
|
|
|
|
if (!showLastMessage) {
|
|
|
|
return '';
|
|
|
|
}
|
2020-01-16 16:05:24 +00:00
|
|
|
if (!lastMessage || !lastMessage.u || lastMessage.pinned) {
|
2019-04-18 20:57:35 +00:00
|
|
|
return I18n.t('No_Message');
|
|
|
|
}
|
2019-09-18 17:32:12 +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-09-16 20:26:32 +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 });
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypted message pending decrypt
|
|
|
|
if (lastMessage.t === E2E_MESSAGE_TYPE && lastMessage.e2e !== E2E_STATUS.DONE) {
|
|
|
|
lastMessage.msg = I18n.t('Encrypted_message');
|
2019-04-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isLastMessageSentByMe) {
|
|
|
|
prefix = I18n.t('You_colon');
|
|
|
|
} else if (type !== 'd') {
|
2020-02-21 16:13:05 +00:00
|
|
|
const { u: { name } } = lastMessage;
|
|
|
|
prefix = `${ useRealName ? name : lastMessage.u.username }: `;
|
2019-04-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 16:18:03 +00:00
|
|
|
return `${ prefix }${ lastMessage.msg }`;
|
2019-04-18 20:57:35 +00:00
|
|
|
};
|
|
|
|
|
2021-02-01 17:18:55 +00:00
|
|
|
const arePropsEqual = (oldProps, newProps) => isEqual(oldProps, newProps);
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
const LastMessage = React.memo(({
|
2020-02-21 16:13:05 +00:00
|
|
|
lastMessage, type, showLastMessage, username, alert, useRealName, theme
|
2019-04-18 20:57:35 +00:00
|
|
|
}) => (
|
2019-10-02 12:41:51 +00:00
|
|
|
<Markdown
|
|
|
|
msg={formatMsg({
|
2020-02-21 16:13:05 +00:00
|
|
|
lastMessage, type, showLastMessage, username, useRealName
|
2019-04-18 20:57:35 +00:00
|
|
|
})}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.markdownText, { color: alert ? themes[theme].bodyText : themes[theme].auxiliaryText }]}
|
2019-10-02 12:41:51 +00:00
|
|
|
customEmojis={false}
|
2020-02-21 16:13:05 +00:00
|
|
|
useRealName={useRealName}
|
2019-10-02 12:41:51 +00:00
|
|
|
numberOfLines={2}
|
|
|
|
preview
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-10-02 12:41:51 +00:00
|
|
|
/>
|
2019-04-18 20:57:35 +00:00
|
|
|
), arePropsEqual);
|
|
|
|
|
|
|
|
LastMessage.propTypes = {
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2019-04-18 20:57:35 +00:00
|
|
|
lastMessage: PropTypes.object,
|
|
|
|
type: PropTypes.string,
|
|
|
|
showLastMessage: PropTypes.bool,
|
2019-04-24 18:36:29 +00:00
|
|
|
username: PropTypes.string,
|
2020-02-21 16:13:05 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2019-04-24 18:36:29 +00:00
|
|
|
alert: PropTypes.bool
|
2019-04-18 20:57:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default LastMessage;
|