2019-06-07 21:49:50 +08:00
|
|
|
import React, { Component } from 'react';
|
2019-09-19 04:06:59 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2019-06-07 21:49:50 +08:00
|
|
|
import { Session } from 'meteor/session';
|
|
|
|
import { withModalMounter } from '/imports/ui/components/modal/service';
|
|
|
|
import BreakoutJoinConfirmation from '/imports/ui/components/breakout-join-confirmation/container';
|
2021-02-09 01:40:03 +08:00
|
|
|
import BreakoutService from '../service';
|
2019-06-07 21:49:50 +08:00
|
|
|
|
2019-09-19 04:06:59 +08:00
|
|
|
const BREAKOUT_MODAL_DELAY = 200;
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
mountModal: PropTypes.func.isRequired,
|
2021-10-02 00:37:37 +08:00
|
|
|
currentBreakoutUrlData: PropTypes.shape({
|
2019-09-19 04:06:59 +08:00
|
|
|
insertedTime: PropTypes.number.isRequired,
|
|
|
|
}),
|
|
|
|
breakoutUserIsIn: PropTypes.shape({
|
|
|
|
sequence: PropTypes.number.isRequired,
|
|
|
|
}),
|
|
|
|
breakouts: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
freeJoin: PropTypes.bool.isRequired,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
2021-10-02 00:37:37 +08:00
|
|
|
currentBreakoutUrlData: undefined,
|
2019-09-19 04:06:59 +08:00
|
|
|
breakoutUserIsIn: undefined,
|
|
|
|
breakouts: [],
|
|
|
|
};
|
|
|
|
|
2019-06-07 21:49:50 +08:00
|
|
|
const openBreakoutJoinConfirmation = (breakout, breakoutName, mountModal) => mountModal(
|
|
|
|
<BreakoutJoinConfirmation
|
|
|
|
breakout={breakout}
|
|
|
|
breakoutName={breakoutName}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
class BreakoutRoomInvitation extends Component {
|
2019-09-19 04:06:59 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
didSendBreakoutInvite: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
// use dummy old data on mount so it works if no data changes
|
|
|
|
this.checkBreakouts({ breakouts: [] });
|
|
|
|
}
|
|
|
|
|
2019-06-07 21:49:50 +08:00
|
|
|
componentDidUpdate(oldProps) {
|
2019-09-19 04:06:59 +08:00
|
|
|
this.checkBreakouts(oldProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkBreakouts(oldProps) {
|
2019-06-07 21:49:50 +08:00
|
|
|
const {
|
|
|
|
breakouts,
|
2021-10-01 21:44:01 +08:00
|
|
|
currentBreakoutUrlData,
|
|
|
|
getBreakoutByUrlData,
|
2019-09-19 04:06:59 +08:00
|
|
|
breakoutUserIsIn,
|
2019-06-07 21:49:50 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2019-09-19 04:06:59 +08:00
|
|
|
const {
|
|
|
|
didSendBreakoutInvite,
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
const hasBreakouts = breakouts.length > 0;
|
2019-06-07 21:49:50 +08:00
|
|
|
|
2021-02-09 01:40:03 +08:00
|
|
|
if (hasBreakouts && !breakoutUserIsIn && BreakoutService.checkInviteModerators()) {
|
2021-12-02 22:05:42 +08:00
|
|
|
const freeJoinRooms = breakouts.filter((breakout) => breakout.freeJoin);
|
|
|
|
|
|
|
|
if (currentBreakoutUrlData) {
|
|
|
|
const breakoutRoom = getBreakoutByUrlData(currentBreakoutUrlData);
|
2021-10-01 21:44:01 +08:00
|
|
|
const currentInsertedTime = currentBreakoutUrlData.insertedTime;
|
2021-10-02 00:37:37 +08:00
|
|
|
const oldCurrentUrlData = oldProps.currentBreakoutUrlData || {};
|
|
|
|
const oldInsertedTime = oldCurrentUrlData.insertedTime;
|
2019-09-19 04:06:59 +08:00
|
|
|
if (currentInsertedTime !== oldInsertedTime) {
|
2021-12-02 22:05:42 +08:00
|
|
|
const lastBreakoutId = Session.get('lastBreakoutOpened');
|
|
|
|
if (breakoutRoom.breakoutId !== lastBreakoutId) {
|
2019-09-19 04:06:59 +08:00
|
|
|
this.inviteUserToBreakout(breakoutRoom);
|
|
|
|
}
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
2021-12-02 22:05:42 +08:00
|
|
|
} else if (freeJoinRooms.length > 0 && !didSendBreakoutInvite) {
|
|
|
|
const maxSeq = Math.max(...freeJoinRooms.map(((room) => room.sequence)));
|
|
|
|
// Check if received all rooms and Pick a room randomly
|
|
|
|
if (maxSeq === freeJoinRooms.length) {
|
|
|
|
const randomRoom = freeJoinRooms[Math.floor(Math.random() * freeJoinRooms.length)];
|
|
|
|
this.inviteUserToBreakout(randomRoom);
|
|
|
|
this.setState({ didSendBreakoutInvite: true });
|
|
|
|
}
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 04:06:59 +08:00
|
|
|
|
|
|
|
if (!hasBreakouts && didSendBreakoutInvite) {
|
|
|
|
this.setState({ didSendBreakoutInvite: false });
|
|
|
|
}
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inviteUserToBreakout(breakout) {
|
|
|
|
const {
|
|
|
|
mountModal,
|
|
|
|
} = this.props;
|
2019-09-19 04:06:59 +08:00
|
|
|
// There's a race condition on page load with modals. Only one modal can be shown at a
|
|
|
|
// time and new ones overwrite old ones. We delay the opening of the breakout modal
|
|
|
|
// because it should always be on top if breakouts are running.
|
|
|
|
setTimeout(() => {
|
|
|
|
openBreakoutJoinConfirmation.call(this, breakout, breakout.name, mountModal);
|
|
|
|
}, BREAKOUT_MODAL_DELAY);
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:06:59 +08:00
|
|
|
BreakoutRoomInvitation.propTypes = propTypes;
|
|
|
|
BreakoutRoomInvitation.defaultProps = defaultProps;
|
|
|
|
|
2019-06-07 21:49:50 +08:00
|
|
|
export default withModalMounter(BreakoutRoomInvitation);
|