您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatMessage.tsx 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import React, { Component } from 'react';
  2. import { Text, View, ViewStyle } from 'react-native';
  3. import { connect } from 'react-redux';
  4. import { IReduxState } from '../../../app/types';
  5. import Avatar from '../../../base/avatar/components/Avatar';
  6. import { translate } from '../../../base/i18n/functions';
  7. import Linkify from '../../../base/react/components/native/Linkify';
  8. import { isGifEnabled, isGifMessage } from '../../../gifs/functions.native';
  9. import { CHAR_LIMIT, MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../../constants';
  10. import {
  11. getCanReplyToMessage,
  12. getFormattedTimestamp,
  13. getMessageText,
  14. getPrivateNoticeMessage,
  15. replaceNonUnicodeEmojis
  16. } from '../../functions';
  17. import { IChatMessageProps } from '../../types';
  18. import GifMessage from './GifMessage';
  19. import PrivateMessageButton from './PrivateMessageButton';
  20. import styles from './styles';
  21. /**
  22. * Renders a single chat message.
  23. */
  24. class ChatMessage extends Component<IChatMessageProps> {
  25. /**
  26. * Implements {@code Component#render}.
  27. *
  28. * @inheritdoc
  29. */
  30. render() {
  31. const { gifEnabled, message, knocking } = this.props;
  32. const localMessage = message.messageType === MESSAGE_TYPE_LOCAL;
  33. const { privateMessage, lobbyChat } = message;
  34. // Style arrays that need to be updated in various scenarios, such as
  35. // error messages or others.
  36. const detailsWrapperStyle: ViewStyle[] = [
  37. styles.detailsWrapper as ViewStyle
  38. ];
  39. const messageBubbleStyle: ViewStyle[] = [
  40. styles.messageBubble as ViewStyle
  41. ];
  42. if (localMessage) {
  43. // This is a message sent by the local participant.
  44. // The wrapper needs to be aligned to the right.
  45. detailsWrapperStyle.push(styles.ownMessageDetailsWrapper as ViewStyle);
  46. // The bubble needs some additional styling
  47. messageBubbleStyle.push(styles.localMessageBubble);
  48. } else if (message.messageType === MESSAGE_TYPE_ERROR) {
  49. // This is a system message.
  50. // The bubble needs some additional styling
  51. messageBubbleStyle.push(styles.systemMessageBubble);
  52. } else {
  53. // This is a remote message sent by a remote participant.
  54. // The bubble needs some additional styling
  55. messageBubbleStyle.push(styles.remoteMessageBubble);
  56. }
  57. if (privateMessage) {
  58. messageBubbleStyle.push(styles.privateMessageBubble);
  59. }
  60. if (lobbyChat && !knocking) {
  61. messageBubbleStyle.push(styles.lobbyMessageBubble);
  62. }
  63. const messageText = getMessageText(this.props.message);
  64. return (
  65. <View
  66. id = { message.messageId }
  67. style = { styles.messageWrapper as ViewStyle } >
  68. { this._renderAvatar() }
  69. <View style = { detailsWrapperStyle }>
  70. <View style = { messageBubbleStyle }>
  71. <View style = { styles.textWrapper as ViewStyle } >
  72. { this._renderDisplayName() }
  73. { gifEnabled && isGifMessage(messageText)
  74. ? <GifMessage message = { messageText } />
  75. : this._renderMessageTextComponent(messageText) }
  76. { this._renderPrivateNotice() }
  77. </View>
  78. { this._renderPrivateReplyButton() }
  79. </View>
  80. { this._renderTimestamp() }
  81. </View>
  82. </View>
  83. );
  84. }
  85. /**
  86. * Renders the avatar of the sender.
  87. *
  88. * @returns {React.ReactElement<*>}
  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.participantId }
  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.ReactElement<*> | 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 text based on number of characters.
  120. *
  121. * @param {string} messageText - The message text.
  122. * @returns {React.ReactElement<*>}
  123. */
  124. _renderMessageTextComponent(messageText: string) {
  125. if (messageText.length >= CHAR_LIMIT) {
  126. return (
  127. <Text
  128. selectable = { true }
  129. style = { styles.chatMessage }>
  130. { messageText }
  131. </Text>
  132. );
  133. }
  134. return (
  135. <Linkify
  136. linkStyle = { styles.chatLink }
  137. style = { styles.chatMessage }>
  138. { replaceNonUnicodeEmojis(messageText) }
  139. </Linkify>
  140. );
  141. }
  142. /**
  143. * Renders the message privacy notice, if necessary.
  144. *
  145. * @returns {React.ReactElement<*> | null}
  146. */
  147. _renderPrivateNotice() {
  148. const { message, knocking } = this.props;
  149. if (!(message.privateMessage || (message.lobbyChat && !knocking))) {
  150. return null;
  151. }
  152. return (
  153. <Text style = { message.lobbyChat ? styles.lobbyMsgNotice : styles.privateNotice }>
  154. { getPrivateNoticeMessage(this.props.message) }
  155. </Text>
  156. );
  157. }
  158. /**
  159. * Renders the private reply button, if necessary.
  160. *
  161. * @returns {React.ReactElement<*> | null}
  162. */
  163. _renderPrivateReplyButton() {
  164. const { message, canReply } = this.props;
  165. const { lobbyChat } = message;
  166. if (!canReply) {
  167. return null;
  168. }
  169. return (
  170. <View style = { styles.replyContainer as ViewStyle }>
  171. <PrivateMessageButton
  172. isLobbyMessage = { lobbyChat }
  173. participantID = { message.participantId }
  174. reply = { true }
  175. showLabel = { false }
  176. toggledStyles = { styles.replyStyles } />
  177. </View>
  178. );
  179. }
  180. /**
  181. * Renders the time at which the message was sent, if necessary.
  182. *
  183. * @returns {React.ReactElement<*> | null}
  184. */
  185. _renderTimestamp() {
  186. if (!this.props.showTimestamp) {
  187. return null;
  188. }
  189. return (
  190. <Text style = { styles.timeText }>
  191. { getFormattedTimestamp(this.props.message) }
  192. </Text>
  193. );
  194. }
  195. }
  196. /**
  197. * Maps part of the redux state to the props of this component.
  198. *
  199. * @param {Object} state - The Redux state.
  200. * @param {IChatMessageProps} message - Message object.
  201. * @returns {IProps}
  202. */
  203. function _mapStateToProps(state: IReduxState, { message }: IChatMessageProps) {
  204. return {
  205. canReply: getCanReplyToMessage(state, message),
  206. gifEnabled: isGifEnabled(state),
  207. knocking: state['features/lobby'].knocking
  208. };
  209. }
  210. export default translate(connect(_mapStateToProps)(ChatMessage));