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
|
|
|
|
2021-04-07 18:31:25 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import { logEvent, events } from '../../utils/log';
|
2021-05-12 19:01:29 +00:00
|
|
|
import { isTeamRoom } from '../../utils/room';
|
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,
|
2021-05-12 19:01:29 +00:00
|
|
|
teamId: PropTypes.string,
|
2019-05-15 19:33:30 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: PropTypes.bool,
|
2021-05-12 19:01:29 +00:00
|
|
|
toggleFollowThread: PropTypes.func,
|
|
|
|
joined: PropTypes.bool
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:31:25 +00:00
|
|
|
goTeamChannels = () => {
|
|
|
|
logEvent(events.ROOM_GO_TEAM_CHANNELS);
|
|
|
|
const {
|
|
|
|
navigation, isMasterDetail, teamId
|
|
|
|
} = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('ModalStackNavigator', {
|
|
|
|
screen: 'TeamChannelsView',
|
|
|
|
params: { teamId }
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
navigation.navigate('TeamChannelsView', { teamId });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-26 17:24:54 +00:00
|
|
|
rid, t, navigation, isMasterDetail
|
2020-07-06 20:56:28 +00:00
|
|
|
} = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('ModalStackNavigator', { screen: 'SearchMessagesView', params: { rid, showCloseModal: true } });
|
|
|
|
} else {
|
2021-05-26 17:24:54 +00:00
|
|
|
navigation.navigate('SearchMessagesView', { rid, t });
|
2020-07-06 20:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2021-04-07 18:31:25 +00:00
|
|
|
const {
|
2021-05-12 19:01:29 +00:00
|
|
|
t, tmid, threadsEnabled, teamId, joined
|
2021-04-07 18:31:25 +00:00
|
|
|
} = this.props;
|
2019-04-17 17:01:03 +00:00
|
|
|
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>
|
2021-05-12 19:01:29 +00:00
|
|
|
{isTeamRoom({ teamId, joined }) ? (
|
2021-04-07 18:31:25 +00:00
|
|
|
<HeaderButton.Item
|
|
|
|
iconName='channel-public'
|
|
|
|
onPress={this.goTeamChannels}
|
|
|
|
testID='room-view-header-team-channels'
|
|
|
|
/>
|
|
|
|
) : null}
|
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);
|