2019-03-27 20:06:57 +00:00
|
|
|
import React from 'react';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { FlatList, InteractionManager } 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 orderBy from 'lodash/orderBy';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
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-09-19 13:32:24 +00:00
|
|
|
import { animateNextTransition } from '../../utils/layoutAnimation';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2019-11-07 19:53:39 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2019-03-27 20:06:57 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
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,
|
2019-12-04 16:39:53 +00:00
|
|
|
animated: PropTypes.bool,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
listRef: PropTypes.func
|
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')
|
2019-11-07 19:53:39 +00:00
|
|
|
.query(Q.where('rid', tmid))
|
2019-11-25 20:01:17 +00:00
|
|
|
.observe();
|
|
|
|
} else if (rid) {
|
2019-09-16 20:26:32 +00:00
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('messages')
|
2019-11-07 19:53:39 +00:00
|
|
|
.query(Q.where('rid', rid))
|
2019-11-25 20:01:17 +00:00
|
|
|
.observe();
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
if (rid) {
|
|
|
|
this.unsubscribeMessages();
|
|
|
|
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) {
|
|
|
|
this.setState({ messages }, () => this.update());
|
|
|
|
} else {
|
|
|
|
this.state.messages = messages;
|
|
|
|
}
|
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2019-11-25 20:01:17 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2019-11-07 19:53:39 +00:00
|
|
|
const { loading, end } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (theme !== nextProps.theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (loading !== nextState.loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (end !== nextState.end) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.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
|
|
|
|
2019-11-07 19:53:39 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
update = () => {
|
|
|
|
animateNextTransition();
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2019-11-21 19:10:55 +00:00
|
|
|
unsubscribeMessages = () => {
|
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
getLastMessage = () => {
|
|
|
|
const { messages } = this.state;
|
|
|
|
if (messages.length > 0) {
|
|
|
|
return messages[0];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
renderFooter = () => {
|
2019-04-17 17:01:03 +00:00
|
|
|
const { loading } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { rid, theme } = 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;
|
|
|
|
}
|
|
|
|
|
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-11-25 20:01:17 +00:00
|
|
|
const { rid, listRef } = this.props;
|
2019-05-03 13:33:38 +00:00
|
|
|
const { messages } = 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} />
|
2019-03-27 20:06:57 +00:00
|
|
|
<FlatList
|
|
|
|
testID='room-view-messages'
|
2019-11-25 20:01:17 +00:00
|
|
|
ref={listRef}
|
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-11-01 17:41:32 +00:00
|
|
|
initialNumToRender={7}
|
2019-03-27 20:06:57 +00:00
|
|
|
onEndReached={this.onEndReached}
|
2019-11-01 17:41:32 +00:00
|
|
|
onEndReachedThreshold={5}
|
|
|
|
maxToRenderPerBatch={5}
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default List;
|