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 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // @flow
  2. import React from 'react';
  3. import AbstractMessageContainer, { type Props }
  4. from '../AbstractMessageContainer';
  5. import ChatMessageGroup from './ChatMessageGroup';
  6. /**
  7. * Displays all received chat messages, grouped by sender.
  8. *
  9. * @extends AbstractMessageContainer
  10. */
  11. export default class MessageContainer extends AbstractMessageContainer<Props> {
  12. /**
  13. * Reference to the HTML element at the end of the list of displayed chat
  14. * messages. Used for scrolling to the end of the chat messages.
  15. */
  16. _messagesListEndRef: Object;
  17. /**
  18. * Initializes a new {@code MessageContainer} instance.
  19. *
  20. * @param {Props} props - The React {@code Component} props to initialize
  21. * the new {@code MessageContainer} instance with.
  22. */
  23. constructor(props: Props) {
  24. super(props);
  25. this._messagesListEndRef = React.createRef();
  26. }
  27. /**
  28. * Implements React's {@link Component#componentDidMount()}.
  29. *
  30. * @inheritdoc
  31. */
  32. componentDidMount() {
  33. this._scrollMessagesToBottom();
  34. }
  35. /**
  36. * Updates chat input focus.
  37. *
  38. * @inheritdoc
  39. */
  40. componentDidUpdate() {
  41. this._scrollMessagesToBottom();
  42. }
  43. /**
  44. * Implements {@code Component#render}.
  45. *
  46. * @inheritdoc
  47. */
  48. render() {
  49. const groupedMessages = this._getMessagesGroupedBySender();
  50. const messages = groupedMessages.map((group, index) => {
  51. const messageType = group[0] && group[0].messageType;
  52. return (
  53. <ChatMessageGroup
  54. className = { messageType || 'remote' }
  55. key = { index }
  56. messages = { group } />
  57. );
  58. });
  59. return (
  60. <div id = 'chatconversation'>
  61. { messages }
  62. <div ref = { this._messagesListEndRef } />
  63. </div>
  64. );
  65. }
  66. _getMessagesGroupedBySender: () => Array<Array<Object>>;
  67. /**
  68. * Automatically scrolls the displayed chat messages down to the latest.
  69. *
  70. * @private
  71. * @returns {void}
  72. */
  73. _scrollMessagesToBottom() {
  74. this._messagesListEndRef.current.scrollIntoView({
  75. behavior: 'smooth'
  76. });
  77. }
  78. }