Rocket.Chat.ReactNative/app/views/ThreadMessagesView/index.js

275 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-04-17 17:01:03 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import {
FlatList, View, Text, InteractionManager
} from 'react-native';
import { connect } from 'react-redux';
import { SafeAreaView } from 'react-navigation';
import moment from 'moment';
import styles from './styles';
import Message from '../../containers/message';
import RCActivityIndicator from '../../containers/ActivityIndicator';
import I18n from '../../i18n';
import RocketChat from '../../lib/rocketchat';
import database, { safeAddListener } from '../../lib/realm';
import StatusBar from '../../containers/StatusBar';
import buildMessage from '../../lib/methods/helpers/buildMessage';
import log from '../../utils/log';
import debounce from '../../utils/debounce';
const Separator = React.memo(() => <View style={styles.separator} />);
const API_FETCH_COUNT = 50;
2019-04-17 17:01:03 +00:00
class ThreadMessagesView extends React.Component {
2019-04-17 17:01:03 +00:00
static navigationOptions = {
title: I18n.t('Threads')
}
static propTypes = {
user: PropTypes.object,
navigation: PropTypes.object,
baseUrl: PropTypes.string,
useRealName: PropTypes.bool
2019-04-17 17:01:03 +00:00
}
constructor(props) {
super(props);
2019-04-17 17:01:03 +00:00
this.rid = props.navigation.getParam('rid');
this.t = props.navigation.getParam('t');
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
this.messages = database.objects('threads').filtered('rid = $0', this.rid).sorted('ts', true);
2019-04-17 17:01:03 +00:00
safeAddListener(this.messages, this.updateMessages);
this.state = {
loading: false,
end: false,
messages: this.messages
2019-04-17 17:01:03 +00:00
};
this.mounted = false;
2019-04-17 17:01:03 +00:00
}
componentDidMount() {
this.mountInteraction = InteractionManager.runAfterInteractions(() => {
this.init();
this.mounted = true;
});
2019-04-17 17:01:03 +00:00
}
componentWillUnmount() {
this.messages.removeAllListeners();
if (this.mountInteraction && this.mountInteraction.cancel) {
this.mountInteraction.cancel();
2019-04-17 17:01:03 +00:00
}
if (this.loadInteraction && this.loadInteraction.cancel) {
this.loadInteraction.cancel();
2019-04-17 17:01:03 +00:00
}
if (this.syncInteraction && this.syncInteraction.cancel) {
this.syncInteraction.cancel();
2019-04-17 17:01:03 +00:00
}
}
// eslint-disable-next-line react/sort-comp
updateMessages = debounce(() => {
this.setState({ messages: this.messages });
}, 300)
// eslint-disable-next-line react/sort-comp
init = () => {
const [room] = this.rooms;
// if there's not room at this point, it's better to show nothing
if (!room) {
return;
}
const lastThreadSync = new Date();
if (room.lastThreadSync) {
this.sync(room.lastThreadSync);
} else {
this.load();
}
database.write(() => {
room.lastThreadSync = lastThreadSync;
});
2019-04-17 17:01:03 +00:00
}
// eslint-disable-next-line react/sort-comp
load = debounce(async() => {
const { loading, end } = this.state;
if (end || loading || !this.mounted) {
2019-04-17 17:01:03 +00:00
return;
}
this.setState({ loading: true });
try {
const result = await RocketChat.getThreadsList({
rid: this.rid, count: API_FETCH_COUNT, offset: this.messages.length
2019-04-17 17:01:03 +00:00
});
if (result.success) {
this.loadInteraction = InteractionManager.runAfterInteractions(() => {
database.write(() => result.threads.forEach((message) => {
try {
database.create('threads', buildMessage(message), true);
} catch (e) {
log(e);
}
}));
this.setState({
loading: false,
end: result.count < API_FETCH_COUNT
});
});
}
} catch (e) {
log(e);
2019-04-17 17:01:03 +00:00
this.setState({ loading: false, end: true });
}
}, 300)
// eslint-disable-next-line react/sort-comp
sync = async(updatedSince) => {
this.setState({ loading: true });
try {
const result = await RocketChat.getSyncThreadsList({
rid: this.rid, updatedSince: updatedSince.toISOString()
});
if (result.success && result.threads) {
this.syncInteraction = InteractionManager.runAfterInteractions(() => {
const { update, remove } = result.threads;
database.write(() => {
if (update && update.length) {
update.forEach((message) => {
try {
database.create('threads', buildMessage(message), true);
} catch (e) {
log(e);
}
});
}
if (remove && remove.length) {
remove.forEach((message) => {
const oldMessage = database.objectForPrimaryKey('threads', message._id);
if (oldMessage) {
try {
database.delete(oldMessage);
} catch (e) {
log(e);
}
}
});
}
});
this.setState({
loading: false
});
});
}
} catch (e) {
log(e);
this.setState({ loading: false });
}
}
2019-04-17 17:01:03 +00:00
formatMessage = lm => (
lm ? moment(lm).calendar(null, {
lastDay: `[${ I18n.t('Yesterday') }]`,
sameDay: 'h:mm A',
lastWeek: 'dddd',
sameElse: 'MMM D'
}) : null
)
onThreadPress = debounce((item) => {
const { navigation } = this.props;
if (item.tmid) {
navigation.push('RoomView', {
rid: item.rid, tmid: item.tmid, name: item.tmsg, t: 'thread'
});
} else if (item.tlm) {
const title = item.msg || (item.attachments && item.attachments.length && item.attachments[0].title);
navigation.push('RoomView', {
rid: item.rid, tmid: item._id, name: title, t: 'thread'
});
}
}, 1000, true)
2019-04-17 17:01:03 +00:00
renderSeparator = () => <Separator />
renderEmpty = () => (
<View style={styles.listEmptyContainer} testID='thread-messages-view'>
<Text style={styles.noDataFound}>{I18n.t('No_thread_messages')}</Text>
</View>
)
renderItem = ({ item }) => {
const {
user, navigation, baseUrl, useRealName
} = this.props;
if (item.isValid && item.isValid()) {
return (
<Message
key={item._id}
item={item}
user={user}
archived={false}
broadcast={false}
status={item.status}
_updatedAt={item._updatedAt}
navigation={navigation}
timeFormat='MMM D'
customThreadTimeFormat='MMM Do YYYY, h:mm:ss a'
onThreadPress={this.onThreadPress}
baseUrl={baseUrl}
useRealName={useRealName}
/>
);
}
return null;
2019-04-17 17:01:03 +00:00
}
render() {
const { loading, messages } = this.state;
2019-04-17 17:01:03 +00:00
if (!loading && this.messages.length === 0) {
2019-04-17 17:01:03 +00:00
return this.renderEmpty();
}
return (
<SafeAreaView style={styles.list} testID='thread-messages-view' forceInset={{ vertical: 'never' }}>
2019-04-17 17:01:03 +00:00
<StatusBar />
<FlatList
data={messages}
extraData={this.state}
2019-04-17 17:01:03 +00:00
renderItem={this.renderItem}
style={styles.list}
contentContainerStyle={styles.contentContainer}
keyExtractor={item => item._id}
onEndReached={this.load}
onEndReachedThreshold={0.5}
maxToRenderPerBatch={5}
initialNumToRender={1}
ItemSeparatorComponent={this.renderSeparator}
ListFooterComponent={loading ? <RCActivityIndicator /> : null}
/>
</SafeAreaView>
);
}
}
const mapStateToProps = state => ({
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
user: {
id: state.login.user && state.login.user.id,
username: state.login.user && state.login.user.username,
token: state.login.user && state.login.user.token
},
useRealName: state.settings.UI_Use_Real_Name
});
export default connect(mapStateToProps)(ThreadMessagesView);