2019-03-27 20:06:57 +00:00
|
|
|
import React from 'react';
|
2021-06-15 20:13:42 +00:00
|
|
|
import { RefreshControl } from 'react-native';
|
2019-03-27 20:06:57 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2020-02-05 13:27:06 +00:00
|
|
|
import moment from 'moment';
|
2021-03-15 20:16:34 +00:00
|
|
|
import { dequal } from 'dequal';
|
2021-06-15 20:13:42 +00:00
|
|
|
import { Value, event } from 'react-native-reanimated';
|
|
|
|
|
|
|
|
import database from '../../../lib/database';
|
|
|
|
import RocketChat from '../../../lib/rocketchat';
|
|
|
|
import log from '../../../utils/log';
|
|
|
|
import EmptyRoom from '../EmptyRoom';
|
|
|
|
import { animateNextTransition } from '../../../utils/layoutAnimation';
|
|
|
|
import ActivityIndicator from '../../../containers/ActivityIndicator';
|
|
|
|
import { themes } from '../../../constants/colors';
|
2021-10-01 18:12:19 +00:00
|
|
|
import debounce from '../../../utils/debounce';
|
2021-06-15 20:13:42 +00:00
|
|
|
import List from './List';
|
|
|
|
import NavBottomFAB from './NavBottomFAB';
|
2019-03-27 20:06:57 +00:00
|
|
|
|
2020-07-20 16:44:54 +00:00
|
|
|
const QUERY_SIZE = 50;
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
const onScroll = ({ y }) =>
|
|
|
|
event(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
nativeEvent: {
|
|
|
|
contentOffset: { y }
|
|
|
|
}
|
2021-06-15 20:13:42 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
],
|
|
|
|
{ useNativeDriver: true }
|
|
|
|
);
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
class ListContainer extends React.Component {
|
2019-03-27 20:06:57 +00:00
|
|
|
static propTypes = {
|
|
|
|
renderRow: PropTypes.func,
|
2019-04-08 12:35:28 +00:00
|
|
|
rid: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
tmid: PropTypes.string,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2020-05-08 16:37:49 +00:00
|
|
|
loading: PropTypes.bool,
|
2020-02-17 19:06:18 +00:00
|
|
|
listRef: PropTypes.func,
|
2020-02-20 20:43:56 +00:00
|
|
|
hideSystemMessages: PropTypes.array,
|
2020-11-12 14:17:32 +00:00
|
|
|
tunread: PropTypes.array,
|
2020-11-30 20:00:31 +00:00
|
|
|
ignored: PropTypes.array,
|
2020-10-30 17:35:07 +00:00
|
|
|
navigation: PropTypes.object,
|
|
|
|
showMessageInMainThread: PropTypes.bool
|
2019-03-27 20:06:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2021-10-01 18:12:19 +00:00
|
|
|
console.time(`${this.constructor.name} init`);
|
|
|
|
console.time(`${this.constructor.name} mount`);
|
2020-07-20 16:44:54 +00:00
|
|
|
this.count = 0;
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = false;
|
2020-12-17 12:56:53 +00:00
|
|
|
this.animated = false;
|
2021-06-15 20:13:42 +00:00
|
|
|
this.jumping = false;
|
2019-03-27 20:06:57 +00:00
|
|
|
this.state = {
|
2020-02-05 13:27:06 +00:00
|
|
|
messages: [],
|
2021-06-15 20:13:42 +00:00
|
|
|
refreshing: false,
|
|
|
|
highlightedMessage: null
|
2019-03-27 20:06:57 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
this.y = new Value(0);
|
|
|
|
this.onScroll = onScroll({ y: this.y });
|
2020-07-20 16:44:54 +00:00
|
|
|
this.query();
|
2020-06-15 14:00:46 +00:00
|
|
|
this.unsubscribeFocus = props.navigation.addListener('focus', () => {
|
2020-12-17 12:56:53 +00:00
|
|
|
this.animated = true;
|
2020-02-17 19:06:18 +00:00
|
|
|
});
|
2021-06-15 20:13:42 +00:00
|
|
|
this.viewabilityConfig = {
|
|
|
|
itemVisiblePercentThreshold: 10
|
|
|
|
};
|
2021-10-01 18:12:19 +00:00
|
|
|
console.timeEnd(`${this.constructor.name} init`);
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
componentDidMount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = true;
|
2021-10-01 18:12:19 +00:00
|
|
|
console.timeEnd(`${this.constructor.name} mount`);
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { refreshing, highlightedMessage } = this.state;
|
2021-10-01 18:12:19 +00:00
|
|
|
const { hideSystemMessages, theme, tunread, ignored, loading } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (theme !== nextProps.theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-15 20:13:42 +00:00
|
|
|
if (loading !== nextProps.loading) {
|
2019-09-16 20:26:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-15 20:13:42 +00:00
|
|
|
if (highlightedMessage !== nextState.highlightedMessage) {
|
2019-09-16 20:26:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-02-05 13:27:06 +00:00
|
|
|
if (refreshing !== nextState.refreshing) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-15 20:16:34 +00:00
|
|
|
if (!dequal(hideSystemMessages, nextProps.hideSystemMessages)) {
|
2020-03-06 14:19:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-15 20:16:34 +00:00
|
|
|
if (!dequal(tunread, nextProps.tunread)) {
|
2020-11-12 14:17:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-15 20:16:34 +00:00
|
|
|
if (!dequal(ignored, nextProps.ignored)) {
|
2020-11-30 20:00:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-06 14:19:03 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { hideSystemMessages } = this.props;
|
2021-03-15 20:16:34 +00:00
|
|
|
if (!dequal(hideSystemMessages, prevProps.hideSystemMessages)) {
|
2020-03-06 14:19:03 +00:00
|
|
|
this.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
componentWillUnmount() {
|
2019-11-21 19:10:55 +00:00
|
|
|
this.unsubscribeMessages();
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.onEndReached && this.onEndReached.stop) {
|
|
|
|
this.onEndReached.stop();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
if (this.unsubscribeFocus) {
|
|
|
|
this.unsubscribeFocus();
|
2020-02-17 19:06:18 +00:00
|
|
|
}
|
2021-06-15 20:13:42 +00:00
|
|
|
this.clearHighlightedMessageTimeout();
|
2021-10-01 18:12:19 +00:00
|
|
|
console.countReset(`${this.constructor.name}.render calls`);
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 20:13:42 +00:00
|
|
|
clearHighlightedMessageTimeout = () => {
|
|
|
|
if (this.highlightedMessageTimeout) {
|
|
|
|
clearTimeout(this.highlightedMessageTimeout);
|
|
|
|
this.highlightedMessageTimeout = false;
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-07-20 16:44:54 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
query = async () => {
|
2020-07-20 16:44:54 +00:00
|
|
|
this.count += QUERY_SIZE;
|
2020-10-30 17:35:07 +00:00
|
|
|
const { rid, tmid, showMessageInMainThread } = this.props;
|
2020-07-20 16:44:54 +00:00
|
|
|
const db = database.active;
|
|
|
|
|
|
|
|
// handle servers with version < 3.0.0
|
|
|
|
let { hideSystemMessages = [] } = this.props;
|
|
|
|
if (!Array.isArray(hideSystemMessages)) {
|
|
|
|
hideSystemMessages = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmid) {
|
|
|
|
try {
|
2021-10-01 18:12:19 +00:00
|
|
|
this.thread = await db.collections.get('threads').find(tmid);
|
2020-07-20 16:44:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('thread_messages')
|
2021-10-01 18:12:19 +00:00
|
|
|
.query(Q.where('rid', tmid), Q.experimentalSortBy('ts', Q.desc), Q.experimentalSkip(0), Q.experimentalTake(this.count))
|
2020-07-20 16:44:54 +00:00
|
|
|
.observe();
|
|
|
|
} else if (rid) {
|
2020-10-30 17:35:07 +00:00
|
|
|
const whereClause = [
|
|
|
|
Q.where('rid', rid),
|
|
|
|
Q.experimentalSortBy('ts', Q.desc),
|
|
|
|
Q.experimentalSkip(0),
|
|
|
|
Q.experimentalTake(this.count)
|
|
|
|
];
|
|
|
|
if (!showMessageInMainThread) {
|
2021-10-01 18:12:19 +00:00
|
|
|
whereClause.push(Q.or(Q.where('tmid', null), Q.where('tshow', Q.eq(true))));
|
2020-10-30 17:35:07 +00:00
|
|
|
}
|
2020-07-20 16:44:54 +00:00
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('messages')
|
2020-10-30 17:35:07 +00:00
|
|
|
.query(...whereClause)
|
2020-07-20 16:44:54 +00:00
|
|
|
.observe();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rid) {
|
|
|
|
this.unsubscribeMessages();
|
2021-10-01 18:12:19 +00:00
|
|
|
this.messagesSubscription = this.messagesObservable.subscribe(messages => {
|
|
|
|
if (tmid && this.thread) {
|
|
|
|
messages = [...messages, this.thread];
|
|
|
|
}
|
|
|
|
messages = messages.filter(m => !m.t || !hideSystemMessages?.includes(m.t));
|
2020-07-20 16:44:54 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ messages }, () => this.update());
|
|
|
|
} else {
|
|
|
|
this.state.messages = messages;
|
|
|
|
}
|
|
|
|
// TODO: move it away from here
|
|
|
|
this.readThreads();
|
|
|
|
});
|
2020-07-20 16:44:54 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-07-20 16:44:54 +00:00
|
|
|
|
|
|
|
reload = () => {
|
|
|
|
this.count = 0;
|
|
|
|
this.query();
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-07-20 16:44:54 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
readThreads = debounce(async () => {
|
2020-07-20 16:44:54 +00:00
|
|
|
const { tmid } = this.props;
|
|
|
|
|
|
|
|
if (tmid) {
|
|
|
|
try {
|
|
|
|
await RocketChat.readThreads(tmid);
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
}, 300);
|
2020-06-09 20:09:18 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
onEndReached = () => this.query();
|
2020-06-09 20:09:18 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
onRefresh = () =>
|
|
|
|
this.setState({ refreshing: true }, async () => {
|
|
|
|
const { messages } = this.state;
|
|
|
|
const { rid, tmid } = this.props;
|
2020-02-05 13:27:06 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
if (messages.length) {
|
|
|
|
try {
|
|
|
|
if (tmid) {
|
|
|
|
await RocketChat.loadThreadMessages({ tmid, rid });
|
|
|
|
} else {
|
|
|
|
await RocketChat.loadMissedMessages({ rid, lastOpen: moment().subtract(7, 'days').toDate() });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2020-02-05 13:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
this.setState({ refreshing: false });
|
|
|
|
});
|
2020-02-05 13:27:06 +00:00
|
|
|
|
2019-11-07 19:53:39 +00:00
|
|
|
update = () => {
|
2020-12-17 12:56:53 +00:00
|
|
|
if (this.animated) {
|
2020-02-17 19:06:18 +00:00
|
|
|
animateNextTransition();
|
|
|
|
}
|
2019-11-07 19:53:39 +00:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2019-11-21 19:10:55 +00:00
|
|
|
unsubscribeMessages = () => {
|
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2019-11-21 19:10:55 +00:00
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
getLastMessage = () => {
|
|
|
|
const { messages } = this.state;
|
|
|
|
if (messages.length > 0) {
|
|
|
|
return messages[0];
|
|
|
|
}
|
|
|
|
return null;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
handleScrollToIndexFailed = params => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { listRef } = this.props;
|
|
|
|
listRef.current.getNode().scrollToIndex({ index: params.highestMeasuredFrameIndex, animated: false });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
jumpToMessage = messageId =>
|
|
|
|
new Promise(async resolve => {
|
|
|
|
this.jumping = true;
|
|
|
|
const { messages } = this.state;
|
|
|
|
const { listRef } = this.props;
|
|
|
|
const index = messages.findIndex(item => item.id === messageId);
|
|
|
|
if (index > -1) {
|
|
|
|
listRef.current.getNode().scrollToIndex({ index, viewPosition: 0.5, viewOffset: 100 });
|
|
|
|
await new Promise(res => setTimeout(res, 300));
|
|
|
|
if (!this.viewableItems.map(vi => vi.key).includes(messageId)) {
|
|
|
|
if (!this.jumping) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
await setTimeout(() => resolve(this.jumpToMessage(messageId)), 300);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({ highlightedMessage: messageId });
|
|
|
|
this.clearHighlightedMessageTimeout();
|
|
|
|
this.highlightedMessageTimeout = setTimeout(() => {
|
|
|
|
this.setState({ highlightedMessage: null });
|
|
|
|
}, 10000);
|
|
|
|
await setTimeout(() => resolve(), 300);
|
|
|
|
} else {
|
|
|
|
listRef.current.getNode().scrollToIndex({ index: messages.length - 1, animated: false });
|
2021-06-15 20:13:42 +00:00
|
|
|
if (!this.jumping) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
await setTimeout(() => resolve(this.jumpToMessage(messageId)), 300);
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
});
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
// this.jumping is checked in between operations to make sure we're not stuck
|
|
|
|
cancelJumpToMessage = () => {
|
|
|
|
this.jumping = false;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
jumpToBottom = () => {
|
|
|
|
const { listRef } = this.props;
|
|
|
|
listRef.current.getNode().scrollToOffset({ offset: -100 });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
renderFooter = () => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { rid, theme, loading } = this.props;
|
2019-11-25 20:01:17 +00:00
|
|
|
if (loading && rid) {
|
2019-12-04 16:39:53 +00:00
|
|
|
return <ActivityIndicator theme={theme} />;
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2019-03-27 20:06:57 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem = ({ item, index }) => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { messages, highlightedMessage } = this.state;
|
2019-04-17 17:01:03 +00:00
|
|
|
const { renderRow } = this.props;
|
2021-06-15 20:13:42 +00:00
|
|
|
return renderRow(item, messages[index + 1], highlightedMessage);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
onViewableItemsChanged = ({ viewableItems }) => {
|
|
|
|
this.viewableItems = viewableItems;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
render() {
|
2021-10-01 18:12:19 +00:00
|
|
|
console.count(`${this.constructor.name}.render calls`);
|
2021-06-15 20:13:42 +00:00
|
|
|
const { rid, tmid, listRef } = this.props;
|
2020-02-05 13:27:06 +00:00
|
|
|
const { messages, refreshing } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-03-27 20:06:57 +00:00
|
|
|
return (
|
2019-09-16 20:26:32 +00:00
|
|
|
<>
|
2019-12-04 16:39:53 +00:00
|
|
|
<EmptyRoom rid={rid} length={messages.length} mounted={this.mounted} theme={theme} />
|
2021-06-15 20:13:42 +00:00
|
|
|
<List
|
|
|
|
onScroll={this.onScroll}
|
|
|
|
scrollEventThrottle={16}
|
|
|
|
listRef={listRef}
|
2019-05-03 13:33:38 +00:00
|
|
|
data={messages}
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem={this.renderItem}
|
2019-03-27 20:06:57 +00:00
|
|
|
onEndReached={this.onEndReached}
|
|
|
|
ListFooterComponent={this.renderFooter}
|
2021-06-15 20:13:42 +00:00
|
|
|
onScrollToIndexFailed={this.handleScrollToIndexFailed}
|
|
|
|
onViewableItemsChanged={this.onViewableItemsChanged}
|
|
|
|
viewabilityConfig={this.viewabilityConfig}
|
2021-10-01 18:12:19 +00:00
|
|
|
refreshControl={
|
|
|
|
<RefreshControl refreshing={refreshing} onRefresh={this.onRefresh} tintColor={themes[theme].auxiliaryText} />
|
|
|
|
}
|
2019-03-27 20:06:57 +00:00
|
|
|
/>
|
2021-06-15 20:13:42 +00:00
|
|
|
<NavBottomFAB y={this.y} onPress={this.jumpToBottom} isThread={!!tmid} />
|
2019-09-16 20:26:32 +00:00
|
|
|
</>
|
2019-03-27 20:06:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2021-06-15 20:13:42 +00:00
|
|
|
export default ListContainer;
|