2018-10-30 05:02:23 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-07-31 23:33:22 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
2018-05-16 15:03:10 +00:00
|
|
|
import { translate } from '../../base/i18n';
|
2017-07-31 23:33:22 +00:00
|
|
|
import { getParticipantById } from '../../base/participants';
|
2018-06-26 22:56:22 +00:00
|
|
|
import { Text } from '../../base/react';
|
2019-03-21 16:38:29 +00:00
|
|
|
import { connect } from '../../base/redux';
|
2018-05-16 15:03:10 +00:00
|
|
|
import { STATUS_TO_I18N_KEY } from '../constants';
|
2020-01-14 09:41:16 +00:00
|
|
|
import { presenceStatusDisabled } from '../functions';
|
2018-05-16 15:03:10 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link PresenceLabel}.
|
|
|
|
*/
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current present status associated with the passed in participantID
|
|
|
|
* prop.
|
|
|
|
*/
|
|
|
|
_presence: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class name for the presence label.
|
|
|
|
*/
|
|
|
|
className: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default presence status that will be displayed if user's presence status
|
|
|
|
* is not available.
|
|
|
|
*/
|
|
|
|
defaultPresence: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the participant whose presence status should display.
|
|
|
|
*/
|
|
|
|
participantID: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Styles for the presence label.
|
|
|
|
*/
|
|
|
|
style: Object,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
|
|
|
t: Function
|
|
|
|
};
|
|
|
|
|
2017-07-31 23:33:22 +00:00
|
|
|
/**
|
|
|
|
* React {@code Component} for displaying the current presence status of a
|
|
|
|
* participant.
|
|
|
|
*
|
|
|
|
* @extends Component
|
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
class PresenceLabel extends Component<Props> {
|
2017-07-31 23:33:22 +00:00
|
|
|
/**
|
|
|
|
* The default values for {@code PresenceLabel} component's property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
_presence: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2018-07-09 22:18:09 +00:00
|
|
|
const text = this._getPresenceText();
|
|
|
|
|
|
|
|
if (text === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { style, className } = this.props;
|
2017-07-31 23:33:22 +00:00
|
|
|
|
|
|
|
return (
|
2018-07-09 22:18:09 +00:00
|
|
|
<Text
|
|
|
|
className = { className }
|
|
|
|
{ ...style }>
|
|
|
|
{ text }
|
|
|
|
</Text>);
|
2017-07-31 23:33:22 +00:00
|
|
|
}
|
2018-05-16 15:03:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the text associated with the current presence status.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_getPresenceText() {
|
|
|
|
const { _presence, t } = this.props;
|
|
|
|
|
|
|
|
if (!_presence) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const i18nKey = STATUS_TO_I18N_KEY[_presence];
|
|
|
|
|
|
|
|
if (!i18nKey) { // fallback to status value
|
|
|
|
return _presence;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t(i18nKey);
|
|
|
|
}
|
2017-07-31 23:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps (parts of) the Redux state to the associated {@code PresenceLabel}'s
|
|
|
|
* props.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @param {Object} ownProps - The React Component props passed to the associated
|
|
|
|
* instance of {@code PresenceLabel}.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* _presence: (string|undefined)
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
function _mapStateToProps(state, ownProps) {
|
2018-05-22 22:41:53 +00:00
|
|
|
const participant = getParticipantById(state, ownProps.participantID);
|
2017-07-31 23:33:22 +00:00
|
|
|
|
|
|
|
return {
|
2020-01-14 09:41:16 +00:00
|
|
|
_presence: presenceStatusDisabled() ? ''
|
|
|
|
: participant?.presence || ownProps.defaultPresence
|
|
|
|
|
2017-07-31 23:33:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:03:10 +00:00
|
|
|
export default translate(connect(_mapStateToProps)(PresenceLabel));
|