verdnatura-chat/app/views/JitsiMeetView.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import JitsiMeet, { JitsiMeetView as RNJitsiMeetView } from 'react-native-jitsi-meet';
import BackgroundTimer from 'react-native-background-timer';
2020-02-19 19:43:47 +00:00
import { connect } from 'react-redux';
import RocketChat from '../lib/rocketchat';
2020-02-19 19:43:47 +00:00
import { getUserSelector } from '../selectors/login';
import sharedStyles from './Styles';
2020-02-19 19:43:47 +00:00
const formatUrl = (url, baseUrl, uriSize, avatarAuthURLFragment) => (
`${ baseUrl }/avatar/${ url }?format=png&width=${ uriSize }&height=${ uriSize }${ avatarAuthURLFragment }`
);
class JitsiMeetView extends React.Component {
static propTypes = {
2020-02-19 19:43:47 +00:00
navigation: PropTypes.object,
baseUrl: PropTypes.string,
user: PropTypes.shape({
id: PropTypes.string,
username: PropTypes.string,
name: PropTypes.string,
token: PropTypes.string
})
}
constructor(props) {
super(props);
this.rid = props.navigation.getParam('rid');
this.onConferenceTerminated = this.onConferenceTerminated.bind(this);
this.onConferenceJoined = this.onConferenceJoined.bind(this);
this.jitsiTimeout = null;
}
componentDidMount() {
2020-02-19 19:43:47 +00:00
const { navigation, user, baseUrl } = this.props;
const {
name: displayName, id: userId, token, username
} = user;
const avatarAuthURLFragment = `&rc_token=${ token }&rc_uid=${ userId }`;
const avatar = formatUrl(username, baseUrl, 100, avatarAuthURLFragment);
setTimeout(() => {
2020-02-19 19:43:47 +00:00
const userInfo = {
displayName,
avatar
};
const url = navigation.getParam('url');
const onlyAudio = navigation.getParam('onlyAudio', false);
if (onlyAudio) {
2020-02-19 19:43:47 +00:00
JitsiMeet.audioCall(url, userInfo);
} else {
2020-02-19 19:43:47 +00:00
JitsiMeet.call(url, userInfo);
}
}, 1000);
}
2019-12-11 23:01:12 +00:00
componentWillUnmount() {
if (this.jitsiTimeout) {
BackgroundTimer.clearInterval(this.jitsiTimeout);
}
JitsiMeet.endCall();
}
// Jitsi Update Timeout needs to be called every 10 seconds to make sure
// call is not ended and is available to web users.
onConferenceJoined = () => {
RocketChat.updateJitsiTimeout(this.rid).catch(e => console.log(e));
if (this.jitsiTimeout) {
BackgroundTimer.clearInterval(this.jitsiTimeout);
}
this.jitsiTimeout = BackgroundTimer.setInterval(() => {
RocketChat.updateJitsiTimeout(this.rid).catch(e => console.log(e));
}, 10000);
}
onConferenceTerminated = () => {
const { navigation } = this.props;
if (this.jitsiTimeout) {
BackgroundTimer.clearInterval(this.jitsiTimeout);
}
navigation.pop();
}
render() {
return (
<RNJitsiMeetView
onConferenceTerminated={this.onConferenceTerminated}
onConferenceJoined={this.onConferenceJoined}
2019-12-11 23:01:12 +00:00
style={sharedStyles.container}
/>
);
}
}
2020-02-19 19:43:47 +00:00
const mapStateToProps = state => ({
user: getUserSelector(state),
baseUrl: state.server.server
});
export default connect(mapStateToProps)(JitsiMeetView);