Rocket.Chat.ReactNative/app/views/RoomView/List.js

162 lines
4.4 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 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 = database.objects('threads').filtered('_id = $0', props.tmid);
2019-04-17 17:01:03 +00:00
} 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.interactionManagerState && this.interactionManagerState.cancel) {
this.interactionManagerState.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(() => {
const { tmid } = this.props;
let messages = this.data;
if (tmid && this.threads[0]) {
const thread = { ...this.threads[0] };
thread.tlm = null;
messages = [...messages, thread];
}
2019-04-17 17:01:03 +00:00
this.setState({
messages: messages.slice(),
2019-04-17 17:01:03 +00:00
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 = 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) {
// `offset` is `messages.length - 1` because we append thread start to `messages` obj
result = await RocketChat.loadThreadMessages({ tmid, 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 });
}
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 });
log(e);
2019-03-27 20:06:57 +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 }) => {
const { messages, threads } = this.state;
const { renderRow } = this.props;
if (item.tmid) {
const thread = threads.find(t => t._id === item.tmid);
if (thread) {
const tmsg = thread.msg || (thread.attachments && thread.attachments.length && thread.attachments[0].title);
2019-04-17 17:01:03 +00:00
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} />
2019-03-27 20:06:57 +00:00
<FlatList
testID='room-view-messages'
ref={ref => this.list = ref}
keyExtractor={item => item._id}
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
removeClippedSubviews
initialNumToRender={7}
2019-03-27 20:06:57 +00:00
onEndReached={this.onEndReached}
onEndReachedThreshold={5}
2019-04-08 12:35:28 +00:00
maxToRenderPerBatch={5}
windowSize={10}
2019-03-27 20:06:57 +00:00
ListFooterComponent={this.renderFooter}
{...scrollPersistTaps}
/>
</React.Fragment>
);
}
}