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.

OverflowMenuProfileItem.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Avatar } from '../../../base/avatar';
  4. import { translate } from '../../../base/i18n';
  5. import { getLocalParticipant } from '../../../base/participants';
  6. import { connect } from '../../../base/redux';
  7. declare var interfaceConfig: Object;
  8. /**
  9. * The type of the React {@code Component} props of
  10. * {@link OverflowMenuProfileItem}.
  11. */
  12. type Props = {
  13. /**
  14. * The redux representation of the local participant.
  15. */
  16. _localParticipant: Object,
  17. /**
  18. * Whether the button support clicking or not.
  19. */
  20. _unclickable: boolean,
  21. /**
  22. * The callback to invoke when {@code OverflowMenuProfileItem} is
  23. * clicked.
  24. */
  25. onClick: Function,
  26. /**
  27. * Invoked to obtain translated strings.
  28. */
  29. t: Function
  30. };
  31. /**
  32. * A React {@code Component} for displaying a link with a profile avatar as an
  33. * icon.
  34. *
  35. * @extends Component
  36. */
  37. class OverflowMenuProfileItem extends Component<Props> {
  38. /**
  39. * Initializes a new {@code OverflowMenuProfileItem} instance.
  40. *
  41. * @param {Object} props - The read-only properties with which the new
  42. * instance is to be initialized.
  43. */
  44. constructor(props: Props) {
  45. super(props);
  46. // Bind event handler so it is only bound once for every instance.
  47. this._onClick = this._onClick.bind(this);
  48. this._onKeyPress = this._onKeyPress.bind(this);
  49. }
  50. /**
  51. * Implements React's {@link Component#render()}.
  52. *
  53. * @inheritdoc
  54. * @returns {ReactElement}
  55. */
  56. render() {
  57. const { _localParticipant, _unclickable, t } = this.props;
  58. const classNames = `overflow-menu-item ${
  59. _unclickable ? 'unclickable' : ''}`;
  60. let displayName;
  61. if (_localParticipant && _localParticipant.name) {
  62. displayName = _localParticipant.name;
  63. } else {
  64. displayName = interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME;
  65. }
  66. return (
  67. <li
  68. aria-label = { t('toolbar.accessibilityLabel.profile') }
  69. className = { classNames }
  70. onClick = { this._onClick }
  71. onKeyPress = { this._onKeyPress }
  72. role = 'menuitem'
  73. tabIndex = { 0 }>
  74. <span className = 'overflow-menu-item-icon'>
  75. <Avatar
  76. participantId = { _localParticipant.id }
  77. size = { 20 } />
  78. </span>
  79. <span className = 'profile-text'>
  80. { displayName }
  81. </span>
  82. </li>
  83. );
  84. }
  85. _onClick: () => void;
  86. /**
  87. * Invokes an on click callback if clicking is allowed.
  88. *
  89. * @returns {void}
  90. */
  91. _onClick() {
  92. if (!this.props._unclickable) {
  93. this.props.onClick();
  94. }
  95. }
  96. _onKeyPress: (Object) => void;
  97. /**
  98. * KeyPress handler for accessibility.
  99. *
  100. * @param {Object} e - The key event to handle.
  101. *
  102. * @returns {void}
  103. */
  104. _onKeyPress(e) {
  105. if (!this.props._unclickable && (e.key === ' ' || e.key === 'Enter')) {
  106. e.preventDefault();
  107. this.props.onClick();
  108. }
  109. }
  110. }
  111. /**
  112. * Maps (parts of) the Redux state to the associated
  113. * {@code OverflowMenuProfileItem} component's props.
  114. *
  115. * @param {Object} state - The Redux state.
  116. * @private
  117. * @returns {{
  118. * _localParticipant: Object,
  119. * _unclickable: boolean
  120. * }}
  121. */
  122. function _mapStateToProps(state) {
  123. return {
  124. _localParticipant: getLocalParticipant(state),
  125. _unclickable: state['features/base/config'].disableProfile
  126. || !interfaceConfig.SETTINGS_SECTIONS.includes('profile')
  127. };
  128. }
  129. export default translate(connect(_mapStateToProps)(OverflowMenuProfileItem));