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.2KB

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