2019-04-17 17:01:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-10-30 17:35:07 +00:00
|
|
|
import { FlatList, InteractionManager } from 'react-native';
|
2019-04-17 17:01:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-10-30 17:35:07 +00:00
|
|
|
import { withSafeAreaInsets } from 'react-native-safe-area-context';
|
|
|
|
import { HeaderBackButton } from '@react-navigation/stack';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
2020-10-30 17:35:07 +00:00
|
|
|
import Item from './Item';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2019-04-17 17:01:03 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2020-10-30 17:35:07 +00:00
|
|
|
import { sanitizeLikeString } from '../../lib/database/utils';
|
2019-04-17 17:01:03 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import buildMessage from '../../lib/methods/helpers/buildMessage';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import debounce from '../../utils/debounce';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from '../../lib/methods/helpers/protectedFunction';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2020-10-30 17:35:07 +00:00
|
|
|
import * as List from '../../containers/List';
|
|
|
|
import Dropdown from './Dropdown';
|
|
|
|
import DropdownItemHeader from './Dropdown/DropdownItemHeader';
|
|
|
|
import { FILTER } from './filters';
|
|
|
|
import NoDataFound from './NoDataFound';
|
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
|
|
|
import { getBadgeColor, makeThreadName } from '../../utils/room';
|
|
|
|
import { getHeaderTitlePosition } from '../../containers/Header';
|
|
|
|
import SearchHeader from './SearchHeader';
|
2021-01-25 17:14:45 +00:00
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import { LISTENER } from '../../containers/Toast';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
const API_FETCH_COUNT = 50;
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class ThreadMessagesView extends React.Component {
|
2019-04-17 17:01:03 +00:00
|
|
|
static propTypes = {
|
|
|
|
user: PropTypes.object,
|
2019-05-20 20:43:50 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2019-05-20 20:43:50 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2020-10-30 17:35:07 +00:00
|
|
|
isMasterDetail: PropTypes.bool,
|
|
|
|
insets: PropTypes.object
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = false;
|
2020-06-15 14:00:46 +00:00
|
|
|
this.rid = props.route.params?.rid;
|
|
|
|
this.t = props.route.params?.t;
|
2019-04-17 17:01:03 +00:00
|
|
|
this.state = {
|
|
|
|
loading: false,
|
|
|
|
end: false,
|
2020-10-30 17:35:07 +00:00
|
|
|
messages: [],
|
|
|
|
displayingThreads: [],
|
|
|
|
subscription: {},
|
|
|
|
showFilterDropdown: false,
|
|
|
|
currentFilter: FILTER.ALL,
|
|
|
|
isSearching: false,
|
|
|
|
searchText: ''
|
2019-04-17 17:01:03 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
this.setHeader();
|
|
|
|
this.initSubscription();
|
|
|
|
this.subscribeMessages();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = true;
|
2019-04-24 18:36:29 +00:00
|
|
|
this.mountInteraction = InteractionManager.runAfterInteractions(() => {
|
|
|
|
this.init();
|
|
|
|
});
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const {
|
|
|
|
insets
|
|
|
|
} = this.props;
|
|
|
|
if (insets.left !== prevProps.insets.left || insets.right !== prevProps.insets.right) {
|
|
|
|
this.setHeader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
componentWillUnmount() {
|
2020-04-06 19:07:40 +00:00
|
|
|
console.countReset(`${ this.constructor.name }.render calls`);
|
2019-04-24 18:36:29 +00:00
|
|
|
if (this.mountInteraction && this.mountInteraction.cancel) {
|
|
|
|
this.mountInteraction.cancel();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
if (this.syncInteraction && this.syncInteraction.cancel) {
|
|
|
|
this.syncInteraction.cancel();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.subSubscription && this.subSubscription.unsubscribe) {
|
|
|
|
this.subSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
getHeader = () => {
|
|
|
|
const { isSearching } = this.state;
|
|
|
|
const {
|
|
|
|
navigation, isMasterDetail, insets, theme
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (isSearching) {
|
2020-11-04 19:13:29 +00:00
|
|
|
const headerTitlePosition = getHeaderTitlePosition({ insets, numIconsRight: 1 });
|
2020-10-30 17:35:07 +00:00
|
|
|
return {
|
|
|
|
headerTitleAlign: 'left',
|
|
|
|
headerLeft: () => (
|
|
|
|
<HeaderButton.Container left>
|
|
|
|
<HeaderButton.Item
|
|
|
|
iconName='close'
|
|
|
|
onPress={this.onCancelSearchPress}
|
|
|
|
/>
|
|
|
|
</HeaderButton.Container>
|
|
|
|
),
|
|
|
|
headerTitle: () => <SearchHeader onSearchChangeText={this.onSearchChangeText} />,
|
|
|
|
headerTitleContainerStyle: {
|
|
|
|
left: headerTitlePosition.left,
|
|
|
|
right: headerTitlePosition.right
|
|
|
|
},
|
|
|
|
headerRight: () => null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
headerLeft: () => (
|
|
|
|
<HeaderBackButton
|
|
|
|
labelVisible={false}
|
|
|
|
onPress={() => navigation.pop()}
|
|
|
|
tintColor={themes[theme].headerTintColor}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
headerTitleAlign: 'center',
|
|
|
|
headerTitle: I18n.t('Threads'),
|
|
|
|
headerTitleContainerStyle: {
|
|
|
|
left: null,
|
|
|
|
right: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isMasterDetail) {
|
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.headerRight = () => (
|
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item iconName='search' onPress={this.onSearchPress} />
|
|
|
|
</HeaderButton.Container>
|
|
|
|
);
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHeader = () => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
const options = this.getHeader();
|
|
|
|
navigation.setOptions(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
initSubscription = async() => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
// subscription query
|
2020-04-06 19:07:40 +00:00
|
|
|
const subscription = await db.collections
|
2019-09-16 20:26:32 +00:00
|
|
|
.get('subscriptions')
|
2020-04-06 19:07:40 +00:00
|
|
|
.find(this.rid);
|
|
|
|
const observable = subscription.observe();
|
|
|
|
this.subSubscription = observable
|
2019-09-16 20:26:32 +00:00
|
|
|
.subscribe((data) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
this.setState({ subscription: data });
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
this.subscribeMessages(subscription);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeMessages = (subscription, searchText) => {
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
|
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
const whereClause = [
|
|
|
|
Q.where('rid', this.rid),
|
|
|
|
Q.experimentalSortBy('tlm', Q.desc)
|
|
|
|
];
|
|
|
|
|
|
|
|
if (searchText?.trim()) {
|
|
|
|
whereClause.push(Q.where('msg', Q.like(`%${ sanitizeLikeString(searchText.trim()) }%`)));
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
this.messagesObservable = db.collections
|
|
|
|
.get('threads')
|
2020-10-30 17:35:07 +00:00
|
|
|
.query(...whereClause)
|
2019-09-16 20:26:32 +00:00
|
|
|
.observeWithColumns(['updated_at']);
|
|
|
|
this.messagesSubscription = this.messagesObservable
|
2020-10-30 17:35:07 +00:00
|
|
|
.subscribe((messages) => {
|
|
|
|
const { currentFilter } = this.state;
|
|
|
|
const displayingThreads = this.getFilteredThreads(messages, subscription, currentFilter);
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.mounted) {
|
2020-10-30 17:35:07 +00:00
|
|
|
this.setState({ messages, displayingThreads });
|
2019-09-16 20:26:32 +00:00
|
|
|
} else {
|
|
|
|
this.state.messages = messages;
|
2020-10-30 17:35:07 +00:00
|
|
|
this.state.displayingThreads = displayingThreads;
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-10-30 17:35:07 +00:00
|
|
|
log(e);
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
|
|
|
|
init = () => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { subscription } = this.state;
|
|
|
|
if (!subscription) {
|
2020-07-08 16:50:07 +00:00
|
|
|
return this.load();
|
2019-06-03 12:53:06 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const lastThreadSync = new Date();
|
2020-10-30 17:35:07 +00:00
|
|
|
if (subscription.lastThreadSync) {
|
|
|
|
this.sync(subscription.lastThreadSync);
|
2019-09-16 20:26:32 +00:00
|
|
|
} else {
|
|
|
|
this.load(lastThreadSync);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 12:53:06 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
updateThreads = async({ update, remove, lastThreadSync }) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { subscription } = this.state;
|
2020-04-06 19:07:40 +00:00
|
|
|
// if there's no subscription, manage data on this.state.messages
|
|
|
|
// note: sync will never be called without subscription
|
2020-10-30 17:35:07 +00:00
|
|
|
if (!subscription) {
|
2020-04-06 19:07:40 +00:00
|
|
|
this.setState(({ messages }) => ({ messages: [...messages, ...update] }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const threadsCollection = db.collections.get('threads');
|
2020-10-30 17:35:07 +00:00
|
|
|
const allThreadsRecords = await subscription.threads.fetch();
|
2019-09-16 20:26:32 +00:00
|
|
|
let threadsToCreate = [];
|
|
|
|
let threadsToUpdate = [];
|
|
|
|
let threadsToDelete = [];
|
|
|
|
|
|
|
|
if (update && update.length) {
|
|
|
|
update = update.map(m => buildMessage(m));
|
|
|
|
// filter threads
|
|
|
|
threadsToCreate = update.filter(i1 => !allThreadsRecords.find(i2 => i1._id === i2.id));
|
|
|
|
threadsToUpdate = allThreadsRecords.filter(i1 => update.find(i2 => i1.id === i2._id));
|
|
|
|
threadsToCreate = threadsToCreate.map(thread => threadsCollection.prepareCreate(protectedFunction((t) => {
|
|
|
|
t._raw = sanitizedRaw({ id: thread._id }, threadsCollection.schema);
|
2020-10-30 17:35:07 +00:00
|
|
|
t.subscription.set(subscription);
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(t, thread);
|
|
|
|
})));
|
|
|
|
threadsToUpdate = threadsToUpdate.map((thread) => {
|
|
|
|
const newThread = update.find(t => t._id === thread.id);
|
|
|
|
return thread.prepareUpdate(protectedFunction((t) => {
|
|
|
|
Object.assign(t, newThread);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove && remove.length) {
|
|
|
|
threadsToDelete = allThreadsRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
threadsToDelete = threadsToDelete.map(t => t.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(
|
|
|
|
...threadsToCreate,
|
|
|
|
...threadsToUpdate,
|
|
|
|
...threadsToDelete,
|
2020-10-30 17:35:07 +00:00
|
|
|
subscription.prepareUpdate((s) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
s.lastThreadSync = lastThreadSync;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-24 18:36:29 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
2019-09-16 20:26:32 +00:00
|
|
|
load = debounce(async(lastThreadSync) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const {
|
|
|
|
loading, end, messages, searchText
|
|
|
|
} = this.state;
|
2019-04-24 18:36:29 +00:00
|
|
|
if (end || loading || !this.mounted) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
2019-04-24 18:36:29 +00:00
|
|
|
const result = await RocketChat.getThreadsList({
|
2020-10-30 17:35:07 +00:00
|
|
|
rid: this.rid, count: API_FETCH_COUNT, offset: messages.length, text: searchText
|
2019-04-17 17:01:03 +00:00
|
|
|
});
|
2019-04-24 18:36:29 +00:00
|
|
|
if (result.success) {
|
2020-04-06 19:07:40 +00:00
|
|
|
this.updateThreads({ update: result.threads, lastThreadSync });
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
end: result.count < API_FETCH_COUNT
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
}
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-17 17:01:03 +00:00
|
|
|
this.setState({ loading: false, end: true });
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
}, 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;
|
2019-09-16 20:26:32 +00:00
|
|
|
this.updateThreads({ update, remove, lastThreadSync: updatedSince });
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
this.setState({
|
|
|
|
loading: false
|
|
|
|
});
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-24 18:36:29 +00:00
|
|
|
this.setState({ loading: false });
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
onSearchPress = () => {
|
|
|
|
this.setState({ isSearching: true }, () => this.setHeader());
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
onCancelSearchPress = () => {
|
|
|
|
this.setState({ isSearching: false, searchText: '' }, () => {
|
|
|
|
const { subscription } = this.state;
|
|
|
|
this.setHeader();
|
|
|
|
this.subscribeMessages(subscription);
|
|
|
|
});
|
2020-01-28 13:41:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
onSearchChangeText = debounce((searchText) => {
|
|
|
|
const { subscription } = this.state;
|
|
|
|
this.setState({ searchText }, () => this.subscribeMessages(subscription, searchText));
|
|
|
|
}, 300)
|
|
|
|
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
onThreadPress = debounce((item) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { subscription } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.pop();
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
navigation.push('RoomView', {
|
2020-10-30 17:35:07 +00:00
|
|
|
rid: item.subscription.id,
|
|
|
|
tmid: item.id,
|
|
|
|
name: makeThreadName(item),
|
|
|
|
t: 'thread',
|
|
|
|
roomUserId: RocketChat.getUidDirectMessage(subscription)
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2019-05-20 20:43:50 +00:00
|
|
|
}, 1000, true)
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
getBadgeColor = (item) => {
|
|
|
|
const { subscription } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
return getBadgeColor({ subscription, theme, messageId: item?.id });
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
// helper to query threads
|
|
|
|
getFilteredThreads = (messages, subscription, currentFilter) => {
|
|
|
|
// const { currentFilter } = this.state;
|
|
|
|
const { user } = this.props;
|
|
|
|
if (currentFilter === FILTER.FOLLOWING) {
|
|
|
|
return messages?.filter(item => item?.replies?.find(u => u === user.id));
|
|
|
|
} else if (currentFilter === FILTER.UNREAD) {
|
|
|
|
return messages?.filter(item => subscription?.tunread?.includes(item?.id));
|
|
|
|
}
|
|
|
|
return messages;
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
// method to update state with filtered threads
|
|
|
|
filterThreads = () => {
|
|
|
|
const { messages, subscription } = this.state;
|
|
|
|
const displayingThreads = this.getFilteredThreads(messages, subscription);
|
|
|
|
this.setState({ displayingThreads });
|
|
|
|
}
|
|
|
|
|
|
|
|
showFilterDropdown = () => this.setState({ showFilterDropdown: true })
|
|
|
|
|
|
|
|
closeFilterDropdown = () => this.setState({ showFilterDropdown: false })
|
|
|
|
|
|
|
|
onFilterSelected = (filter) => {
|
|
|
|
const { messages, subscription } = this.state;
|
|
|
|
const displayingThreads = this.getFilteredThreads(messages, subscription, filter);
|
|
|
|
this.setState({ currentFilter: filter, displayingThreads });
|
2019-11-13 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 17:14:45 +00:00
|
|
|
toggleFollowThread = async(isFollowingThread, tmid) => {
|
|
|
|
try {
|
|
|
|
await RocketChat.toggleFollowMessage(tmid, !isFollowingThread);
|
|
|
|
EventEmitter.emit(LISTENER, { message: isFollowingThread ? I18n.t('Unfollowed_thread') : I18n.t('Following_thread') });
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
renderItem = ({ item }) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
const {
|
|
|
|
user, navigation, baseUrl, useRealName
|
|
|
|
} = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
const badgeColor = this.getBadgeColor(item);
|
2019-09-16 20:26:32 +00:00
|
|
|
return (
|
2020-10-30 17:35:07 +00:00
|
|
|
<Item
|
|
|
|
{...{
|
|
|
|
item,
|
|
|
|
user,
|
|
|
|
navigation,
|
|
|
|
baseUrl,
|
|
|
|
useRealName,
|
|
|
|
badgeColor
|
|
|
|
}}
|
|
|
|
onPress={this.onThreadPress}
|
2021-01-25 17:14:45 +00:00
|
|
|
toggleFollowThread={this.toggleFollowThread}
|
2019-09-16 20:26:32 +00:00
|
|
|
/>
|
|
|
|
);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
const { messages, currentFilter } = this.state;
|
|
|
|
if (!messages.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DropdownItemHeader currentFilter={currentFilter} onPress={this.showFilterDropdown} />
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent = () => {
|
|
|
|
const {
|
|
|
|
loading, messages, displayingThreads, currentFilter
|
|
|
|
} = this.state;
|
|
|
|
const { theme } = this.props;
|
|
|
|
if (!messages?.length || !displayingThreads?.length) {
|
|
|
|
let text;
|
|
|
|
if (currentFilter === FILTER.FOLLOWING) {
|
|
|
|
text = I18n.t('No_threads_following');
|
|
|
|
} else if (currentFilter === FILTER.UNREAD) {
|
|
|
|
text = I18n.t('No_threads_unread');
|
|
|
|
} else {
|
|
|
|
text = I18n.t('No_threads');
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{this.renderHeader()}
|
|
|
|
<NoDataFound text={text} />
|
|
|
|
</>
|
|
|
|
);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={displayingThreads}
|
|
|
|
extraData={this.state}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
|
|
|
contentContainerStyle={styles.contentContainer}
|
|
|
|
onEndReached={this.load}
|
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
maxToRenderPerBatch={5}
|
|
|
|
windowSize={10}
|
|
|
|
initialNumToRender={7}
|
|
|
|
removeClippedSubviews={isIOS}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
|
|
|
scrollIndicatorInsets={{ right: 1 }} // https://github.com/facebook/react-native/issues/26610#issuecomment-539843444
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
console.count(`${ this.constructor.name }.render calls`);
|
|
|
|
const { showFilterDropdown, currentFilter } = this.state;
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='thread-messages-view'>
|
|
|
|
<StatusBar />
|
2020-10-30 17:35:07 +00:00
|
|
|
{this.renderContent()}
|
|
|
|
{showFilterDropdown
|
|
|
|
? (
|
|
|
|
<Dropdown
|
|
|
|
currentFilter={currentFilter}
|
|
|
|
onFilterSelected={this.onFilterSelected}
|
|
|
|
onClose={this.closeFilterDropdown}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: null}
|
2019-04-17 17:01:03 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2019-09-16 20:26:32 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name,
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(withSafeAreaInsets(ThreadMessagesView)));
|