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

156 lines
4.5 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';
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 debounce from '../../utils/debounce';
import RocketChat from '../../lib/rocketchat';
import log from '../../utils/log';
import EmptyRoom from './EmptyRoom';
2019-04-08 12:35:28 +00:00
// import ScrollBottomButton from './ScrollBottomButton';
2019-03-27 20:06:57 +00:00
export class List extends React.Component {
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-03-27 20:06:57 +00:00
window: PropTypes.object
};
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-03-27 20:06:57 +00:00
this.data = database
.objects('messages')
2019-04-08 12:35:28 +00:00
.filtered('rid = $0', props.rid)
2019-03-27 20:06:57 +00:00
.sorted('ts', true);
this.state = {
loading: true,
loadingMore: false,
end: false,
2019-04-08 12:35:28 +00:00
messages: this.data.slice()
// showScollToBottomButton: false
2019-03-27 20:06:57 +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
}
// shouldComponentUpdate(nextProps, nextState) {
// const {
// loadingMore, loading, end, showScollToBottomButton, messages
// } = this.state;
// const { window } = this.props;
// return end !== nextState.end
// || loadingMore !== nextState.loadingMore
// || loading !== nextState.loading
// || showScollToBottomButton !== nextState.showScollToBottomButton
// // || messages.length !== nextState.messages.length
// || !equal(messages, nextState.messages)
// || window.width !== nextProps.window.width;
// }
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-08 12:35:28 +00:00
if (this.updateState && this.updateState.stop) {
this.updateState.stop();
}
if (this.interactionManager && this.interactionManager.cancel) {
this.interactionManager.cancel();
}
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-08 12:35:28 +00:00
this.interactionManager = InteractionManager.runAfterInteractions(() => {
this.setState({ messages: this.data.slice(), loading: false, loadingMore: false });
});
2019-03-27 20:06:57 +00:00
}, 300);
onEndReached = async() => {
const {
loadingMore, loading, end, messages
} = this.state;
if (loadingMore || loading || end || messages.length < 50) {
return;
}
this.setState({ loadingMore: true });
2019-04-08 12:35:28 +00:00
const { rid, t } = this.props;
2019-03-27 20:06:57 +00:00
try {
2019-04-08 12:35:28 +00:00
const result = await RocketChat.loadMessagesForRoom({ rid, t, latest: this.data[this.data.length - 1].ts });
2019-03-27 20:06:57 +00:00
this.setState({ end: result.length < 50 });
} catch (e) {
this.setState({ loadingMore: false });
log('ListView.onEndReached', e);
}
}
2019-04-08 12:35:28 +00:00
// scrollToBottom = () => {
// requestAnimationFrame(() => {
// this.list.scrollToOffset({ offset: isNotch ? -90 : -60 });
// });
// }
2019-03-27 20:06:57 +00:00
2019-04-08 12:35:28 +00:00
// handleScroll = (event) => {
// if (event.nativeEvent.contentOffset.y > 0) {
// this.setState({ showScollToBottomButton: true });
// } else {
// this.setState({ showScollToBottomButton: false });
// }
// }
2019-03-27 20:06:57 +00:00
renderFooter = () => {
const { loadingMore, loading } = this.state;
if (loadingMore || loading) {
return <ActivityIndicator style={styles.loadingMore} />;
}
return null;
}
render() {
2019-04-08 12:35:28 +00:00
console.count(`${ this.constructor.name }.render calls`);
const { renderRow } = this.props;
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}
renderItem={({ item, index }) => renderRow(item, messages[index + 1])}
2019-03-29 19:36:07 +00:00
contentContainerStyle={styles.contentContainer}
2019-03-27 20:06:57 +00:00
style={styles.list}
2019-04-08 12:35:28 +00:00
// onScroll={this.handleScroll}
2019-03-27 20:06:57 +00:00
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}
/>
2019-04-08 12:35:28 +00:00
{/* <ScrollBottomButton
2019-03-27 20:06:57 +00:00
show={showScollToBottomButton}
onPress={this.scrollToBottom}
landscape={window.width > window.height}
2019-04-08 12:35:28 +00:00
/> */}
2019-03-27 20:06:57 +00:00
</React.Fragment>
);
}
}