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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @flow
  2. import React from 'react';
  3. import { FlatList, Text, View } from 'react-native';
  4. import { ScrollView } from 'react-native-gesture-handler';
  5. import { ColorSchemeRegistry } from '../../../base/color-scheme';
  6. import { translate } from '../../../base/i18n';
  7. import { connect } from '../../../base/redux';
  8. import { StyleType } from '../../../base/styles';
  9. import AbstractMessageContainer, { type Props as AbstractProps }
  10. from '../AbstractMessageContainer';
  11. import ChatMessageGroup from './ChatMessageGroup';
  12. import styles from './styles';
  13. type Props = AbstractProps & {
  14. /**
  15. * The color-schemed stylesheet of the feature.
  16. */
  17. _styles: StyleType,
  18. /**
  19. * Function to be used to translate i18n labels.
  20. */
  21. t: Function
  22. };
  23. /**
  24. * Implements a container to render all the chat messages in a conference.
  25. */
  26. class MessageContainer extends AbstractMessageContainer<Props> {
  27. /**
  28. * Instantiates a new instance of the component.
  29. *
  30. * @inheritdoc
  31. */
  32. constructor(props: Props) {
  33. super(props);
  34. this._keyExtractor = this._keyExtractor.bind(this);
  35. this._renderListEmptyComponent = this._renderListEmptyComponent.bind(this);
  36. this._renderMessageGroup = this._renderMessageGroup.bind(this);
  37. }
  38. /**
  39. * Implements {@code Component#render}.
  40. *
  41. * @inheritdoc
  42. */
  43. render() {
  44. const data = this._getMessagesGroupedBySender();
  45. return (
  46. <FlatList
  47. ListEmptyComponent = { this._renderListEmptyComponent }
  48. bounces = { false }
  49. data = { data }
  50. // Workaround for RN bug:
  51. // https://github.com/facebook/react-native/issues/21196
  52. inverted = { Boolean(data.length) }
  53. keyExtractor = { this._keyExtractor }
  54. keyboardShouldPersistTaps = 'always'
  55. renderItem = { this._renderMessageGroup } />
  56. );
  57. }
  58. _getMessagesGroupedBySender: () => Array<Array<Object>>;
  59. _keyExtractor: Object => string;
  60. /**
  61. * Key extractor for the flatlist.
  62. *
  63. * @param {Object} item - The flatlist item that we need the key to be
  64. * generated for.
  65. * @param {number} index - The index of the element.
  66. * @returns {string}
  67. */
  68. _keyExtractor(item, index) {
  69. return `key_${index}`;
  70. }
  71. _renderListEmptyComponent: () => React$Element<any>;
  72. /**
  73. * Renders a message when there are no messages in the chat yet.
  74. *
  75. * @returns {React$Element<any>}
  76. */
  77. _renderListEmptyComponent() {
  78. const { _styles, t } = this.props;
  79. return (
  80. <View style = { styles.emptyComponentWrapper }>
  81. <Text style = { _styles.emptyComponentText }>
  82. { t('chat.noMessagesMessage') }
  83. </Text>
  84. </View>
  85. );
  86. }
  87. _renderMessageGroup: Object => React$Element<any>;
  88. /**
  89. * Renders a single chat message.
  90. *
  91. * @param {Array<Object>} messages - The chat message to render.
  92. * @returns {React$Element<*>}
  93. */
  94. _renderMessageGroup({ item: messages }) {
  95. return (
  96. <ScrollView>
  97. <ChatMessageGroup messages = { messages } />
  98. </ScrollView>
  99. );
  100. }
  101. }
  102. /**
  103. * Maps part of the redux state to the props of this component.
  104. *
  105. * @param {Object} state - The Redux state.
  106. * @returns {Props}
  107. */
  108. function _mapStateToProps(state) {
  109. return {
  110. _styles: ColorSchemeRegistry.get(state, 'Chat')
  111. };
  112. }
  113. export default translate(connect(_mapStateToProps)(MessageContainer));