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

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