Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ChatMessage.tsx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 { isGifMessage } from '../../../gifs/functions.native';
  9. import { 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 { 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 = replaceNonUnicodeEmojis(getMessageText(this.props.message));
  64. return (
  65. <View style = { styles.messageWrapper as ViewStyle } >
  66. { this._renderAvatar() }
  67. <View style = { detailsWrapperStyle }>
  68. <View style = { messageBubbleStyle }>
  69. <View style = { styles.textWrapper as ViewStyle } >
  70. { this._renderDisplayName() }
  71. { isGifMessage(messageText)
  72. ? <GifMessage message = { messageText } />
  73. : (
  74. <Linkify
  75. linkStyle = { styles.chatLink }
  76. style = { styles.chatMessage }>
  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. /**
  90. * Renders the avatar of the sender.
  91. *
  92. * @returns {React$Element<*>}
  93. */
  94. _renderAvatar() {
  95. const { message } = this.props;
  96. return (
  97. <View style = { styles.avatarWrapper }>
  98. { this.props.showAvatar && <Avatar
  99. displayName = { message.displayName }
  100. participantId = { message.participantId }
  101. size = { styles.avatarWrapper.width } />
  102. }
  103. </View>
  104. );
  105. }
  106. /**
  107. * Renders the display name of the sender if necessary.
  108. *
  109. * @returns {React$Element<*> | null}
  110. */
  111. _renderDisplayName() {
  112. const { message, showDisplayName } = this.props;
  113. if (!showDisplayName) {
  114. return null;
  115. }
  116. return (
  117. <Text style = { styles.senderDisplayName }>
  118. { message.displayName }
  119. </Text>
  120. );
  121. }
  122. /**
  123. * Renders the message privacy notice, if necessary.
  124. *
  125. * @returns {React$Element<*> | null}
  126. */
  127. _renderPrivateNotice() {
  128. const { message, knocking } = this.props;
  129. if (!(message.privateMessage || (message.lobbyChat && !knocking))) {
  130. return null;
  131. }
  132. return (
  133. <Text style = { message.lobbyChat ? styles.lobbyMsgNotice : styles.privateNotice }>
  134. { getPrivateNoticeMessage(this.props.message) }
  135. </Text>
  136. );
  137. }
  138. /**
  139. * Renders the private reply button, if necessary.
  140. *
  141. * @returns {React$Element<*> | null}
  142. */
  143. _renderPrivateReplyButton() {
  144. const { message, canReply } = this.props;
  145. const { lobbyChat } = message;
  146. if (!canReply) {
  147. return null;
  148. }
  149. return (
  150. <View style = { styles.replyContainer as ViewStyle }>
  151. <PrivateMessageButton
  152. isLobbyMessage = { lobbyChat }
  153. participantID = { message.participantId }
  154. reply = { true }
  155. showLabel = { false }
  156. toggledStyles = { styles.replyStyles } />
  157. </View>
  158. );
  159. }
  160. /**
  161. * Renders the time at which the message was sent, if necessary.
  162. *
  163. * @returns {React$Element<*> | null}
  164. */
  165. _renderTimestamp() {
  166. if (!this.props.showTimestamp) {
  167. return null;
  168. }
  169. return (
  170. <Text style = { styles.timeText }>
  171. { getFormattedTimestamp(this.props.message) }
  172. </Text>
  173. );
  174. }
  175. }
  176. /**
  177. * Maps part of the redux state to the props of this component.
  178. *
  179. * @param {Object} state - The Redux state.
  180. * @returns {IProps}
  181. */
  182. function _mapStateToProps(state: IReduxState, { message }: IChatMessageProps) {
  183. return {
  184. canReply: getCanReplyToMessage(state, message),
  185. knocking: state['features/lobby'].knocking
  186. };
  187. }
  188. export default translate(connect(_mapStateToProps)(ChatMessage));