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 4.0KB

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