vn-verdnaturachat/app/views/RoomView/List.js

162 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-03-27 20:06:57 +00:00
import React from 'react';
2019-04-08 12:35:28 +00:00
import { ActivityIndicator, FlatList, InteractionManager } 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 { emojify } from 'react-emojione';
import debounce from 'lodash/debounce';
2019-03-27 20:06:57 +00:00
import styles from './styles';
2019-04-04 18:08:40 +00:00
import database, { safeAddListener } from '../../lib/realm';
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-04-17 17:01:03 +00:00
export class List extends React.PureComponent {
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-04-17 17:01:03 +00:00
tmid: PropTypes.string
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
if (props.tmid) {
this.data = database
.objects('threadMessages')
.filtered('rid = $0', props.tmid)
.sorted('ts', true);
this.threads = [];
} else {
this.data = database
.objects('messages')
.filtered('rid = $0', props.rid)
.sorted('ts', true);
this.threads = database.objects('threads').filtered('rid = $0', props.rid);
}
2019-03-27 20:06:57 +00:00
this.state = {
loading: true,
end: false,
2019-04-17 17:01:03 +00:00
messages: this.data.slice(),
threads: this.threads.slice()
2019-03-27 20:06:57 +00:00
};
2019-04-17 17:01:03 +00:00
2019-04-04 18:08:40 +00:00
safeAddListener(this.data, this.updateState);
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() {
console.timeEnd(`${ this.constructor.name } mount`);
}
2019-03-27 20:06:57 +00:00
componentWillUnmount() {
this.data.removeAllListeners();
2019-04-17 17:01:03 +00:00
this.threads.removeAllListeners();
2019-04-08 12:35:28 +00:00
if (this.updateState && this.updateState.stop) {
this.updateState.stop();
}
2019-04-17 17:01:03 +00:00
if (this.updateThreads && this.updateThreads.stop) {
this.updateThreads.stop();
}
if (this.interactionManagerState && this.interactionManagerState.cancel) {
this.interactionManagerState.cancel();
}
if (this.interactionManagerThreads && this.interactionManagerThreads.cancel) {
this.interactionManagerThreads.cancel();
2019-04-08 12:35:28 +00:00
}
console.countReset(`${ this.constructor.name }.render calls`);
2019-03-27 20:06:57 +00:00
}
// eslint-disable-next-line react/sort-comp
updateState = debounce(() => {
2019-04-17 17:01:03 +00:00
this.interactionManagerState = InteractionManager.runAfterInteractions(() => {
this.setState({
messages: this.data.slice(),
threads: this.threads.slice(),
loading: false
});
2019-04-08 12:35:28 +00:00
});
2019-04-17 17:01:03 +00:00
}, 300, { leading: true });
2019-03-27 20:06:57 +00:00
onEndReached = async() => {
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) {
result = await RocketChat.loadThreadMessages({ tmid, skip: messages.length });
} else {
result = await RocketChat.loadMessagesForRoom({ rid, t, latest: messages[messages.length - 1].ts });
}
2019-03-27 20:06:57 +00:00
this.setState({ end: result.length < 50 });
} catch (e) {
2019-04-17 17:01:03 +00:00
this.setState({ loading: false });
2019-03-27 20:06:57 +00:00
log('ListView.onEndReached', e);
}
}
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 }) => {
const { messages, threads } = this.state;
const { renderRow } = this.props;
if (item.tmid) {
const thread = threads.find(t => t._id === item.tmid);
if (thread) {
let tmsg = thread.msg || (thread.attachments && thread.attachments.length && thread.attachments[0].title);
tmsg = emojify(tmsg, { output: 'unicode' });
item = { ...item, tmsg };
}
}
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`);
const { messages } = this.state;
2019-03-27 20:06:57 +00:00
return (
<React.Fragment>
<EmptyRoom length={messages.length} />
<FlatList
testID='room-view-messages'
ref={ref => this.list = ref}
keyExtractor={item => item._id}
data={messages}
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
removeClippedSubviews
2019-04-08 12:35:28 +00:00
initialNumToRender={1}
2019-03-27 20:06:57 +00:00
onEndReached={this.onEndReached}
onEndReachedThreshold={0.5}
2019-04-08 12:35:28 +00:00
maxToRenderPerBatch={5}
windowSize={21}
2019-03-27 20:06:57 +00:00
ListFooterComponent={this.renderFooter}
{...scrollPersistTaps}
/>
</React.Fragment>
);
}
}