2019-03-27 20:06:57 +00:00
|
|
|
import React from 'react';
|
2019-09-16 20:26:32 +00:00
|
|
|
import {
|
|
|
|
ActivityIndicator, FlatList, InteractionManager, LayoutAnimation
|
|
|
|
} from 'react-native';
|
2019-03-27 20:06:57 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-04-17 17:01:03 +00:00
|
|
|
import debounce from 'lodash/debounce';
|
2019-09-16 20:26:32 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import isEqual from 'lodash/isEqual';
|
2019-03-27 20:06:57 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2019-03-27 20:06:57 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import EmptyRoom from './EmptyRoom';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
2019-03-27 20:06:57 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export class List extends React.Component {
|
2019-03-27 20:06:57 +00:00
|
|
|
static propTypes = {
|
|
|
|
onEndReached: PropTypes.func,
|
|
|
|
renderFooter: PropTypes.func,
|
|
|
|
renderRow: PropTypes.func,
|
2019-04-08 12:35:28 +00:00
|
|
|
rid: PropTypes.string,
|
|
|
|
t: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
tmid: PropTypes.string,
|
|
|
|
animated: PropTypes.bool
|
2019-03-27 20:06:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-04-08 12:35:28 +00:00
|
|
|
console.time(`${ this.constructor.name } init`);
|
|
|
|
console.time(`${ this.constructor.name } mount`);
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = false;
|
2019-03-27 20:06:57 +00:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
end: false,
|
2019-09-16 20:26:32 +00:00
|
|
|
messages: []
|
2019-03-27 20:06:57 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2019-04-08 12:35:28 +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;
|
2019-04-08 12:35:28 +00:00
|
|
|
console.timeEnd(`${ this.constructor.name } mount`);
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
async init() {
|
|
|
|
const { rid, tmid } = this.props;
|
|
|
|
const db = database.active;
|
|
|
|
|
|
|
|
if (tmid) {
|
|
|
|
try {
|
|
|
|
this.thread = await db.collections
|
|
|
|
.get('threads')
|
|
|
|
.find(tmid);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('thread_messages')
|
|
|
|
.query(
|
|
|
|
Q.where('rid', tmid)
|
|
|
|
)
|
|
|
|
.observeWithColumns(['_updated_at']);
|
|
|
|
} else {
|
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('messages')
|
|
|
|
.query(
|
|
|
|
Q.where('rid', rid)
|
|
|
|
)
|
|
|
|
.observeWithColumns(['_updated_at']);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.messagesSubscription = this.messagesObservable
|
|
|
|
.subscribe((data) => {
|
|
|
|
this.interaction = InteractionManager.runAfterInteractions(() => {
|
|
|
|
if (tmid) {
|
|
|
|
data = [this.thread, ...data];
|
|
|
|
}
|
|
|
|
const messages = orderBy(data, ['ts'], ['desc']);
|
|
|
|
if (this.mounted) {
|
|
|
|
LayoutAnimation.easeInEaseOut();
|
|
|
|
this.setState({ messages });
|
|
|
|
} else {
|
|
|
|
this.state.messages = messages;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.state.loading works for this.onEndReached and RoomView.init
|
|
|
|
static getDerivedStateFromProps(props, state) {
|
|
|
|
if (props.loading !== state.loading) {
|
|
|
|
return {
|
|
|
|
loading: props.loading
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { messages, loading, end } = this.state;
|
|
|
|
if (loading !== nextState.loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (end !== nextState.end) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!isEqual(messages, nextState.messages)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
componentWillUnmount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
if (this.interaction && this.interaction.cancel) {
|
|
|
|
this.interaction.cancel();
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
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
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
console.countReset(`${ this.constructor.name }.render calls`);
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
onEndReached = debounce(async() => {
|
2019-03-27 20:06:57 +00:00
|
|
|
const {
|
2019-04-17 17:01:03 +00:00
|
|
|
loading, end, messages
|
2019-03-27 20:06:57 +00:00
|
|
|
} = this.state;
|
2019-04-17 17:01:03 +00:00
|
|
|
if (loading || end || messages.length < 50) {
|
2019-03-27 20:06:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
this.setState({ loading: true });
|
|
|
|
const { rid, t, tmid } = this.props;
|
2019-03-27 20:06:57 +00:00
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
let result;
|
|
|
|
if (tmid) {
|
2019-05-03 13:33:38 +00:00
|
|
|
// `offset` is `messages.length - 1` because we append thread start to `messages` obj
|
2019-09-16 20:26:32 +00:00
|
|
|
result = await RocketChat.loadThreadMessages({ tmid, rid, offset: messages.length - 1 });
|
2019-04-17 17:01:03 +00:00
|
|
|
} else {
|
|
|
|
result = await RocketChat.loadMessagesForRoom({ rid, t, latest: messages[messages.length - 1].ts });
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
this.setState({ end: result.length < 50, loading: false });
|
2019-03-27 20:06:57 +00:00
|
|
|
} catch (e) {
|
2019-04-17 17:01:03 +00:00
|
|
|
this.setState({ loading: false });
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
}, 300)
|
2019-03-27 20:06:57 +00:00
|
|
|
|
|
|
|
renderFooter = () => {
|
2019-04-17 17:01:03 +00:00
|
|
|
const { loading } = this.state;
|
|
|
|
if (loading) {
|
|
|
|
return <ActivityIndicator style={styles.loading} />;
|
2019-03-27 20:06:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem = ({ item, index }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { messages } = this.state;
|
2019-04-17 17:01:03 +00:00
|
|
|
const { renderRow } = this.props;
|
|
|
|
return renderRow(item, messages[index + 1]);
|
|
|
|
}
|
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
render() {
|
2019-04-08 12:35:28 +00:00
|
|
|
console.count(`${ this.constructor.name }.render calls`);
|
2019-05-03 13:33:38 +00:00
|
|
|
const { messages } = this.state;
|
2019-03-27 20:06:57 +00:00
|
|
|
return (
|
2019-09-16 20:26:32 +00:00
|
|
|
<>
|
|
|
|
<EmptyRoom length={messages.length} mounted={this.mounted} />
|
2019-03-27 20:06:57 +00:00
|
|
|
<FlatList
|
|
|
|
testID='room-view-messages'
|
|
|
|
ref={ref => this.list = ref}
|
2019-09-16 20:26:32 +00:00
|
|
|
keyExtractor={item => item.id}
|
2019-05-03 13:33:38 +00:00
|
|
|
data={messages}
|
2019-03-27 20:06:57 +00:00
|
|
|
extraData={this.state}
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem={this.renderItem}
|
2019-03-29 19:36:07 +00:00
|
|
|
contentContainerStyle={styles.contentContainer}
|
2019-03-27 20:06:57 +00:00
|
|
|
style={styles.list}
|
|
|
|
inverted
|
2019-09-16 20:26:32 +00:00
|
|
|
removeClippedSubviews={isIOS}
|
2019-05-20 20:43:50 +00:00
|
|
|
initialNumToRender={7}
|
2019-03-27 20:06:57 +00:00
|
|
|
onEndReached={this.onEndReached}
|
2019-05-20 20:43:50 +00:00
|
|
|
onEndReachedThreshold={5}
|
2019-04-08 12:35:28 +00:00
|
|
|
maxToRenderPerBatch={5}
|
2019-05-20 20:43:50 +00:00
|
|
|
windowSize={10}
|
2019-03-27 20:06:57 +00:00
|
|
|
ListFooterComponent={this.renderFooter}
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
/>
|
2019-09-16 20:26:32 +00:00
|
|
|
</>
|
2019-03-27 20:06:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|