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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 {
  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 {@code Component#render}.
  29. *
  30. * @inheritdoc
  31. */
  32. render() {
  33. const groupedMessages = this._getMessagesGroupedBySender();
  34. const messages = groupedMessages.map((group, index) => {
  35. const messageType = group[0] && group[0].messageType;
  36. return (
  37. <ChatMessageGroup
  38. className = { messageType || 'remote' }
  39. key = { index }
  40. messages = { group } />
  41. );
  42. });
  43. return (
  44. <div id = 'chatconversation'>
  45. { messages }
  46. <div ref = { this._messagesListEndRef } />
  47. </div>
  48. );
  49. }
  50. /**
  51. * Automatically scrolls the displayed chat messages down to the latest.
  52. *
  53. * @param {boolean} withAnimation - Whether or not to show a scrolling
  54. * animation.
  55. * @returns {void}
  56. */
  57. scrollToBottom(withAnimation: boolean) {
  58. this._messagesListEndRef.current.scrollIntoView({
  59. behavior: withAnimation ? 'smooth' : 'auto'
  60. });
  61. }
  62. _getMessagesGroupedBySender: () => Array<Array<Object>>;
  63. }