2019-04-18 20:57:35 +00:00
|
|
|
import React from 'react';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import styles from './styles';
|
2022-04-20 21:37:54 +00:00
|
|
|
import { MarkdownPreview } from '../markdown';
|
2022-06-27 18:23:43 +00:00
|
|
|
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../../lib/constants';
|
2022-04-20 21:37:54 +00:00
|
|
|
import { ILastMessageProps } from './interfaces';
|
2022-06-27 18:23:43 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2019-04-18 20:57:35 +00:00
|
|
|
|
2022-04-20 21:37:54 +00:00
|
|
|
const formatMsg = ({ lastMessage, type, showLastMessage, username, useRealName }: Partial<ILastMessageProps>) => {
|
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');
|
2021-09-13 20:41:05 +00:00
|
|
|
} else if (type !== 'd') {
|
|
|
|
const {
|
|
|
|
u: { name }
|
|
|
|
} = lastMessage;
|
|
|
|
prefix = `${useRealName ? name : lastMessage.u.username}: `;
|
2019-04-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return `${prefix}${lastMessage.msg}`;
|
2019-04-18 20:57:35 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const arePropsEqual = (oldProps: any, newProps: any) => dequal(oldProps, newProps);
|
2019-04-18 20:57:35 +00:00
|
|
|
|
2022-06-27 18:23:43 +00:00
|
|
|
const LastMessage = React.memo(({ lastMessage, type, showLastMessage, username, alert, useRealName }: ILastMessageProps) => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
return (
|
2022-02-17 15:27:01 +00:00
|
|
|
<MarkdownPreview
|
2021-09-13 20:41:05 +00:00
|
|
|
msg={formatMsg({
|
|
|
|
lastMessage,
|
|
|
|
type,
|
|
|
|
showLastMessage,
|
|
|
|
username,
|
|
|
|
useRealName
|
|
|
|
})}
|
2022-06-27 18:23:43 +00:00
|
|
|
style={[styles.markdownText, { color: alert ? colors.bodyText : colors.auxiliaryText }]}
|
2021-09-13 20:41:05 +00:00
|
|
|
numberOfLines={2}
|
2021-10-06 20:30:10 +00:00
|
|
|
testID='room-item-last-message'
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
2022-06-27 18:23:43 +00:00
|
|
|
);
|
|
|
|
}, arePropsEqual);
|
2019-04-18 20:57:35 +00:00
|
|
|
|
|
|
|
export default LastMessage;
|