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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 styles from './styles';
  10. /**
  11. * Renders a single chat message.
  12. */
  13. class ChatMessage extends AbstractChatMessage<Props> {
  14. /**
  15. * Implements {@code Component#render}.
  16. *
  17. * @inheritdoc
  18. */
  19. render() {
  20. const { message } = this.props;
  21. const localMessage = message.messageType === 'local';
  22. // Style arrays that need to be updated in various scenarios, such as
  23. // error messages or others.
  24. const detailsWrapperStyle = [
  25. styles.detailsWrapper
  26. ];
  27. const textWrapperStyle = [
  28. styles.textWrapper
  29. ];
  30. if (localMessage) {
  31. // The wrapper needs to be aligned to the right.
  32. detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
  33. // The bubble needs to be differently styled.
  34. textWrapperStyle.push(styles.ownTextWrapper);
  35. } else if (message.messageType === 'error') {
  36. // The bubble needs to be differently styled.
  37. textWrapperStyle.push(styles.systemTextWrapper);
  38. }
  39. const messageText = message.messageType === 'error'
  40. ? this.props.t('chat.error', {
  41. error: message.error,
  42. originalText: message.message
  43. })
  44. : message.message;
  45. return (
  46. <View style = { styles.messageWrapper } >
  47. { this._renderAvatar() }
  48. <View style = { detailsWrapperStyle }>
  49. <View style = { textWrapperStyle } >
  50. {
  51. this.props.showDisplayName
  52. && this._renderDisplayName()
  53. }
  54. <Linkify linkStyle = { styles.chatLink }>
  55. { replaceNonUnicodeEmojis(messageText) }
  56. </Linkify>
  57. </View>
  58. { this.props.showTimestamp && this._renderTimestamp() }
  59. </View>
  60. </View>
  61. );
  62. }
  63. _getFormattedTimestamp: () => string;
  64. /**
  65. * Renders the avatar of the sender.
  66. *
  67. * @returns {React$Element<*>}
  68. */
  69. _renderAvatar() {
  70. const { message } = this.props;
  71. return (
  72. <View style = { styles.avatarWrapper }>
  73. { this.props.showAvatar && <Avatar
  74. displayName = { message.displayName }
  75. participantId = { message.id }
  76. size = { styles.avatarWrapper.width } />
  77. }
  78. </View>
  79. );
  80. }
  81. /**
  82. * Renders the display name of the sender.
  83. *
  84. * @returns {React$Element<*>}
  85. */
  86. _renderDisplayName() {
  87. return (
  88. <Text style = { styles.displayName }>
  89. { this.props.message.displayName }
  90. </Text>
  91. );
  92. }
  93. /**
  94. * Renders the time at which the message was sent.
  95. *
  96. * @returns {React$Element<*>}
  97. */
  98. _renderTimestamp() {
  99. return (
  100. <Text style = { styles.timeText }>
  101. { this._getFormattedTimestamp() }
  102. </Text>
  103. );
  104. }
  105. }
  106. export default translate(ChatMessage);