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.

PresenceLabel.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState } from '../../app/types';
  5. import { translate } from '../../base/i18n/functions';
  6. import { getParticipantById } from '../../base/participants/functions';
  7. import { Text } from '../../base/react/components/index';
  8. import { STATUS_TO_I18N_KEY } from '../constants';
  9. import { presenceStatusDisabled } from '../functions';
  10. /**
  11. * The type of the React {@code Component} props of {@link PresenceLabel}.
  12. */
  13. interface IProps extends WithTranslation {
  14. /**
  15. * The current present status associated with the passed in participantID
  16. * prop.
  17. */
  18. _presence: string;
  19. /**
  20. * Class name for the presence label.
  21. */
  22. className: string;
  23. /**
  24. * Default presence status that will be displayed if user's presence status
  25. * is not available.
  26. */
  27. defaultPresence: string;
  28. /**
  29. * The ID of the participant whose presence status should display.
  30. */
  31. participantID: string;
  32. /**
  33. * Styles for the presence label.
  34. */
  35. style: Object;
  36. }
  37. /**
  38. * React {@code Component} for displaying the current presence status of a
  39. * participant.
  40. *
  41. * @augments Component
  42. */
  43. class PresenceLabel extends Component<IProps> {
  44. /**
  45. * The default values for {@code PresenceLabel} component's property types.
  46. *
  47. * @static
  48. */
  49. static defaultProps = {
  50. _presence: ''
  51. };
  52. /**
  53. * Implements React's {@link Component#render()}.
  54. *
  55. * @inheritdoc
  56. * @returns {ReactElement}
  57. */
  58. render() {
  59. const text = this._getPresenceText();
  60. if (text === null) {
  61. return null;
  62. }
  63. const { style, className } = this.props;
  64. return (
  65. <Text // @ts-ignore
  66. className = { className }
  67. { ...style }>
  68. { text }
  69. </Text>
  70. );
  71. }
  72. /**
  73. * Returns the text associated with the current presence status.
  74. *
  75. * @returns {string}
  76. */
  77. _getPresenceText() {
  78. const { _presence, t } = this.props;
  79. if (!_presence) {
  80. return null;
  81. }
  82. const i18nKey = STATUS_TO_I18N_KEY[_presence as keyof typeof STATUS_TO_I18N_KEY];
  83. if (!i18nKey) { // fallback to status value
  84. return _presence;
  85. }
  86. return t(i18nKey);
  87. }
  88. }
  89. /**
  90. * Maps (parts of) the Redux state to the associated {@code PresenceLabel}'s
  91. * props.
  92. *
  93. * @param {Object} state - The Redux state.
  94. * @param {Object} ownProps - The React Component props passed to the associated
  95. * instance of {@code PresenceLabel}.
  96. * @private
  97. * @returns {{
  98. * _presence: (string|undefined)
  99. * }}
  100. */
  101. function _mapStateToProps(state: IReduxState, ownProps: any) {
  102. const participant = getParticipantById(state, ownProps.participantID);
  103. return {
  104. _presence: presenceStatusDisabled() ? ''
  105. : participant?.presence || ownProps.defaultPresence
  106. };
  107. }
  108. export default translate(connect(_mapStateToProps)(PresenceLabel));