bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/raisehand-notifier/component.jsx

204 lines
6.2 KiB
React
Raw Normal View History

2020-06-30 10:43:20 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
import { toast } from 'react-toastify';
2022-02-15 22:51:51 +08:00
import Icon from '/imports/ui/components/common/icon/component';
2020-06-30 10:43:20 +08:00
import { ENTER } from '/imports/utils/keyCodes';
2021-11-09 03:54:19 +08:00
import Styled from './styles';
import { Meteor } from 'meteor/meteor';
2022-02-25 01:00:27 +08:00
import TooltipContainer from '/imports/ui/components/common/tooltip/container';
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
2020-06-30 10:43:20 +08:00
const messages = defineMessages({
lowerHandsLabel: {
id: 'app.statusNotifier.lowerHands',
description: 'text displayed to clear all raised hands',
},
2022-02-25 01:00:27 +08:00
lowerHandDescOneUser: {
id: 'app.statusNotifier.lowerHandDescOneUser',
description: 'text displayed to clear a single user raised hands',
},
2020-06-30 10:43:20 +08:00
raisedHandsTitle: {
id: 'app.statusNotifier.raisedHandsTitle',
description: 'heading for raised hands toast',
},
raisedHandDesc: {
id: 'app.statusNotifier.raisedHandDesc',
description: 'label for multiple users with raised hands',
},
raisedHandDescOneUser: {
id: 'app.statusNotifier.raisedHandDescOneUser',
description: 'label for a single user with raised hand',
},
2020-06-30 10:43:20 +08:00
and: {
id: 'app.statusNotifier.and',
description: 'used as conjunction word',
},
});
const MAX_AVATAR_COUNT = 3;
class RaiseHandNotifier extends Component {
2020-06-30 10:43:20 +08:00
constructor(props) {
super(props);
this.statusNotifierId = null;
2020-12-01 00:09:35 +08:00
this.audio = new Audio(`${Meteor.settings.public.app.cdn + Meteor.settings.public.app.basename + Meteor.settings.public.app.instanceId}/resources/sounds/bbb-handRaise.mp3`);
2020-06-30 10:43:20 +08:00
this.renderRaisedHands = this.renderRaisedHands.bind(this);
this.getRaisedHandNames = this.getRaisedHandNames.bind(this);
this.raisedHandAvatars = this.raisedHandAvatars.bind(this);
}
componentDidUpdate(prevProps) {
const {
raiseHandUsers, raiseHandAudioAlert, raiseHandPushAlert, isViewer, isPresenter,
2020-06-30 10:43:20 +08:00
} = this.props;
if (isViewer && !isPresenter) {
if (this.statusNotifierId) toast.dismiss(this.statusNotifierId);
return false;
}
if (raiseHandUsers.length === 0) {
return this.statusNotifierId ? toast.dismiss(this.statusNotifierId) : null;
}
if (raiseHandAudioAlert && raiseHandUsers.length > prevProps.raiseHandUsers.length) {
this.audio.play();
}
if (raiseHandPushAlert) {
if (this.statusNotifierId) {
return toast.update(this.statusNotifierId, {
render: this.renderRaisedHands(),
});
}
this.statusNotifierId = toast(this.renderRaisedHands(), {
onClose: () => { this.statusNotifierId = null; },
autoClose: false,
closeOnClick: false,
closeButton: false,
className: 'raiseHandToast',
});
2020-06-30 10:43:20 +08:00
}
return true;
}
getRaisedHandNames() {
const { raiseHandUsers, intl } = this.props;
if (raiseHandUsers.length === 0) return '';
2020-06-30 10:43:20 +08:00
const _names = raiseHandUsers.map((u) => u.name);
2020-06-30 10:43:20 +08:00
const { length } = _names;
const and = intl.formatMessage(messages.and);
let formattedNames = '';
switch (length) {
case 1:
formattedNames = _names;
break;
case 2:
formattedNames = _names.join(` ${and} `);
break;
case 3:
formattedNames = _names.slice(0, length - 1).join(', ');
formattedNames += ` ${and} ${_names.slice(length - 1)}`;
break;
default:
formattedNames = _names.slice(0, MAX_AVATAR_COUNT).join(', ');
formattedNames += ` ${and} ${length - MAX_AVATAR_COUNT}+ `;
2020-06-30 10:43:20 +08:00
break;
}
const raisedHandMessageString = length === 1
? messages.raisedHandDescOneUser : messages.raisedHandDesc;
return intl.formatMessage(raisedHandMessageString, { 0: formattedNames });
2020-06-30 10:43:20 +08:00
}
raisedHandAvatars() {
const { raiseHandUsers, lowerUserHands, intl } = this.props;
let users = raiseHandUsers;
if (raiseHandUsers.length > MAX_AVATAR_COUNT) users = users.slice(0, MAX_AVATAR_COUNT);
2020-06-30 10:43:20 +08:00
const avatars = users.map((u) => (
2022-02-25 01:00:27 +08:00
<TooltipContainer
2020-06-30 10:43:20 +08:00
key={`statusToastAvatar-${u.userId}`}
title={intl.formatMessage(messages.lowerHandDescOneUser, { 0: u.name })}
>
2022-02-25 01:00:27 +08:00
<Styled.Avatar
role="button"
tabIndex={0}
style={{ backgroundColor: `${u.color}` }}
onClick={() => lowerUserHands(u.userId)}
onKeyDown={(e) => (e.keyCode === ENTER ? lowerUserHands(u.userId) : null)}
2022-02-25 01:00:27 +08:00
data-test="avatarsWrapperAvatar"
moderator={u.role === ROLE_MODERATOR}
avatar={u.avatar}
2022-02-25 01:00:27 +08:00
>
{u.name.slice(0, 2)}
</Styled.Avatar>
</TooltipContainer>
2020-06-30 10:43:20 +08:00
));
if (raiseHandUsers.length > MAX_AVATAR_COUNT) {
2020-06-30 10:43:20 +08:00
avatars.push(
<Styled.AvatarsExtra key={`statusToastAvatar-${raiseHandUsers.length}`}>
{raiseHandUsers.length}
2021-11-09 03:54:19 +08:00
</Styled.AvatarsExtra>,
2020-06-30 10:43:20 +08:00
);
}
return avatars;
}
renderRaisedHands() {
const { raiseHandUsers, intl, lowerUserHands } = this.props;
2020-06-30 10:43:20 +08:00
const formattedRaisedHands = this.getRaisedHandNames();
return (
<div>
2021-11-09 03:54:19 +08:00
<Styled.ToastIcon>
<Styled.IconWrapper>
2020-06-30 10:43:20 +08:00
<Icon iconName="hand" />
2021-11-09 03:54:19 +08:00
</Styled.IconWrapper>
</Styled.ToastIcon>
<Styled.AvatarsWrapper data-test="avatarsWrapper">
2020-06-30 10:43:20 +08:00
{this.raisedHandAvatars()}
2021-11-09 03:54:19 +08:00
</Styled.AvatarsWrapper>
<Styled.ToastMessage>
2020-06-30 10:43:20 +08:00
<div>{intl.formatMessage(messages.raisedHandsTitle)}</div>
{formattedRaisedHands}
2021-11-09 03:54:19 +08:00
</Styled.ToastMessage>
2021-11-11 19:47:39 +08:00
<Styled.ToastSeparator />
2021-11-09 03:54:19 +08:00
<Styled.ClearButton
2020-06-30 10:43:20 +08:00
label={intl.formatMessage(messages.lowerHandsLabel)}
color="default"
size="md"
onClick={() => {
raiseHandUsers.map((u) => lowerUserHands(u.userId));
2020-06-30 10:43:20 +08:00
}}
2023-02-04 04:04:45 +08:00
data-test="raiseHandRejection"
2020-06-30 10:43:20 +08:00
/>
</div>
);
}
render() { return null; }
}
export default injectIntl(RaiseHandNotifier);
2020-06-30 10:43:20 +08:00
RaiseHandNotifier.propTypes = {
2020-06-30 10:43:20 +08:00
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
lowerUserHands: PropTypes.func.isRequired,
raiseHandUsers: PropTypes.instanceOf(Array).isRequired,
2020-06-30 10:43:20 +08:00
raiseHandAudioAlert: PropTypes.bool.isRequired,
raiseHandPushAlert: PropTypes.bool.isRequired,
};