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

ChatMessage.js 6.7KB

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