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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 AbstractChatMessage, { type Props } from '../AbstractChatMessage';
  8. import styles from './styles';
  9. /**
  10. * Renders a single chat message.
  11. */
  12. class ChatMessage extends AbstractChatMessage<Props> {
  13. /**
  14. * Implements {@code Component#render}.
  15. *
  16. * @inheritdoc
  17. */
  18. render() {
  19. const { message } = this.props;
  20. const localMessage = message.messageType === 'local';
  21. // Style arrays that need to be updated in various scenarios, such as
  22. // error messages or others.
  23. const detailsWrapperStyle = [
  24. styles.detailsWrapper
  25. ];
  26. const textWrapperStyle = [
  27. styles.textWrapper
  28. ];
  29. if (localMessage) {
  30. // The wrapper needs to be aligned to the right.
  31. detailsWrapperStyle.push(styles.ownMessageDetailsWrapper);
  32. // The bubble needs to be differently styled.
  33. textWrapperStyle.push(styles.ownTextWrapper);
  34. } else if (message.messageType === 'error') {
  35. // The bubble needs to be differently styled.
  36. textWrapperStyle.push(styles.systemTextWrapper);
  37. }
  38. const messageText = message.messageType === 'error'
  39. ? this.props.t('chat.error', {
  40. error: message.error,
  41. originalText: message.message
  42. })
  43. : message.message;
  44. return (
  45. <View style = { styles.messageWrapper } >
  46. { this._renderAvatar() }
  47. <View style = { detailsWrapperStyle }>
  48. <View style = { textWrapperStyle } >
  49. {
  50. this.props.showDisplayName
  51. && this._renderDisplayName()
  52. }
  53. <Linkify linkStyle = { styles.chatLink }>
  54. { messageText }
  55. </Linkify>
  56. </View>
  57. { this.props.showTimestamp && this._renderTimestamp() }
  58. </View>
  59. </View>
  60. );
  61. }
  62. _getFormattedTimestamp: () => string;
  63. /**
  64. * Renders the avatar of the sender.
  65. *
  66. * @returns {React$Element<*>}
  67. */
  68. _renderAvatar() {
  69. const { message } = this.props;
  70. return (
  71. <View style = { styles.avatarWrapper }>
  72. { this.props.showAvatar && <Avatar
  73. displayName = { message.displayName }
  74. participantId = { message.id }
  75. size = { styles.avatarWrapper.width } />
  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(ChatMessage);