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.

AbstractChat.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { IReduxState, IStore } from '../../app/types';
  4. import { getLocalParticipant } from '../../base/participants/functions';
  5. import { sendMessage, setIsPollsTabFocused } from '../actions';
  6. import { SMALL_WIDTH_THRESHOLD } from '../constants';
  7. /**
  8. * The type of the React {@code Component} props of {@code AbstractChat}.
  9. */
  10. export interface IProps extends WithTranslation {
  11. /**
  12. * Whether the chat is opened in a modal or not (computed based on window width).
  13. */
  14. _isModal: boolean;
  15. /**
  16. * True if the chat window should be rendered.
  17. */
  18. _isOpen: boolean;
  19. /**
  20. * True if the polls feature is enabled.
  21. */
  22. _isPollsEnabled: boolean;
  23. /**
  24. * Whether the poll tab is focused or not.
  25. */
  26. _isPollsTabFocused: boolean;
  27. /**
  28. * All the chat messages in the conference.
  29. */
  30. _messages: Array<Object>;
  31. /**
  32. * Number of unread chat messages.
  33. */
  34. _nbUnreadMessages: number;
  35. /**
  36. * Number of unread poll messages.
  37. */
  38. _nbUnreadPolls: number;
  39. /**
  40. * Function to send a text message.
  41. *
  42. * @protected
  43. */
  44. _onSendMessage: Function;
  45. /**
  46. * Function to toggle the chat window.
  47. */
  48. _onToggleChat: Function;
  49. /**
  50. * Function to display the chat tab.
  51. *
  52. * @protected
  53. */
  54. _onToggleChatTab: Function;
  55. /**
  56. * Function to display the polls tab.
  57. *
  58. * @protected
  59. */
  60. _onTogglePollsTab: Function;
  61. /**
  62. * Whether or not to block chat access with a nickname input form.
  63. */
  64. _showNamePrompt: boolean;
  65. /**
  66. * The Redux dispatch function.
  67. */
  68. dispatch: IStore['dispatch'];
  69. }
  70. /**
  71. * Implements an abstract chat panel.
  72. */
  73. export default class AbstractChat<P extends IProps> extends Component<P> {
  74. /**
  75. * Initializes a new {@code AbstractChat} instance.
  76. *
  77. * @param {Props} props - The React {@code Component} props to initialize
  78. * the new {@code AbstractChat} instance with.
  79. */
  80. constructor(props: P) {
  81. super(props);
  82. // Bind event handlers so they are only bound once per instance.
  83. this._onSendMessage = this._onSendMessage.bind(this);
  84. this._onToggleChatTab = this._onToggleChatTab.bind(this);
  85. this._onTogglePollsTab = this._onTogglePollsTab.bind(this);
  86. }
  87. /**
  88. * Sends a text message.
  89. *
  90. * @private
  91. * @param {string} text - The text message to be sent.
  92. * @returns {void}
  93. * @type {Function}
  94. */
  95. _onSendMessage(text: string) {
  96. this.props.dispatch(sendMessage(text));
  97. }
  98. /**
  99. * Display the Chat tab.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. _onToggleChatTab() {
  105. this.props.dispatch(setIsPollsTabFocused(false));
  106. }
  107. /**
  108. * Display the Polls tab.
  109. *
  110. * @private
  111. * @returns {void}
  112. */
  113. _onTogglePollsTab() {
  114. this.props.dispatch(setIsPollsTabFocused(true));
  115. }
  116. }
  117. /**
  118. * Maps (parts of) the redux state to {@link Chat} React {@code Component}
  119. * props.
  120. *
  121. * @param {Object} state - The redux store/state.
  122. * @private
  123. * @returns {{
  124. * _isOpen: boolean,
  125. * _messages: Array<Object>,
  126. * _showNamePrompt: boolean
  127. * }}
  128. */
  129. export function _mapStateToProps(state: IReduxState) {
  130. const { isOpen, isPollsTabFocused, messages, nbUnreadMessages } = state['features/chat'];
  131. const { nbUnreadPolls } = state['features/polls'];
  132. const _localParticipant = getLocalParticipant(state);
  133. const { disablePolls } = state['features/base/config'];
  134. return {
  135. _isModal: window.innerWidth <= SMALL_WIDTH_THRESHOLD,
  136. _isOpen: isOpen,
  137. _isPollsEnabled: !disablePolls,
  138. _isPollsTabFocused: isPollsTabFocused,
  139. _messages: messages,
  140. _nbUnreadMessages: nbUnreadMessages,
  141. _nbUnreadPolls: nbUnreadPolls,
  142. _showNamePrompt: !_localParticipant?.name
  143. };
  144. }