You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatMessage.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // @flow
  2. import React from 'react';
  3. import { Text, View } from 'react-native';
  4. import { Avatar } from '../../../base/avatar';
  5. import { translate } from '../../../base/i18n';
  6. import { Linkify } from '../../../base/react';
  7. import { replaceNonUnicodeEmojis } from '../../functions';
  8. import AbstractChatMessage, { type Props } from '../AbstractChatMessage';
  9. import PrivateMessageButton from '../PrivateMessageButton';
  10. import styles from './styles';
  11. /**
  12. * Renders a single chat message.
  13. */
  14. class ChatMessage extends AbstractChatMessage<Props> {
  15. /**
  16. * Implements {@code Component#render}.
  17. *
  18. * @inheritdoc
  19. */
  20. render() {
  21. const { message } = this.props;
  22. const localMessage = message.messageType === 'local';
  23. // Style arrays that need to be updated in various scenarios, such as
  24. // error messages or others.
  25. const detailsWrapperStyle = [
  26. styles.detailsWrapper
  27. ];
  28. const textWrapperStyle = [
  29. styles.textWrapper
  30. ];
  31. if (localMessage) {
  32. // The wrapper needs to be aligned to the right.
  33. detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
  34. // The bubble needs to be differently styled.
  35. textWrapperStyle.push(styles.ownTextWrapper);
  36. } else if (message.messageType === 'error') {
  37. // The bubble needs to be differently styled.
  38. textWrapperStyle.push(styles.systemTextWrapper);
  39. }
  40. const messageText = message.messageType === 'error'
  41. ? this.props.t('chat.error', {
  42. error: message.error,
  43. originalText: message.message
  44. })
  45. : message.message;
  46. return (
  47. <View style = { styles.messageWrapper } >
  48. { this._renderAvatar() }
  49. <View style = { detailsWrapperStyle }>
  50. <View style = { styles.replyWrapper }>
  51. <View style = { textWrapperStyle } >
  52. {
  53. this.props.showDisplayName
  54. && this._renderDisplayName()
  55. }
  56. <Linkify linkStyle = { styles.chatLink }>
  57. { replaceNonUnicodeEmojis(messageText) }
  58. </Linkify>
  59. {
  60. message.privateMessage
  61. && this._renderPrivateNotice()
  62. }
  63. </View>
  64. { message.privateMessage && !localMessage
  65. && <PrivateMessageButton
  66. participantID = { message.id }
  67. reply = { true }
  68. showLabel = { false }
  69. toggledStyles = { styles.replyStyles } /> }
  70. </View>
  71. { this.props.showTimestamp && this._renderTimestamp() }
  72. </View>
  73. </View>
  74. );
  75. }
  76. _getFormattedTimestamp: () => string;
  77. _getPrivateNoticeMessage: () => string;
  78. /**
  79. * Renders the avatar of the sender.
  80. *
  81. * @returns {React$Element<*>}
  82. */
  83. _renderAvatar() {
  84. const { message } = this.props;
  85. return (
  86. <View style = { styles.avatarWrapper }>
  87. { this.props.showAvatar && <Avatar
  88. displayName = { message.displayName }
  89. participantId = { message.id }
  90. size = { styles.avatarWrapper.width } />
  91. }
  92. </View>
  93. );
  94. }
  95. /**
  96. * Renders the display name of the sender.
  97. *
  98. * @returns {React$Element<*>}
  99. */
  100. _renderDisplayName() {
  101. return (
  102. <Text style = { styles.displayName }>
  103. { this.props.message.displayName }
  104. </Text>
  105. );
  106. }
  107. /**
  108. * Renders the message privacy notice.
  109. *
  110. * @returns {React$Element<*>}
  111. */
  112. _renderPrivateNotice() {
  113. return (
  114. <Text style = { styles.privateNotice }>
  115. { this._getPrivateNoticeMessage() }
  116. </Text>
  117. );
  118. }
  119. /**
  120. * Renders the time at which the message was sent.
  121. *
  122. * @returns {React$Element<*>}
  123. */
  124. _renderTimestamp() {
  125. return (
  126. <Text style = { styles.timeText }>
  127. { this._getFormattedTimestamp() }
  128. </Text>
  129. );
  130. }
  131. }
  132. export default translate(ChatMessage);