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.

ChatButton.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @flow
  2. import { CHAT_ENABLED, getFeatureFlag } from '../../../base/flags';
  3. import { IconChat, IconChatUnread } from '../../../base/icons';
  4. import { setActiveModalId } from '../../../base/modal';
  5. import { getLocalParticipant } from '../../../base/participants';
  6. import { connect } from '../../../base/redux';
  7. import {
  8. AbstractButton,
  9. type AbstractButtonProps
  10. } from '../../../base/toolbox';
  11. import { openDisplayNamePrompt } from '../../../display-name';
  12. import { CHAT_VIEW_MODAL_ID } from '../../constants';
  13. import { getUnreadCount } from '../../functions';
  14. type Props = AbstractButtonProps & {
  15. /**
  16. * Function to display chat.
  17. *
  18. * @protected
  19. */
  20. _displayChat: Function,
  21. /**
  22. * Function to diaply the name prompt before displaying the chat
  23. * window, if the user has no display name set.
  24. */
  25. _displayNameInputDialog: Function,
  26. /**
  27. * Whether or not to block chat access with a nickname input form.
  28. */
  29. _showNamePrompt: boolean,
  30. /**
  31. * The unread message count.
  32. */
  33. _unreadMessageCount: number
  34. };
  35. /**
  36. * Implements an {@link AbstractButton} to open the chat screen on mobile.
  37. */
  38. class ChatButton extends AbstractButton<Props, *> {
  39. accessibilityLabel = 'toolbar.accessibilityLabel.chat';
  40. icon = IconChat;
  41. label = 'toolbar.chat';
  42. toggledIcon = IconChatUnread;
  43. /**
  44. * Handles clicking / pressing the button, and opens the appropriate dialog.
  45. *
  46. * @private
  47. * @returns {void}
  48. */
  49. _handleClick() {
  50. if (this.props._showNamePrompt) {
  51. this.props._displayNameInputDialog(() => {
  52. this.props._displayChat();
  53. });
  54. } else {
  55. this.props._displayChat();
  56. }
  57. }
  58. /**
  59. * Renders the button toggled when there are unread messages.
  60. *
  61. * @protected
  62. * @returns {boolean}
  63. */
  64. _isToggled() {
  65. return Boolean(this.props._unreadMessageCount);
  66. }
  67. }
  68. /**
  69. * Maps redux actions to the props of the component.
  70. *
  71. * @param {Function} dispatch - The redux action {@code dispatch} function.
  72. * @returns {{
  73. * _displayChat,
  74. * _displayNameInputDialog
  75. * }}
  76. * @private
  77. */
  78. function _mapDispatchToProps(dispatch: Function) {
  79. return {
  80. /**
  81. * Launches native invite dialog.
  82. *
  83. * @private
  84. * @returns {void}
  85. */
  86. _displayChat() {
  87. dispatch(setActiveModalId(CHAT_VIEW_MODAL_ID));
  88. },
  89. /**
  90. * Displays a display name prompt.
  91. *
  92. * @param {Function} onPostSubmit - The function to invoke after a
  93. * succesfulsetting of the display name.
  94. * @returns {void}
  95. */
  96. _displayNameInputDialog(onPostSubmit) {
  97. dispatch(openDisplayNamePrompt(onPostSubmit));
  98. }
  99. };
  100. }
  101. /**
  102. * Maps part of the redux state to the component's props.
  103. *
  104. * @param {Object} state - The Redux state.
  105. * @param {Object} ownProps - The properties explicitly passed to the component instance.
  106. * @returns {Props}
  107. */
  108. function _mapStateToProps(state, ownProps) {
  109. const localParticipant = getLocalParticipant(state);
  110. const enabled = getFeatureFlag(state, CHAT_ENABLED, true);
  111. const { visible = enabled } = ownProps;
  112. return {
  113. _showNamePrompt: !localParticipant.name,
  114. _unreadMessageCount: getUnreadCount(state),
  115. visible
  116. };
  117. }
  118. export default connect(_mapStateToProps, _mapDispatchToProps)(ChatButton);