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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { Component } from 'react';
  2. import { FlatList } from 'react-native';
  3. import { MESSAGE_TYPE_LOCAL, MESSAGE_TYPE_REMOTE } from '../../constants';
  4. import { IMessage } from '../../types';
  5. import ChatMessage from './ChatMessage';
  6. interface IProps {
  7. /**
  8. * The messages array to render.
  9. */
  10. messages: Array<IMessage>;
  11. }
  12. /**
  13. * Implements a container to render all the chat messages in a conference.
  14. */
  15. export default class ChatMessageGroup extends Component<IProps> {
  16. /**
  17. * Instantiates a new instance of the component.
  18. *
  19. * @inheritdoc
  20. */
  21. constructor(props: IProps) {
  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. /**
  41. * Key extractor for the flatlist.
  42. *
  43. * @param {Object} _item - The flatlist item that we need the key to be
  44. * generated for.
  45. * @param {number} index - The index of the element.
  46. * @returns {string}
  47. */
  48. _keyExtractor(_item: Object, index: number) {
  49. return `key_${index}`;
  50. }
  51. /**
  52. * Renders a single chat message.
  53. *
  54. * @param {Object} message - The chat message to render.
  55. * @returns {React$Element<*>}
  56. */
  57. _renderMessage({ index, item: message }: { index: number; item: IMessage; }) {
  58. return (
  59. <ChatMessage
  60. message = { message }
  61. showAvatar = {
  62. this.props.messages[0].messageType !== MESSAGE_TYPE_LOCAL
  63. && index === this.props.messages.length - 1
  64. }
  65. showDisplayName = {
  66. this.props.messages[0].messageType === MESSAGE_TYPE_REMOTE
  67. && index === this.props.messages.length - 1
  68. }
  69. showTimestamp = { index === 0 } />
  70. );
  71. }
  72. }