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.

ChatMessageGroup.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { FlatList } from 'react-native';
  4. import { MESSAGE_TYPE_LOCAL, MESSAGE_TYPE_REMOTE } from '../../constants';
  5. import ChatMessage from './ChatMessage';
  6. type Props = {
  7. /**
  8. * The messages array to render.
  9. */
  10. messages: Array<Object>
  11. }
  12. /**
  13. * Implements a container to render all the chat messages in a conference.
  14. */
  15. export default class ChatMessageGroup extends Component<Props> {
  16. /**
  17. * Instantiates a new instance of the component.
  18. *
  19. * @inheritdoc
  20. */
  21. constructor(props: Props) {
  22. super(props);
  23. this._keyExtractor = this._keyExtractor.bind(this);
  24. this._renderMessage = this._renderMessage.bind(this);
  25. }
  26. /**
  27. * Implements {@code Component#render}.
  28. *
  29. * @inheritdoc
  30. */
  31. render() {
  32. return (
  33. <FlatList
  34. data = { this.props.messages }
  35. inverted = { true }
  36. keyExtractor = { this._keyExtractor }
  37. renderItem = { this._renderMessage } />
  38. );
  39. }
  40. _keyExtractor: Object => string;
  41. /**
  42. * Key extractor for the flatlist.
  43. *
  44. * @param {Object} item - The flatlist item that we need the key to be
  45. * generated for.
  46. * @param {number} index - The index of the element.
  47. * @returns {string}
  48. */
  49. _keyExtractor(item, index) {
  50. return `key_${index}`;
  51. }
  52. _renderMessage: Object => React$Element<*>;
  53. /**
  54. * Renders a single chat message.
  55. *
  56. * @param {Object} message - The chat message to render.
  57. * @returns {React$Element<*>}
  58. */
  59. _renderMessage({ index, item: message }) {
  60. return (
  61. <ChatMessage
  62. message = { message }
  63. showAvatar = {
  64. this.props.messages[0].messageType !== MESSAGE_TYPE_LOCAL
  65. && index === this.props.messages.length - 1
  66. }
  67. showDisplayName = {
  68. this.props.messages[0].messageType === MESSAGE_TYPE_REMOTE
  69. && index === this.props.messages.length - 1
  70. }
  71. showTimestamp = { index === 0 } />
  72. );
  73. }
  74. }