2018-12-21 10:55:35 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-10-31 18:40:08 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { responsive } from 'react-native-responsive-ui';
|
|
|
|
import equal from 'deep-equal';
|
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
import database, { safeAddListener } from '../../../lib/realm';
|
2019-04-08 12:35:28 +00:00
|
|
|
import Header from './Header';
|
2019-04-17 17:01:03 +00:00
|
|
|
import RightButtons from './RightButtons';
|
2018-10-31 18:40:08 +00:00
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
class RoomHeaderView extends Component {
|
2018-10-31 18:40:08 +00:00
|
|
|
static propTypes = {
|
|
|
|
title: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
2019-04-08 12:35:28 +00:00
|
|
|
prid: PropTypes.string,
|
2019-04-17 17:01:03 +00:00
|
|
|
tmid: PropTypes.string,
|
2019-04-08 12:35:28 +00:00
|
|
|
rid: PropTypes.string,
|
2018-10-31 18:40:08 +00:00
|
|
|
window: PropTypes.object,
|
2019-04-17 17:01:03 +00:00
|
|
|
status: PropTypes.string,
|
2019-04-30 19:31:51 +00:00
|
|
|
connecting: PropTypes.bool,
|
2019-04-29 16:03:52 +00:00
|
|
|
widthOffset: PropTypes.number,
|
|
|
|
isLoggedUser: PropTypes.bool,
|
|
|
|
userId: PropTypes.string
|
2018-10-31 18:40:08 +00:00
|
|
|
};
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.usersTyping = database.memoryDatabase.objects('usersTyping').filtered('rid = $0', props.rid);
|
2019-04-29 16:03:52 +00:00
|
|
|
this.user = [];
|
|
|
|
if (props.type === 'd' && !props.isLoggedUser) {
|
|
|
|
this.user = database.memoryDatabase.objects('activeUsers').filtered('id == $0', props.userId);
|
|
|
|
safeAddListener(this.user, this.updateUser);
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
this.state = {
|
2019-04-29 16:03:52 +00:00
|
|
|
usersTyping: this.usersTyping.slice() || [],
|
|
|
|
user: this.user[0] || {}
|
2019-04-08 12:35:28 +00:00
|
|
|
};
|
|
|
|
this.usersTyping.addListener(this.updateState);
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-04-29 16:03:52 +00:00
|
|
|
const { usersTyping, user } = this.state;
|
2018-12-21 10:55:35 +00:00
|
|
|
const {
|
2019-05-14 20:06:17 +00:00
|
|
|
type, title, status, window, connecting
|
2018-12-21 10:55:35 +00:00
|
|
|
} = this.props;
|
|
|
|
if (nextProps.type !== type) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.title !== title) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.status !== status) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-30 19:31:51 +00:00
|
|
|
if (nextProps.connecting !== connecting) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextProps.window.width !== window.width) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.window.height !== window.height) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
if (!equal(nextState.usersTyping, usersTyping)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-29 16:03:52 +00:00
|
|
|
if (!equal(nextState.user, user)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:46:09 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.usersTyping.removeAllListeners();
|
|
|
|
if (this.user && this.user.removeAllListeners) {
|
|
|
|
this.user.removeAllListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
updateState = () => {
|
|
|
|
this.setState({ usersTyping: this.usersTyping.slice() });
|
2018-10-31 18:40:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
updateUser = () => {
|
|
|
|
if (this.user.length) {
|
|
|
|
this.setState({ user: this.user[0] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:40:08 +00:00
|
|
|
render() {
|
2019-04-29 16:03:52 +00:00
|
|
|
const { usersTyping, user } = this.state;
|
2018-10-31 18:40:08 +00:00
|
|
|
const {
|
2019-05-14 20:06:17 +00:00
|
|
|
window, title, type, prid, tmid, widthOffset, isLoggedUser, status: userStatus, connecting
|
2018-10-31 18:40:08 +00:00
|
|
|
} = this.props;
|
2019-04-29 16:03:52 +00:00
|
|
|
let status = 'offline';
|
|
|
|
|
|
|
|
if (type === 'd') {
|
|
|
|
if (isLoggedUser) {
|
|
|
|
status = userStatus;
|
|
|
|
} else {
|
|
|
|
status = user.status || 'offline';
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 18:40:08 +00:00
|
|
|
|
|
|
|
return (
|
2019-04-08 12:35:28 +00:00
|
|
|
<Header
|
|
|
|
prid={prid}
|
2019-04-17 17:01:03 +00:00
|
|
|
tmid={tmid}
|
2019-04-08 12:35:28 +00:00
|
|
|
title={title}
|
|
|
|
type={type}
|
|
|
|
status={status}
|
|
|
|
width={window.width}
|
|
|
|
height={window.height}
|
|
|
|
usersTyping={usersTyping}
|
2019-04-17 17:01:03 +00:00
|
|
|
widthOffset={widthOffset}
|
2019-04-30 19:31:51 +00:00
|
|
|
connecting={connecting}
|
2019-04-08 12:35:28 +00:00
|
|
|
/>
|
2018-10-31 18:40:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
let status;
|
|
|
|
let userId;
|
|
|
|
let isLoggedUser = false;
|
|
|
|
const { rid, type } = ownProps;
|
|
|
|
if (type === 'd') {
|
|
|
|
if (state.login.user && state.login.user.id) {
|
|
|
|
const { id: loggedUserId } = state.login.user;
|
|
|
|
userId = rid.replace(loggedUserId, '').trim();
|
|
|
|
isLoggedUser = userId === loggedUserId;
|
|
|
|
if (isLoggedUser) {
|
|
|
|
status = state.login.user.status; // eslint-disable-line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
connecting: state.meteor.connecting,
|
|
|
|
userId,
|
|
|
|
isLoggedUser,
|
|
|
|
status
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default responsive(connect(mapStateToProps)(RoomHeaderView));
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
export { RightButtons };
|