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

ChatMessage.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 { MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOCAL } from '../../constants';
  11. import { replaceNonUnicodeEmojis } from '../../functions';
  12. import AbstractChatMessage, { type Props as AbstractProps } from '../AbstractChatMessage';
  13. import PrivateMessageButton from '../PrivateMessageButton';
  14. import styles from './styles';
  15. type Props = AbstractProps & {
  16. /**
  17. * The color-schemed stylesheet of the feature.
  18. */
  19. _styles: StyleType
  20. };
  21. /**
  22. * Renders a single chat message.
  23. */
  24. class ChatMessage extends AbstractChatMessage<Props> {
  25. /**
  26. * Implements {@code Component#render}.
  27. *
  28. * @inheritdoc
  29. */
  30. render() {
  31. const { _styles, message } = this.props;
  32. const localMessage = message.messageType === MESSAGE_TYPE_LOCAL;
  33. const { privateMessage } = message;
  34. // Style arrays that need to be updated in various scenarios, such as
  35. // error messages or others.
  36. const detailsWrapperStyle = [
  37. styles.detailsWrapper
  38. ];
  39. const messageBubbleStyle = [
  40. styles.messageBubble
  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);
  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. return (
  61. <View style = { styles.messageWrapper } >
  62. { this._renderAvatar() }
  63. <View style = { detailsWrapperStyle }>
  64. <View style = { messageBubbleStyle }>
  65. <View style = { styles.textWrapper } >
  66. { this._renderDisplayName() }
  67. <Linkify linkStyle = { styles.chatLink }>
  68. { replaceNonUnicodeEmojis(this._getMessageText()) }
  69. </Linkify>
  70. { this._renderPrivateNotice() }
  71. </View>
  72. { this._renderPrivateReplyButton() }
  73. </View>
  74. { this._renderTimestamp() }
  75. </View>
  76. </View>
  77. );
  78. }
  79. _getFormattedTimestamp: () => string;
  80. _getMessageText: () => string;
  81. _getPrivateNoticeMessage: () => string;
  82. /**
  83. * Renders the avatar of the sender.
  84. *
  85. * @returns {React$Element<*>}
  86. */
  87. _renderAvatar() {
  88. const { message } = this.props;
  89. return (
  90. <View style = { styles.avatarWrapper }>
  91. { this.props.showAvatar && <Avatar
  92. displayName = { message.displayName }
  93. participantId = { message.id }
  94. size = { styles.avatarWrapper.width } />
  95. }
  96. </View>
  97. );
  98. }
  99. /**
  100. * Renders the display name of the sender if necessary.
  101. *
  102. * @returns {React$Element<*> | null}
  103. */
  104. _renderDisplayName() {
  105. const { _styles, message, showDisplayName } = this.props;
  106. if (!showDisplayName) {
  107. return null;
  108. }
  109. return (
  110. <Text style = { _styles.displayName }>
  111. { message.displayName }
  112. </Text>
  113. );
  114. }
  115. /**
  116. * Renders the message privacy notice, if necessary.
  117. *
  118. * @returns {React$Element<*> | null}
  119. */
  120. _renderPrivateNotice() {
  121. const { _styles, message } = this.props;
  122. if (!message.privateMessage) {
  123. return null;
  124. }
  125. return (
  126. <Text style = { _styles.privateNotice }>
  127. { this._getPrivateNoticeMessage() }
  128. </Text>
  129. );
  130. }
  131. /**
  132. * Renders the private reply button, if necessary.
  133. *
  134. * @returns {React$Element<*> | null}
  135. */
  136. _renderPrivateReplyButton() {
  137. const { _styles, message } = this.props;
  138. const { messageType, privateMessage } = message;
  139. if (!privateMessage || messageType === MESSAGE_TYPE_LOCAL) {
  140. return null;
  141. }
  142. return (
  143. <View style = { _styles.replyContainer }>
  144. <PrivateMessageButton
  145. participantID = { message.id }
  146. reply = { true }
  147. showLabel = { false }
  148. toggledStyles = { _styles.replyStyles } />
  149. </View>
  150. );
  151. }
  152. /**
  153. * Renders the time at which the message was sent, if necessary.
  154. *
  155. * @returns {React$Element<*> | null}
  156. */
  157. _renderTimestamp() {
  158. if (!this.props.showTimestamp) {
  159. return null;
  160. }
  161. return (
  162. <Text style = { styles.timeText }>
  163. { this._getFormattedTimestamp() }
  164. </Text>
  165. );
  166. }
  167. }
  168. /**
  169. * Maps part of the redux state to the props of this component.
  170. *
  171. * @param {Object} state - The Redux state.
  172. * @returns {Props}
  173. */
  174. function _mapStateToProps(state) {
  175. return {
  176. _styles: ColorSchemeRegistry.get(state, 'Chat')
  177. };
  178. }
  179. export default translate(connect(_mapStateToProps)(ChatMessage));