Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ChatMessage.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React from 'react';
  2. import { Text, View } from 'react-native';
  3. import { Avatar } from '../../../base/avatar';
  4. import { translate } from '../../../base/i18n';
  5. import { Linkify } from '../../../base/react';
  6. import { connect } from '../../../base/redux';
  7. import { isGifMessage } from '../../../gifs/functions';
  8. import { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../../constants';
  9. import { replaceNonUnicodeEmojis } from '../../functions';
  10. import AbstractChatMessage, { type Props } from '../AbstractChatMessage';
  11. import GifMessage from './GifMessage';
  12. import PrivateMessageButton from './PrivateMessageButton';
  13. import styles from './styles';
  14. /**
  15. * Renders a single chat message.
  16. */
  17. class ChatMessage extends AbstractChatMessage<Props> {
  18. /**
  19. * Implements {@code Component#render}.
  20. *
  21. * @inheritdoc
  22. */
  23. render() {
  24. const { message, knocking } = this.props;
  25. const localMessage = message.messageType === MESSAGE_TYPE_LOCAL;
  26. const { privateMessage, lobbyChat } = message;
  27. // Style arrays that need to be updated in various scenarios, such as
  28. // error messages or others.
  29. const detailsWrapperStyle = [
  30. styles.detailsWrapper
  31. ];
  32. const messageBubbleStyle = [
  33. styles.messageBubble
  34. ];
  35. if (localMessage) {
  36. // This is a message sent by the local participant.
  37. // The wrapper needs to be aligned to the right.
  38. detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
  39. // The bubble needs some additional styling
  40. messageBubbleStyle.push(styles.localMessageBubble);
  41. } else if (message.messageType === MESSAGE_TYPE_ERROR) {
  42. // This is a system message.
  43. // The bubble needs some additional styling
  44. messageBubbleStyle.push(styles.systemMessageBubble);
  45. } else {
  46. // This is a remote message sent by a remote participant.
  47. // The bubble needs some additional styling
  48. messageBubbleStyle.push(styles.remoteMessageBubble);
  49. }
  50. if (privateMessage) {
  51. messageBubbleStyle.push(styles.privateMessageBubble);
  52. }
  53. if (lobbyChat && !knocking) {
  54. messageBubbleStyle.push(styles.lobbyMessageBubble);
  55. }
  56. const messageText = replaceNonUnicodeEmojis(this._getMessageText());
  57. return (
  58. <View style = { styles.messageWrapper } >
  59. { this._renderAvatar() }
  60. <View style = { detailsWrapperStyle }>
  61. <View style = { messageBubbleStyle }>
  62. <View style = { styles.textWrapper } >
  63. { this._renderDisplayName() }
  64. { isGifMessage(messageText)
  65. ? <GifMessage message = { messageText } />
  66. : (
  67. <Linkify
  68. linkStyle = { styles.chatLink }
  69. style = { styles.chatMessage }>
  70. { messageText }
  71. </Linkify>
  72. )}
  73. { this._renderPrivateNotice() }
  74. </View>
  75. { this._renderPrivateReplyButton() }
  76. </View>
  77. { this._renderTimestamp() }
  78. </View>
  79. </View>
  80. );
  81. }
  82. _getFormattedTimestamp: () => string;
  83. _getMessageText: () => string;
  84. _getPrivateNoticeMessage: () => string;
  85. /**
  86. * Renders the avatar of the sender.
  87. *
  88. * @returns {React$Element<*>}
  89. */
  90. _renderAvatar() {
  91. const { message } = this.props;
  92. return (
  93. <View style = { styles.avatarWrapper }>
  94. { this.props.showAvatar && <Avatar
  95. displayName = { message.displayName }
  96. participantId = { message.id }
  97. size = { styles.avatarWrapper.width } />
  98. }
  99. </View>
  100. );
  101. }
  102. /**
  103. * Renders the display name of the sender if necessary.
  104. *
  105. * @returns {React$Element<*> | null}
  106. */
  107. _renderDisplayName() {
  108. const { message, showDisplayName } = this.props;
  109. if (!showDisplayName) {
  110. return null;
  111. }
  112. return (
  113. <Text style = { styles.senderDisplayName }>
  114. { message.displayName }
  115. </Text>
  116. );
  117. }
  118. /**
  119. * Renders the message privacy notice, if necessary.
  120. *
  121. * @returns {React$Element<*> | null}
  122. */
  123. _renderPrivateNotice() {
  124. const { message, knocking } = this.props;
  125. if (!(message.privateMessage || (message.lobbyChat && !knocking))) {
  126. return null;
  127. }
  128. return (
  129. <Text style = { message.lobbyChat ? styles.lobbyMsgNotice : styles.privateNotice }>
  130. { this._getPrivateNoticeMessage() }
  131. </Text>
  132. );
  133. }
  134. /**
  135. * Renders the private reply button, if necessary.
  136. *
  137. * @returns {React$Element<*> | null}
  138. */
  139. _renderPrivateReplyButton() {
  140. const { message, knocking } = this.props;
  141. const { messageType, privateMessage, lobbyChat } = message;
  142. if (!(privateMessage || lobbyChat) || messageType === MESSAGE_TYPE_LOCAL || knocking) {
  143. return null;
  144. }
  145. return (
  146. <View style = { styles.replyContainer }>
  147. <PrivateMessageButton
  148. isLobbyMessage = { lobbyChat }
  149. participantID = { message.id }
  150. reply = { true }
  151. showLabel = { false }
  152. toggledStyles = { styles.replyStyles } />
  153. </View>
  154. );
  155. }
  156. /**
  157. * Renders the time at which the message was sent, if necessary.
  158. *
  159. * @returns {React$Element<*> | null}
  160. */
  161. _renderTimestamp() {
  162. if (!this.props.showTimestamp) {
  163. return null;
  164. }
  165. return (
  166. <Text style = { styles.timeText }>
  167. { this._getFormattedTimestamp() }
  168. </Text>
  169. );
  170. }
  171. }
  172. /**
  173. * Maps part of the redux state to the props of this component.
  174. *
  175. * @param {Object} state - The Redux state.
  176. * @returns {Props}
  177. */
  178. function _mapStateToProps(state) {
  179. return {
  180. knocking: state['features/lobby'].knocking
  181. };
  182. }
  183. export default translate(connect(_mapStateToProps)(ChatMessage));