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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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<Props> {
  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. aria-labelledby = 'chat-header'
  61. id = 'chatconversation'
  62. onScroll = { this._onChatScroll }
  63. ref = { this._messageListRef }
  64. role = 'log'
  65. tabIndex = { 0 }>
  66. { messages }
  67. <div ref = { this._messagesListEndRef } />
  68. </div>
  69. );
  70. }
  71. /**
  72. * Scrolls to the bottom again if the instance had previously been scrolled
  73. * to the bottom. This method is used when a resize has occurred below the
  74. * instance and bottom scroll needs to be maintained.
  75. *
  76. * @returns {void}
  77. */
  78. maybeUpdateBottomScroll() {
  79. if (this._isScrolledToBottom) {
  80. this.scrollToBottom(false);
  81. }
  82. }
  83. /**
  84. * Automatically scrolls the displayed chat messages down to the latest.
  85. *
  86. * @param {boolean} withAnimation - Whether or not to show a scrolling
  87. * animation.
  88. * @returns {void}
  89. */
  90. scrollToBottom(withAnimation: boolean) {
  91. this._messagesListEndRef.current.scrollIntoView({
  92. behavior: withAnimation ? 'smooth' : 'auto',
  93. block: 'nearest'
  94. });
  95. }
  96. _getMessagesGroupedBySender: () => Array<Array<Object>>;
  97. _onChatScroll: () => void;
  98. /**
  99. * Callback invoked to listen to the current scroll location.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. _onChatScroll() {
  105. const element = this._messageListRef.current;
  106. this._isScrolledToBottom
  107. = element.scrollHeight - element.scrollTop === element.clientHeight;
  108. }
  109. }