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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // @flow
  2. import React from 'react';
  3. import { Text, View } from 'react-native';
  4. import { getLocalizedDateFormatter, translate } from '../../../base/i18n';
  5. import { Avatar } from '../../../base/participants';
  6. import { connect } from '../../../base/redux';
  7. import AbstractChatMessage, {
  8. _mapStateToProps,
  9. type Props
  10. } from '../AbstractChatMessage';
  11. import styles from './styles';
  12. /**
  13. * Size of the rendered avatar in the message.
  14. */
  15. const AVATAR_SIZE = 32;
  16. /**
  17. * Formatter string to display the message timestamp.
  18. */
  19. const TIMESTAMP_FORMAT = 'H:mm';
  20. /**
  21. * Renders a single chat message.
  22. */
  23. class ChatMessage extends AbstractChatMessage<Props> {
  24. /**
  25. * Implements {@code Component#render}.
  26. *
  27. * @inheritdoc
  28. */
  29. render() {
  30. const { message } = this.props;
  31. const timeStamp = getLocalizedDateFormatter(
  32. new Date(message.timestamp)).format(TIMESTAMP_FORMAT);
  33. const localMessage = message.messageType === 'local';
  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 textWrapperStyle = [
  40. styles.textWrapper
  41. ];
  42. if (localMessage) {
  43. // The wrapper needs to be aligned to the right.
  44. detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
  45. // The bubble needs to be differently styled.
  46. textWrapperStyle.push(styles.ownTextWrapper);
  47. } else if (message.messageType === 'error') {
  48. // The bubble needs to be differently styled.
  49. textWrapperStyle.push(styles.systemTextWrapper);
  50. }
  51. return (
  52. <View style = { styles.messageWrapper } >
  53. {
  54. // Avatar is only rendered for remote messages.
  55. !localMessage && this._renderAvatar()
  56. }
  57. <View style = { detailsWrapperStyle }>
  58. <View style = { textWrapperStyle } >
  59. {
  60. // Display name is only rendered for remote
  61. // messages.
  62. !localMessage && this._renderDisplayName()
  63. }
  64. <Text style = { styles.messageText }>
  65. { message.messageType === 'error'
  66. ? this.props.t('chat.error', {
  67. error: message.error,
  68. originalText: message.message
  69. })
  70. : message.message }
  71. </Text>
  72. </View>
  73. <Text style = { styles.timeText }>
  74. { timeStamp }
  75. </Text>
  76. </View>
  77. </View>
  78. );
  79. }
  80. /**
  81. * Renders the avatar of the sender.
  82. *
  83. * @returns {React$Element<*>}
  84. */
  85. _renderAvatar() {
  86. const { _avatarURL } = this.props;
  87. return (
  88. <View style = { styles.avatarWrapper }>
  89. <Avatar
  90. size = { AVATAR_SIZE }
  91. uri = { _avatarURL } />
  92. </View>
  93. );
  94. }
  95. /**
  96. * Renders the display name of the sender.
  97. *
  98. * @returns {React$Element<*>}
  99. */
  100. _renderDisplayName() {
  101. const { message } = this.props;
  102. return (
  103. <Text style = { styles.displayName }>
  104. { message.displayName }
  105. </Text>
  106. );
  107. }
  108. }
  109. export default translate(connect(_mapStateToProps)(ChatMessage));