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.js 3.0KB

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