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 2.8KB

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