2020-10-30 17:35:07 +00:00
|
|
|
import React, { Component } from 'react';
|
2019-04-17 17:01:03 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../../containers/HeaderButton';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../../lib/database';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../../selectors/login';
|
2020-08-05 13:15:56 +00:00
|
|
|
import { logEvent, events } from '../../../utils/log';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
class RightButtonsContainer extends Component {
|
2019-04-17 17:01:03 +00:00
|
|
|
static propTypes = {
|
|
|
|
userId: PropTypes.string,
|
|
|
|
threadsEnabled: PropTypes.bool,
|
|
|
|
rid: PropTypes.string,
|
|
|
|
t: PropTypes.string,
|
|
|
|
tmid: PropTypes.string,
|
2019-05-15 19:33:30 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: PropTypes.bool,
|
2019-11-25 20:01:17 +00:00
|
|
|
toggleFollowThread: PropTypes.func
|
2019-04-17 17:01:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2020-10-30 17:35:07 +00:00
|
|
|
isFollowingThread: true,
|
|
|
|
tunread: [],
|
|
|
|
tunreadUser: [],
|
|
|
|
tunreadGroup: []
|
2019-04-17 17:01:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async componentDidMount() {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { tmid, rid } = this.props;
|
|
|
|
const db = database.active;
|
2019-09-16 20:26:32 +00:00
|
|
|
if (tmid) {
|
2019-11-18 22:09:54 +00:00
|
|
|
try {
|
2021-02-26 16:25:51 +00:00
|
|
|
const threadRecord = await db.get('messages').find(tmid);
|
2020-10-30 17:35:07 +00:00
|
|
|
this.observeThread(threadRecord);
|
2019-11-18 22:09:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log('Can\'t find message to observe.');
|
|
|
|
}
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
2020-10-30 17:35:07 +00:00
|
|
|
if (rid) {
|
|
|
|
try {
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2020-10-30 17:35:07 +00:00
|
|
|
const subRecord = await subCollection.find(rid);
|
|
|
|
this.observeSubscription(subRecord);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Can\'t find subscription to observe.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const {
|
|
|
|
isFollowingThread, tunread, tunreadUser, tunreadGroup
|
|
|
|
} = this.state;
|
|
|
|
if (nextState.isFollowingThread !== isFollowingThread) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextState.tunread, tunread)) {
|
2020-10-30 17:35:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextState.tunreadUser, tunreadUser)) {
|
2020-10-30 17:35:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextState.tunreadGroup, tunreadGroup)) {
|
2020-10-30 17:35:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-02-23 17:44:01 +00:00
|
|
|
return false;
|
2019-05-15 19:33:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 13:33:38 +00:00
|
|
|
componentWillUnmount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.threadSubscription && this.threadSubscription.unsubscribe) {
|
|
|
|
this.threadSubscription.unsubscribe();
|
2019-05-03 13:33:38 +00:00
|
|
|
}
|
2020-10-30 17:35:07 +00:00
|
|
|
if (this.subSubscription && this.subSubscription.unsubscribe) {
|
|
|
|
this.subSubscription.unsubscribe();
|
|
|
|
}
|
2019-05-03 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
observeThread = (threadRecord) => {
|
2019-11-18 22:09:54 +00:00
|
|
|
const threadObservable = threadRecord.observe();
|
|
|
|
this.threadSubscription = threadObservable
|
|
|
|
.subscribe(thread => this.updateThread(thread));
|
|
|
|
}
|
|
|
|
|
|
|
|
updateThread = (thread) => {
|
2019-04-17 17:01:03 +00:00
|
|
|
const { userId } = this.props;
|
|
|
|
this.setState({
|
2019-11-18 22:09:54 +00:00
|
|
|
isFollowingThread: thread.replies && !!thread.replies.find(t => t === userId)
|
2019-04-17 17:01:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
observeSubscription = (subRecord) => {
|
|
|
|
const subObservable = subRecord.observe();
|
|
|
|
this.subSubscription = subObservable
|
|
|
|
.subscribe((sub) => {
|
|
|
|
this.updateSubscription(sub);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSubscription = (sub) => {
|
|
|
|
this.setState({
|
|
|
|
tunread: sub?.tunread,
|
|
|
|
tunreadUser: sub?.tunreadUser,
|
|
|
|
tunreadGroup: sub?.tunreadGroup
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
goThreadsView = () => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.ROOM_GO_THREADS);
|
2020-06-15 14:00:46 +00:00
|
|
|
const {
|
|
|
|
rid, t, navigation, isMasterDetail
|
|
|
|
} = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('ModalStackNavigator', { screen: 'ThreadMessagesView', params: { rid, t } });
|
|
|
|
} else {
|
|
|
|
navigation.navigate('ThreadMessagesView', { rid, t });
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 20:56:28 +00:00
|
|
|
goSearchView = () => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.ROOM_GO_SEARCH);
|
2020-07-06 20:56:28 +00:00
|
|
|
const {
|
|
|
|
rid, navigation, isMasterDetail
|
|
|
|
} = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('ModalStackNavigator', { screen: 'SearchMessagesView', params: { rid, showCloseModal: true } });
|
|
|
|
} else {
|
|
|
|
navigation.navigate('SearchMessagesView', { rid });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 19:33:30 +00:00
|
|
|
toggleFollowThread = () => {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.ROOM_TOGGLE_FOLLOW_THREADS);
|
2019-04-17 17:01:03 +00:00
|
|
|
const { isFollowingThread } = this.state;
|
2019-05-15 19:33:30 +00:00
|
|
|
const { toggleFollowThread } = this.props;
|
|
|
|
if (toggleFollowThread) {
|
|
|
|
toggleFollowThread(isFollowingThread);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-10-30 17:35:07 +00:00
|
|
|
const {
|
|
|
|
isFollowingThread, tunread, tunreadUser, tunreadGroup
|
|
|
|
} = this.state;
|
2019-04-17 17:01:03 +00:00
|
|
|
const { t, tmid, threadsEnabled } = this.props;
|
|
|
|
if (t === 'l') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (tmid) {
|
|
|
|
return (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item
|
2020-07-27 19:53:33 +00:00
|
|
|
iconName={isFollowingThread ? 'notification' : 'notification-disabled'}
|
2019-04-17 17:01:03 +00:00
|
|
|
onPress={this.toggleFollowThread}
|
|
|
|
testID={isFollowingThread ? 'room-view-header-unfollow' : 'room-view-header-follow'}
|
|
|
|
/>
|
2020-10-30 16:15:58 +00:00
|
|
|
</HeaderButton.Container>
|
2019-04-17 17:01:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
2019-04-17 17:01:03 +00:00
|
|
|
{threadsEnabled ? (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Item
|
2020-06-05 13:28:58 +00:00
|
|
|
iconName='threads'
|
2019-04-17 17:01:03 +00:00
|
|
|
onPress={this.goThreadsView}
|
|
|
|
testID='room-view-header-threads'
|
2020-10-30 17:35:07 +00:00
|
|
|
badge={() => (
|
|
|
|
<HeaderButton.Badge
|
|
|
|
tunread={tunread}
|
|
|
|
tunreadUser={tunreadUser}
|
|
|
|
tunreadGroup={tunreadGroup}
|
|
|
|
/>
|
|
|
|
)}
|
2019-04-17 17:01:03 +00:00
|
|
|
/>
|
|
|
|
) : null}
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Item
|
2020-07-27 19:53:33 +00:00
|
|
|
iconName='search'
|
2020-07-06 20:56:28 +00:00
|
|
|
onPress={this.goSearchView}
|
|
|
|
testID='room-view-search'
|
|
|
|
/>
|
2020-10-30 16:15:58 +00:00
|
|
|
</HeaderButton.Container>
|
2019-04-17 17:01:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
userId: getUserSelector(state).id,
|
2020-06-15 14:00:46 +00:00
|
|
|
threadsEnabled: state.settings.Threads_enabled,
|
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RightButtonsContainer);
|