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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. message.createdAt).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.system) {
  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.text }
  66. </Text>
  67. </View>
  68. <Text style = { styles.timeText }>
  69. { timeStamp }
  70. </Text>
  71. </View>
  72. </View>
  73. );
  74. }
  75. /**
  76. * Renders the avatar of the sender.
  77. *
  78. * @returns {React$Element<*>}
  79. */
  80. _renderAvatar() {
  81. const { _avatarURL } = this.props;
  82. return (
  83. <View style = { styles.avatarWrapper }>
  84. <Avatar
  85. size = { AVATAR_SIZE }
  86. uri = { _avatarURL } />
  87. </View>
  88. );
  89. }
  90. /**
  91. * Renders the display name of the sender.
  92. *
  93. * @returns {React$Element<*>}
  94. */
  95. _renderDisplayName() {
  96. const { message } = this.props;
  97. return (
  98. <Text style = { styles.displayName }>
  99. { message.user.name }
  100. </Text>
  101. );
  102. }
  103. }
  104. export default translate(connect(_mapStateToProps)(ChatMessage));