您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MessageContainer.js 3.4KB

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