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.

Chat.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* eslint-disable react/no-multi-comp */
  2. import { Route, useIsFocused } from '@react-navigation/native';
  3. import React, { Component, useEffect } from 'react';
  4. import { connect } from 'react-redux';
  5. import { IReduxState } from '../../../app/types';
  6. import { translate } from '../../../base/i18n/functions';
  7. import JitsiScreen from '../../../base/modal/components/JitsiScreen';
  8. import { TabBarLabelCounter } from '../../../mobile/navigation/components/TabBarLabelCounter';
  9. import { closeChat, sendMessage } from '../../actions.native';
  10. import { IChatProps as AbstractProps } from '../../types';
  11. import ChatInputBar from './ChatInputBar';
  12. import MessageContainer from './MessageContainer';
  13. import MessageRecipient from './MessageRecipient';
  14. import styles from './styles';
  15. interface IProps extends AbstractProps {
  16. /**
  17. * Default prop for navigating between screen components(React Navigation).
  18. */
  19. navigation: any;
  20. /**
  21. * Default prop for navigating between screen components(React Navigation).
  22. */
  23. route: Route<'', { privateMessageRecipient: { name: string; }; }>;
  24. }
  25. /**
  26. * Implements a React native component that renders the chat window (modal) of
  27. * the mobile client.
  28. */
  29. class Chat extends Component<IProps> {
  30. /**
  31. * Initializes a new {@code AbstractChat} instance.
  32. *
  33. * @param {Props} props - The React {@code Component} props to initialize
  34. * the new {@code AbstractChat} instance with.
  35. */
  36. constructor(props: IProps) {
  37. super(props);
  38. // Bind event handlers so they are only bound once per instance.
  39. this._onSendMessage = this._onSendMessage.bind(this);
  40. }
  41. /**
  42. * Implements React's {@link Component#render()}.
  43. *
  44. * @inheritdoc
  45. */
  46. render() {
  47. const { _messages, route } = this.props;
  48. const privateMessageRecipient = route?.params?.privateMessageRecipient;
  49. return (
  50. <JitsiScreen
  51. disableForcedKeyboardDismiss = { true }
  52. /* eslint-disable react/jsx-no-bind */
  53. footerComponent = { () =>
  54. <ChatInputBar onSend = { this._onSendMessage } />
  55. }
  56. hasBottomTextInput = { true }
  57. hasExtraHeaderHeight = { true }
  58. style = { styles.chatContainer }>
  59. {/* @ts-ignore */}
  60. <MessageContainer messages = { _messages } />
  61. <MessageRecipient privateMessageRecipient = { privateMessageRecipient } />
  62. </JitsiScreen>
  63. );
  64. }
  65. /**
  66. * Sends a text message.
  67. *
  68. * @private
  69. * @param {string} text - The text message to be sent.
  70. * @returns {void}
  71. * @type {Function}
  72. */
  73. _onSendMessage(text: string) {
  74. this.props.dispatch(sendMessage(text));
  75. }
  76. }
  77. /**
  78. * Maps (parts of) the redux state to {@link Chat} React {@code Component}
  79. * props.
  80. *
  81. * @param {Object} state - The redux store/state.
  82. * @param {any} _ownProps - Components' own props.
  83. * @private
  84. * @returns {{
  85. * _messages: Array<Object>,
  86. * _nbUnreadMessages: number
  87. * }}
  88. */
  89. function _mapStateToProps(state: IReduxState, _ownProps: any) {
  90. const { messages, nbUnreadMessages } = state['features/chat'];
  91. return {
  92. _messages: messages,
  93. _nbUnreadMessages: nbUnreadMessages
  94. };
  95. }
  96. export default translate(connect(_mapStateToProps)((props: IProps) => {
  97. const { _nbUnreadMessages, dispatch, navigation, t } = props;
  98. const unreadMessagesNr = _nbUnreadMessages > 0;
  99. const isFocused = useIsFocused();
  100. useEffect(() => {
  101. navigation?.setOptions({
  102. tabBarLabel: () => (
  103. <TabBarLabelCounter
  104. activeUnreadNr = { unreadMessagesNr }
  105. isFocused = { isFocused }
  106. label = { t('chat.tabs.chat') }
  107. nbUnread = { _nbUnreadMessages } />
  108. )
  109. });
  110. return () => {
  111. isFocused && dispatch(closeChat());
  112. };
  113. }, [ isFocused, _nbUnreadMessages ]);
  114. return (
  115. <Chat { ...props } />
  116. );
  117. }));