2019-04-17 17:01:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import { CustomHeaderButtons, Item } 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';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
class RightButtonsContainer extends React.PureComponent {
|
|
|
|
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,
|
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 = {
|
|
|
|
isFollowingThread: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async componentDidMount() {
|
2019-11-18 22:09:54 +00:00
|
|
|
const { tmid } = this.props;
|
2019-09-16 20:26:32 +00:00
|
|
|
if (tmid) {
|
|
|
|
const db = database.active;
|
2019-11-18 22:09:54 +00:00
|
|
|
try {
|
|
|
|
const threadRecord = await db.collections.get('messages').find(tmid);
|
|
|
|
this.observeThead(threadRecord);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Can\'t find message to observe.');
|
|
|
|
}
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:09:54 +00:00
|
|
|
observeThead = (threadRecord) => {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
goThreadsView = () => {
|
|
|
|
const { rid, t, navigation } = this.props;
|
|
|
|
navigation.navigate('ThreadMessagesView', { rid, t });
|
|
|
|
}
|
|
|
|
|
2019-05-15 19:33:30 +00:00
|
|
|
toggleFollowThread = () => {
|
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() {
|
|
|
|
const { isFollowingThread } = this.state;
|
|
|
|
const { t, tmid, threadsEnabled } = this.props;
|
|
|
|
if (t === 'l') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (tmid) {
|
|
|
|
return (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
<Item
|
|
|
|
title='bell'
|
2020-06-05 13:28:58 +00:00
|
|
|
iconName={isFollowingThread ? 'bell' : 'bell-off'}
|
2019-04-17 17:01:03 +00:00
|
|
|
onPress={this.toggleFollowThread}
|
|
|
|
testID={isFollowingThread ? 'room-view-header-unfollow' : 'room-view-header-follow'}
|
|
|
|
/>
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
{threadsEnabled ? (
|
|
|
|
<Item
|
2020-06-05 13:28:58 +00:00
|
|
|
title='threads'
|
|
|
|
iconName='threads'
|
2019-04-17 17:01:03 +00:00
|
|
|
onPress={this.goThreadsView}
|
|
|
|
testID='room-view-header-threads'
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
userId: getUserSelector(state).id,
|
2019-08-07 13:51:34 +00:00
|
|
|
threadsEnabled: state.settings.Threads_enabled
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RightButtonsContainer);
|