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.

MessageContainer.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // @flow
  2. import React from 'react';
  3. import { MESSAGE_TYPE_REMOTE } from '../../constants';
  4. import AbstractMessageContainer, { type Props }
  5. from '../AbstractMessageContainer';
  6. import ChatMessageGroup from './ChatMessageGroup';
  7. /**
  8. * Displays all received chat messages, grouped by sender.
  9. *
  10. * @extends AbstractMessageContainer
  11. */
  12. export default class MessageContainer extends AbstractMessageContainer {
  13. /**
  14. * Whether or not chat has been scrolled to the bottom of the screen. Used
  15. * to determine if chat should be scrolled automatically to the bottom when
  16. * the {@code ChatInput} resizes.
  17. */
  18. _isScrolledToBottom: boolean;
  19. /**
  20. * Reference to the HTML element at the end of the list of displayed chat
  21. * messages. Used for scrolling to the end of the chat messages.
  22. */
  23. _messagesListEndRef: Object;
  24. /**
  25. * A React ref to the HTML element containing all {@code ChatMessageGroup}
  26. * instances.
  27. */
  28. _messageListRef: Object;
  29. /**
  30. * Initializes a new {@code MessageContainer} instance.
  31. *
  32. * @param {Props} props - The React {@code Component} props to initialize
  33. * the new {@code MessageContainer} instance with.
  34. */
  35. constructor(props: Props) {
  36. super(props);
  37. this._isScrolledToBottom = true;
  38. this._messageListRef = React.createRef();
  39. this._messagesListEndRef = React.createRef();
  40. this._onChatScroll = this._onChatScroll.bind(this);
  41. }
  42. /**
  43. * Implements {@code Component#render}.
  44. *
  45. * @inheritdoc
  46. */
  47. render() {
  48. const groupedMessages = this._getMessagesGroupedBySender();
  49. const messages = groupedMessages.map((group, index) => {
  50. const messageType = group[0] && group[0].messageType;
  51. return (
  52. <ChatMessageGroup
  53. className = { messageType || MESSAGE_TYPE_REMOTE }
  54. key = { index }
  55. messages = { group } />
  56. );
  57. });
  58. return (
  59. <div
  60. id = 'chatconversation'
  61. onScroll = { this._onChatScroll }
  62. ref = { this._messageListRef }>
  63. { messages }
  64. <div ref = { this._messagesListEndRef } />
  65. </div>
  66. );
  67. }
  68. /**
  69. * Scrolls to the bottom again if the instance had previously been scrolled
  70. * to the bottom. This method is used when a resize has occurred below the
  71. * instance and bottom scroll needs to be maintained.
  72. *
  73. * @returns {void}
  74. */
  75. maybeUpdateBottomScroll() {
  76. if (this._isScrolledToBottom) {
  77. this.scrollToBottom(false);
  78. }
  79. }
  80. /**
  81. * Automatically scrolls the displayed chat messages down to the latest.
  82. *
  83. * @param {boolean} withAnimation - Whether or not to show a scrolling
  84. * animation.
  85. * @returns {void}
  86. */
  87. scrollToBottom(withAnimation: boolean) {
  88. this._messagesListEndRef.current.scrollIntoView({
  89. behavior: withAnimation ? 'smooth' : 'auto',
  90. block: 'nearest'
  91. });
  92. }
  93. _getMessagesGroupedBySender: () => Array<Array<Object>>;
  94. _onChatScroll: () => void;
  95. /**
  96. * Callback invoked to listen to the current scroll location.
  97. *
  98. * @private
  99. * @returns {void}
  100. */
  101. _onChatScroll() {
  102. const element = this._messageListRef.current;
  103. this._isScrolledToBottom
  104. = element.scrollHeight - element.scrollTop === element.clientHeight;
  105. }
  106. }