Newer
Older
import {
ActivityIndicator, FlatList, InteractionManager, LayoutAnimation
} from 'react-native';
import orderBy from 'lodash/orderBy';
import { Q } from '@nozbe/watermelondb';
import isEqual from 'lodash/isEqual';
import database from '../../lib/database';
import scrollPersistTaps from '../../utils/scrollPersistTaps';
import RocketChat from '../../lib/rocketchat';
import log from '../../utils/log';
import EmptyRoom from './EmptyRoom';
import { isIOS } from '../../utils/deviceInfo';
export class List extends React.Component {
static propTypes = {
onEndReached: PropTypes.func,
renderFooter: PropTypes.func,
renderRow: PropTypes.func,
tmid: PropTypes.string,
animated: PropTypes.bool
};
constructor(props) {
super(props);
console.time(`${ this.constructor.name } init`);
console.time(`${ this.constructor.name } mount`);
this.state = {
loading: true,
end: false,
console.timeEnd(`${ this.constructor.name } init`);
console.timeEnd(`${ this.constructor.name } mount`);
}
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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')
.query(
Q.where('rid', tmid)
)
.observeWithColumns(['_updated_at']);
} else {
this.messagesObservable = db.collections
.get('messages')
.query(
Q.where('rid', rid)
)
.observeWithColumns(['_updated_at']);
}
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) {
LayoutAnimation.easeInEaseOut();
this.setState({ messages });
} else {
this.state.messages = messages;
}
});
});
}
// 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) {
const { messages, loading, end } = this.state;
if (loading !== nextState.loading) {
return true;
}
if (end !== nextState.end) {
return true;
}
if (!isEqual(messages, nextState.messages)) {
return true;
}
return false;
}
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
this.messagesSubscription.unsubscribe();
}
if (this.interaction && this.interaction.cancel) {
this.interaction.cancel();
if (this.onEndReached && this.onEndReached.stop) {
this.onEndReached.stop();
console.countReset(`${ this.constructor.name }.render calls`);
this.setState({ loading: true });
const { rid, t, tmid } = this.props;
// `offset` is `messages.length - 1` because we append thread start to `messages` obj
result = await RocketChat.loadThreadMessages({ tmid, rid, offset: messages.length - 1 });
} else {
result = await RocketChat.loadMessagesForRoom({ rid, t, latest: messages[messages.length - 1].ts });
}
this.setState({ end: result.length < 50, loading: false });
const { loading } = this.state;
if (loading) {
return <ActivityIndicator style={styles.loading} />;
const { renderRow } = this.props;
return renderRow(item, messages[index + 1]);
}
console.count(`${ this.constructor.name }.render calls`);
const { messages } = this.state;
<>
<EmptyRoom length={messages.length} mounted={this.mounted} />
<FlatList
testID='room-view-messages'
ref={ref => this.list = ref}
ListFooterComponent={this.renderFooter}
{...scrollPersistTaps}
/>