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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. return (
  41. <View style = { styles.messageWrapper } >
  42. { this._renderAvatar() }
  43. <View style = { detailsWrapperStyle }>
  44. <View style = { styles.replyWrapper }>
  45. <View style = { textWrapperStyle } >
  46. {
  47. this.props.showDisplayName
  48. && this._renderDisplayName()
  49. }
  50. <Linkify linkStyle = { styles.chatLink }>
  51. { replaceNonUnicodeEmojis(this._getMessageText()) }
  52. </Linkify>
  53. {
  54. message.privateMessage
  55. && this._renderPrivateNotice()
  56. }
  57. </View>
  58. { message.privateMessage && !localMessage
  59. && <PrivateMessageButton
  60. participantID = { message.id }
  61. reply = { true }
  62. showLabel = { false }
  63. toggledStyles = { styles.replyStyles } /> }
  64. </View>
  65. { this.props.showTimestamp && this._renderTimestamp() }
  66. </View>
  67. </View>
  68. );
  69. }
  70. _getFormattedTimestamp: () => string;
  71. _getMessageText: () => string;
  72. _getPrivateNoticeMessage: () => string;
  73. /**
  74. * Renders the avatar of the sender.
  75. *
  76. * @returns {React$Element<*>}
  77. */
  78. _renderAvatar() {
  79. const { message } = this.props;
  80. return (
  81. <View style = { styles.avatarWrapper }>
  82. { this.props.showAvatar && <Avatar
  83. displayName = { message.displayName }
  84. participantId = { message.id }
  85. size = { styles.avatarWrapper.width } />
  86. }
  87. </View>
  88. );
  89. }
  90. /**
  91. * Renders the display name of the sender.
  92. *
  93. * @returns {React$Element<*>}
  94. */
  95. _renderDisplayName() {
  96. return (
  97. <Text style = { styles.displayName }>
  98. { this.props.message.displayName }
  99. </Text>
  100. );
  101. }
  102. /**
  103. * Renders the message privacy notice.
  104. *
  105. * @returns {React$Element<*>}
  106. */
  107. _renderPrivateNotice() {
  108. return (
  109. <Text style = { styles.privateNotice }>
  110. { this._getPrivateNoticeMessage() }
  111. </Text>
  112. );
  113. }
  114. /**
  115. * Renders the time at which the message was sent.
  116. *
  117. * @returns {React$Element<*>}
  118. */
  119. _renderTimestamp() {
  120. return (
  121. <Text style = { styles.timeText }>
  122. { this._getFormattedTimestamp() }
  123. </Text>
  124. );
  125. }
  126. }
  127. export default translate(ChatMessage);