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

270 lines
6.7 KiB
JavaScript
Raw Normal View History

2019-03-27 20:06:57 +00:00
import React from 'react';
2020-02-19 19:43:47 +00:00
import { FlatList, InteractionManager, RefreshControl } 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';
2020-02-19 19:43:47 +00:00
import moment from 'moment';
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';
2020-02-19 19:43:47 +00:00
import { themes } from '../../constants/colors';
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
theme: PropTypes.string,
2020-02-19 19:43:47 +00:00
listRef: PropTypes.func,
navigation: PropTypes.object
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,
2020-02-19 19:43:47 +00:00
messages: [],
refreshing: false,
animated: false
2019-03-27 20:06:57 +00:00
};
this.init();
2020-02-19 19:43:47 +00:00
this.didFocusListener = props.navigation.addListener('didFocus', () => {
if (this.mounted) {
this.setState({ animated: true });
} else {
this.state.animated = true;
}
});
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) {
2020-02-19 19:43:47 +00:00
const { loading, end, refreshing } = this.state;
2019-12-11 23:01:12 +00:00
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;
}
2020-02-19 19:43:47 +00:00
if (refreshing !== nextState.refreshing) {
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
}
2020-02-19 19:43:47 +00:00
if (this.didFocusListener && this.didFocusListener.remove) {
this.didFocusListener.remove();
}
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
2020-02-19 19:43:47 +00:00
onRefresh = () => this.setState({ refreshing: true }, async() => {
const { messages } = this.state;
const { rid, tmid } = this.props;
if (messages.length) {
try {
if (tmid) {
await RocketChat.loadThreadMessages({ tmid, rid, offset: messages.length - 1 });
} else {
await RocketChat.loadMissedMessages({ rid, lastOpen: moment().subtract(7, 'days').toDate() });
}
} catch (e) {
log(e);
}
}
this.setState({ refreshing: false });
})
2019-12-11 23:01:12 +00:00
// eslint-disable-next-line react/sort-comp
update = () => {
2020-02-19 19:43:47 +00:00
const { animated } = this.state;
if (animated) {
animateNextTransition();
}
2019-12-11 23:01:12 +00:00
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;
2020-02-19 19:43:47 +00:00
const { messages, refreshing } = 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}
2020-01-29 20:20:36 +00:00
onEndReachedThreshold={5}
maxToRenderPerBatch={5}
windowSize={10}
2019-03-27 20:06:57 +00:00
ListFooterComponent={this.renderFooter}
2020-02-19 19:43:47 +00:00
refreshControl={(
<RefreshControl
refreshing={refreshing}
onRefresh={this.onRefresh}
tintColor={themes[theme].auxiliaryText}
/>
)}
2019-03-27 20:06:57 +00:00
{...scrollPersistTaps}
/>
</>
2019-03-27 20:06:57 +00:00
);
}
}
2019-12-11 23:01:12 +00:00
export default List;