verdnatura-chat/app/views/RoomView/List.js

224 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-03-27 20:06:57 +00:00
import React from 'react';
2019-12-11 23:01:12 +00:00
import { FlatList, InteractionManager } from 'react-native';
2019-03-27 20:06:57 +00:00
import PropTypes from 'prop-types';
import orderBy from 'lodash/orderBy';
import { Q } from '@nozbe/watermelondb';
2019-03-27 20:06:57 +00:00
import styles from './styles';
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';
import { isIOS } from '../../utils/deviceInfo';
import { animateNextTransition } from '../../utils/layoutAnimation';
2019-12-11 23:01:12 +00:00
import ActivityIndicator from '../../containers/ActivityIndicator';
import debounce from '../../utils/debounce';
2019-03-27 20:06:57 +00:00
2019-12-11 23:01:12 +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,
tmid: PropTypes.string,
2019-12-11 23:01:12 +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
this.mounted = false;
2019-03-27 20:06:57 +00:00
this.state = {
loading: true,
end: false,
messages: []
2019-03-27 20:06:57 +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() {
this.mounted = true;
2019-04-08 12:35:28 +00:00
console.timeEnd(`${ this.constructor.name } mount`);
}
// 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-12-11 23:01:12 +00:00
.query(Q.where('rid', tmid))
.observe();
} else if (rid) {
this.messagesObservable = db.collections
.get('messages')
2019-12-11 23:01:12 +00:00
.query(Q.where('rid', rid))
.observe();
}
2019-12-11 23:01:12 +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-12-11 23:01:12 +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-12-11 23:01:12 +00:00
const { loading, end } = this.state;
const { theme } = this.props;
if (theme !== nextProps.theme) {
return true;
}
2019-12-11 23:01:12 +00:00
if (loading !== nextState.loading) {
return true;
}
2019-12-11 23:01:12 +00:00
if (end !== nextState.end) {
return true;
}
return false;
}
2019-03-27 20:06:57 +00:00
componentWillUnmount() {
2019-12-11 23:01:12 +00:00
this.unsubscribeMessages();
if (this.interaction && this.interaction.cancel) {
this.interaction.cancel();
2019-04-08 12:35:28 +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
}
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, 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 });
}
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
2019-12-11 23:01:12 +00:00
// eslint-disable-next-line react/sort-comp
update = () => {
animateNextTransition();
this.forceUpdate();
};
unsubscribeMessages = () => {
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
this.messagesSubscription.unsubscribe();
}
}
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-11 23:01:12 +00:00
const { rid, theme } = this.props;
if (loading && rid) {
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 }) => {
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-12-11 23:01:12 +00:00
const { rid, listRef } = this.props;
const { messages } = this.state;
2019-12-11 23:01:12 +00:00
const { theme } = this.props;
2019-03-27 20:06:57 +00:00
return (
<>
2019-12-11 23:01:12 +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-12-11 23:01:12 +00:00
ref={listRef}
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={isIOS}
2019-12-11 23:01:12 +00:00
initialNumToRender={7}
2019-03-27 20:06:57 +00:00
onEndReached={this.onEndReached}
// onEndReachedThreshold={5}
// maxToRenderPerBatch={5}
// windowSize={10}
2019-03-27 20:06:57 +00:00
ListFooterComponent={this.renderFooter}
{...scrollPersistTaps}
/>
</>
2019-03-27 20:06:57 +00:00
);
}
}
2019-12-11 23:01:12 +00:00
export default List;