Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

ref(chat) Refactor ChatMessage component (#13556)

Robert Pintilii78593977905c
diff --git a/react/features/chat/components/AbstractChatMessage.tsx b/react/features/chat/components/AbstractChatMessage.tsx
deleted file mode 100644
index 374280d50a..0000000000
--- a/react/features/chat/components/AbstractChatMessage.tsx
+++ /dev/null
@@ -1,88 +0,0 @@
-import { PureComponent } from 'react';
-import { WithTranslation } from 'react-i18next';
-
-import { getLocalizedDateFormatter } from '../../base/i18n/dateUtil';
-import { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../constants';
-import { IMessage } from '../types';
-
-/**
- * Formatter string to display the message timestamp.
- */
-const TIMESTAMP_FORMAT = 'H:mm';
-
-/**
- * The type of the React {@code Component} props of {@code AbstractChatMessage}.
- */
-export interface IProps extends WithTranslation {
-
- /**
- * Whether current participant is currently knocking in the lobby room.
- */
- knocking: boolean;
-
- /**
- * The representation of a chat message.
- */
- message: IMessage;
-
- /**
- * Whether or not the avatar image of the participant which sent the message
- * should be displayed.
- */
- showAvatar?: boolean;
-
- /**
- * Whether or not the name of the participant which sent the message should
- * be displayed.
- */
- showDisplayName: boolean;
-
- /**
- * Whether or not the time at which the message was sent should be
- * displayed.
- */
- showTimestamp: boolean;
-}
-
-/**
- * Abstract component to display a chat message.
- */
-export default class AbstractChatMessage<P extends IProps> extends PureComponent<P> {
- /**
- * Returns the timestamp to display for the message.
- *
- * @returns {string}
- */
- _getFormattedTimestamp() {
- return getLocalizedDateFormatter(new Date(this.props.message.timestamp))
- .format(TIMESTAMP_FORMAT);
- }
-
- /**
- * Generates the message text to be rendered in the component.
- *
- * @returns {string}
- */
- _getMessageText() {
- const { message } = this.props;
-
- return message.messageType === MESSAGE_TYPE_ERROR
- ? this.props.t('chat.error', {
- error: message.message
- })
- : message.message;
- }
-
- /**
- * Returns the message that is displayed as a notice for private messages.
- *
- * @returns {string}
- */
- _getPrivateNoticeMessage() {
- const { message, t } = this.props;
-
- return t('chat.privateNotice', {
- recipient: message.messageType === MESSAGE_TYPE_LOCAL ? message.recipient : t('chat.you')
- });
- }
-}
diff --git a/react/features/chat/components/native/Chat.tsx b/react/features/chat/components/native/Chat.tsx
index 879813a6a8..4024fb949b 100644
--- a/react/features/chat/components/native/Chat.tsx
+++ b/react/features/chat/components/native/Chat.tsx
@@ -8,7 +8,7 @@ import { translate } from '../../../base/i18n/functions';
import JitsiScreen from '../../../base/modal/components/JitsiScreen';
import { TabBarLabelCounter } from '../../../mobile/navigation/components/TabBarLabelCounter';
import { closeChat, sendMessage } from '../../actions.native';
-import { IProps as AbstractProps } from '../../types';
+import { IChatProps as AbstractProps } from '../../types';
import ChatInputBar from './ChatInputBar';
import MessageContainer from './MessageContainer';
diff --git a/react/features/chat/components/native/ChatMessage.tsx b/react/features/chat/components/native/ChatMessage.tsx
index e3a901f544..595647a697 100644
--- a/react/features/chat/components/native/ChatMessage.tsx
+++ b/react/features/chat/components/native/ChatMessage.tsx
@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { Component } from 'react';
import { Text, View, ViewStyle } from 'react-native';
import { connect } from 'react-redux';
@@ -8,8 +8,13 @@ import { translate } from '../../../base/i18n/functions';
import Linkify from '../../../base/react/components/native/Linkify';
import { isGifMessage } from '../../../gifs/functions.native';
import { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../../constants';
-import { replaceNonUnicodeEmojis } from '../../functions';
-import AbstractChatMessage, { IProps } from '../AbstractChatMessage';
+import {
+ getFormattedTimestamp,
+ getMessageText,
+ getPrivateNoticeMessage,
+ replaceNonUnicodeEmojis
+} from '../../functions';
+import { IChatMessageProps } from '../../types';
import GifMessage from './GifMessage';
import PrivateMessageButton from './PrivateMessageButton';
@@ -19,7 +24,7 @@ import styles from './styles';
/**
* Renders a single chat message.
*/
-class ChatMessage extends AbstractChatMessage<IProps> {
+class ChatMessage extends Component<IChatMessageProps> {
/**
* Implements {@code Component#render}.
*
@@ -67,7 +72,7 @@ class ChatMessage extends AbstractChatMessage<IProps> {
messageBubbleStyle.push(styles.lobbyMessageBubble);
}
- const messageText = replaceNonUnicodeEmojis(this._getMessageText());
+ const messageText = replaceNonUnicodeEmojis(getMessageText(this.props.message));
return (
<View style = { styles.messageWrapper as ViewStyle } >
@@ -147,7 +152,7 @@ class ChatMessage extends AbstractChatMessage<IProps> {
return (
<Text style = { message.lobbyChat ? styles.lobbyMsgNotice : styles.privateNotice }>
- { this._getPrivateNoticeMessage() }
+ { getPrivateNoticeMessage(this.props.message) }
</Text>
);
}
@@ -189,7 +194,7 @@ class ChatMessage extends AbstractChatMessage<IProps> {
return (
<Text style = { styles.timeText }>
- { this._getFormattedTimestamp() }
+ { getFormattedTimestamp(this.props.message) }
</Text>
);
}
diff --git a/react/features/chat/components/web/Chat.tsx b/react/features/chat/components/web/Chat.tsx
index 03457c3323..4cb572909f 100644
--- a/react/features/chat/components/web/Chat.tsx
+++ b/react/features/chat/components/web/Chat.tsx
@@ -9,7 +9,7 @@ import Tabs from '../../../base/ui/components/web/Tabs';
import PollsPane from '../../../polls/components/web/PollsPane';
import { sendMessage, setIsPollsTabFocused, toggleChat } from '../../actions.web';
import { CHAT_TABS, SMALL_WIDTH_THRESHOLD } from '../../constants';
-import { IProps as AbstractProps } from '../../types';
+import { IChatProps as AbstractProps } from '../../types';
import ChatHeader from './ChatHeader';
import ChatInput from './ChatInput';
diff --git a/react/features/chat/components/web/ChatMessage.tsx b/react/features/chat/components/web/ChatMessage.tsx
index a5ad49fe09..37070d3980 100644
--- a/react/features/chat/components/web/ChatMessage.tsx
+++ b/react/features/chat/components/web/ChatMessage.tsx
@@ -1,26 +1,24 @@
import { Theme } from '@mui/material';
-import { withStyles } from '@mui/styles';
-import clsx from 'clsx';
import React from 'react';
import { connect } from 'react-redux';
+import { makeStyles } from 'tss-react/mui';
import { IReduxState } from '../../../app/types';
import { translate } from '../../../base/i18n/functions';
import Message from '../../../base/react/components/web/Message';
import { withPixelLineHeight } from '../../../base/styles/functions.web';
import { MESSAGE_TYPE_LOCAL } from '../../constants';
-import AbstractChatMessage, { IProps as AbstractProps } from '../AbstractChatMessage';
+import { getFormattedTimestamp, getMessageText, getPrivateNoticeMessage } from '../../functions';
+import { IChatMessageProps } from '../../types';
import PrivateMessageButton from './PrivateMessageButton';
-interface IProps extends AbstractProps {
-
- classes: any;
+interface IProps extends IChatMessageProps {
type: string;
}
-const styles = (theme: Theme) => {
+const useStyles = makeStyles()((theme: Theme) => {
return {
chatMessageWrapper: {
maxWidth: '100%'
@@ -110,73 +108,35 @@ const styles = (theme: Theme) => {
marginTop: theme.spacing(1)
}
};
-};
+});
/**
* Renders a single chat message.
+ *
+ * @param {IProps} props - Component's props.
+ * @returns {JSX}
*/
-class ChatMessage extends AbstractChatMessage<IProps> {
- /**
- * Implements React's {@link Component#render()}.
- *
- * @inheritdoc
- * @returns {ReactElement}
- */
- render() {
- const { message, t, knocking, classes, type } = this.props;
-
- return (
- <div
- className = { clsx(classes.chatMessageWrapper, type) }
- id = { this.props.message.messageId }
- tabIndex = { -1 }>
- <div
- className = { clsx('chatmessage', classes.chatMessage, type,
- message.privateMessage && 'privatemessage',
- message.lobbyChat && !knocking && 'lobbymessage') }>
- <div className = { classes.replyWrapper }>
- <div className = { clsx('messagecontent', classes.messageContent) }>
- { this.props.showDisplayName && this._renderDisplayName() }
- <div className = { clsx('usermessage', classes.userMessage) }>
- <span className = 'sr-only'>
- { this.props.message.displayName === this.props.message.recipient
- ? t('chat.messageAccessibleTitleMe')
- : t('chat.messageAccessibleTitle',
- { user: this.props.message.displayName }) }
- </span>
- <Message text = { this._getMessageText() } />
- </div>
- { (message.privateMessage || (message.lobbyChat && !knocking))
- && this._renderPrivateNotice() }
- </div>
- { (message.privateMessage || (message.lobbyChat && !knocking))
- && message.messageType !== MESSAGE_TYPE_LOCAL
- && (
- <div
- className = { classes.replyButtonContainer }>
- <PrivateMessageButton
- isLobbyMessage = { message.lobbyChat }
- participantID = { message.id } />
- </div>
- ) }
- </div>
- </div>
- { this.props.showTimestamp && this._renderTimestamp() }
- </div>
- );
- }
+const ChatMessage = ({
+ knocking,
+ message,
+ showDisplayName,
+ showTimestamp,
+ type,
+ t
+}: IProps) => {
+ const { classes, cx } = useStyles();
/**
* Renders the display name of the sender.
*
* @returns {React$Element<*>}
*/
- _renderDisplayName() {
+ function _renderDisplayName() {
return (
<div
aria-hidden = { true }
- className = { clsx('display-name', this.props.classes.displayName) }>
- { this.props.message.displayName }
+ className = { cx('display-name', classes.displayName) }>
+ {message.displayName}
</div>
);
}
@@ -186,10 +146,10 @@ class ChatMessage extends AbstractChatMessage<IProps> {
*
* @returns {React$Element<*>}
*/
- _renderPrivateNotice() {
+ function _renderPrivateNotice() {
return (
- <div className = { this.props.classes.privateMessageNotice }>
- { this._getPrivateNoticeMessage() }
+ <div className = { classes.privateMessageNotice }>
+ {getPrivateNoticeMessage(message)}
</div>
);
}
@@ -199,14 +159,54 @@ class ChatMessage extends AbstractChatMessage<IProps> {
*
* @returns {React$Element<*>}
*/
- _renderTimestamp() {
+ function _renderTimestamp() {
return (
- <div className = { clsx('timestamp', this.props.classes.timestamp) }>
- { this._getFormattedTimestamp() }
+ <div className = { cx('timestamp', classes.timestamp) }>
+ {getFormattedTimestamp(message)}
</div>
);
}
-}
+
+ return (
+ <div
+ className = { cx(classes.chatMessageWrapper, type) }
+ id = { message.messageId }
+ tabIndex = { -1 }>
+ <div
+ className = { cx('chatmessage', classes.chatMessage, type,
+ message.privateMessage && 'privatemessage',
+ message.lobbyChat && !knocking && 'lobbymessage') }>
+ <div className = { classes.replyWrapper }>
+ <div className = { cx('messagecontent', classes.messageContent) }>
+ {showDisplayName && _renderDisplayName()}
+ <div className = { cx('usermessage', classes.userMessage) }>
+ <span className = 'sr-only'>
+ {message.displayName === message.recipient
+ ? t('chat.messageAccessibleTitleMe')
+ : t('chat.messageAccessibleTitle',
+ { user: message.displayName })}
+ </span>
+ <Message text = { getMessageText(message) } />
+ </div>
+ {(message.privateMessage || (message.lobbyChat && !knocking))
+ && _renderPrivateNotice()}
+ </div>
+ {(message.privateMessage || (message.lobbyChat && !knocking))
+ && message.messageType !== MESSAGE_TYPE_LOCAL
+ && (
+ <div
+ className = { classes.replyButtonContainer }>
+ <PrivateMessageButton
+ isLobbyMessage = { message.lobbyChat }
+ participantID = { message.id } />
+ </div>
+ )}
+ </div>
+ </div>
+ {showTimestamp && _renderTimestamp()}
+ </div>
+ );
+};
/**
* Maps part of the Redux store to the props of this component.
@@ -222,4 +222,4 @@ function _mapStateToProps(state: IReduxState) {
};
}
-export default translate(connect(_mapStateToProps)(withStyles(styles)(ChatMessage)));
+export default translate(connect(_mapStateToProps)(ChatMessage));
diff --git a/react/features/chat/constants.ts b/react/features/chat/constants.ts
index afb824f994..0699725378 100644
--- a/react/features/chat/constants.ts
+++ b/react/features/chat/constants.ts
@@ -38,3 +38,8 @@ export const CHAT_TABS = {
POLLS: 'polls-tab',
CHAT: 'chat-tab'
};
+
+/**
+ * Formatter string to display the message timestamp.
+ */
+export const TIMESTAMP_FORMAT = 'H:mm';
diff --git a/react/features/chat/functions.ts b/react/features/chat/functions.ts
index 046dee2cda..646768d013 100644
--- a/react/features/chat/functions.ts
+++ b/react/features/chat/functions.ts
@@ -5,8 +5,11 @@ import aliases from 'react-emoji-render/data/aliases';
import emojiAsciiAliases from 'react-emoji-render/data/asciiAliases';
import { IReduxState } from '../app/types';
+import { getLocalizedDateFormatter } from '../base/i18n/dateUtil';
+import i18next from '../base/i18n/i18next';
import { escapeRegexp } from '../base/util/helpers';
+import { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL, TIMESTAMP_FORMAT } from './constants';
import { IMessage } from './types';
/**
@@ -132,3 +135,40 @@ export function areSmileysDisabled(state: IReduxState) {
return disableChatSmileys;
}
+
+/**
+ * Returns the timestamp to display for the message.
+ *
+ * @param {IMessage} message - The message from which to get the timestamp.
+ * @returns {string}
+ */
+export function getFormattedTimestamp(message: IMessage) {
+ return getLocalizedDateFormatter(new Date(message.timestamp))
+ .format(TIMESTAMP_FORMAT);
+}
+
+/**
+ * Generates the message text to be rendered in the component.
+ *
+ * @param {IMessage} message - The message from which to get the text.
+ * @returns {string}
+ */
+export function getMessageText(message: IMessage) {
+ return message.messageType === MESSAGE_TYPE_ERROR
+ ? i18next.t('chat.error', {
+ error: message.message
+ })
+ : message.message;
+}
+
+/**
+ * Returns the message that is displayed as a notice for private messages.
+ *
+ * @param {IMessage} message - The message to be checked.
+ * @returns {string}
+ */
+export function getPrivateNoticeMessage(message: IMessage) {
+ return i18next.t('chat.privateNotice', {
+ recipient: message.messageType === MESSAGE_TYPE_LOCAL ? message.recipient : i18next.t('chat.you')
+ });
+}
diff --git a/react/features/chat/types.ts b/react/features/chat/types.ts
index eecb9081a3..f9a4a9510b 100644
--- a/react/features/chat/types.ts
+++ b/react/features/chat/types.ts
@@ -19,7 +19,7 @@ export interface IMessage {
/**
* The type of the React {@code Component} props of {@code AbstractChat}.
*/
-export interface IProps extends WithTranslation {
+export interface IChatProps extends WithTranslation {
/**
* All the chat messages in the conference.
@@ -36,3 +36,34 @@ export interface IProps extends WithTranslation {
*/
dispatch: IStore['dispatch'];
}
+
+export interface IChatMessageProps extends WithTranslation {
+
+ /**
+ * Whether current participant is currently knocking in the lobby room.
+ */
+ knocking: boolean;
+
+ /**
+ * The representation of a chat message.
+ */
+ message: IMessage;
+
+ /**
+ * Whether or not the avatar image of the participant which sent the message
+ * should be displayed.
+ */
+ showAvatar?: boolean;
+
+ /**
+ * Whether or not the name of the participant which sent the message should
+ * be displayed.
+ */
+ showDisplayName: boolean;
+
+ /**
+ * Whether or not the time at which the message was sent should be
+ * displayed.
+ */
+ showTimestamp: boolean;
+}