您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatMessage.js 3.6KB

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