2019-04-17 17:01:03 +00:00
|
|
|
import React from 'react';
|
2021-02-23 18:36:20 +00:00
|
|
|
import { FlatList } 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';
|
2022-01-12 13:53:06 +00:00
|
|
|
import { EdgeInsets, withSafeAreaInsets } from 'react-native-safe-area-context';
|
|
|
|
import { HeaderBackButton, StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
|
|
|
import { Observable, Subscription } from 'rxjs';
|
|
|
|
import Model from '@nozbe/watermelondb/Model';
|
|
|
|
import Database from '@nozbe/watermelondb/Database';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
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';
|
2021-04-07 18:31:25 +00:00
|
|
|
import BackgroundContainer from '../../containers/BackgroundContainer';
|
2020-10-30 17:35:07 +00:00
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
|
|
|
import { getBadgeColor, makeThreadName } from '../../utils/room';
|
|
|
|
import { getHeaderTitlePosition } from '../../containers/Header';
|
2021-01-14 17:06:19 +00:00
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import { LISTENER } from '../../containers/Toast';
|
2021-09-08 23:56:35 +00:00
|
|
|
import SearchHeader from '../../containers/SearchHeader';
|
2022-01-12 13:53:06 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
|
|
|
import { IThreadResult, TThreadModel } from '../../definitions/IThread';
|
|
|
|
import { Filter } from './filters';
|
2021-09-13 20:41:05 +00:00
|
|
|
import DropdownItemHeader from './Dropdown/DropdownItemHeader';
|
|
|
|
import Dropdown from './Dropdown';
|
|
|
|
import Item from './Item';
|
|
|
|
import styles from './styles';
|
2022-01-12 13:53:06 +00:00
|
|
|
import { SubscriptionType, TSubscriptionModel } from '../../definitions/ISubscription';
|
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
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
interface IResultFetch {
|
|
|
|
threads: IThreadResult[];
|
|
|
|
count: number;
|
|
|
|
offset: number;
|
|
|
|
total: number;
|
|
|
|
success: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IThreadMessagesViewState {
|
|
|
|
loading: boolean;
|
|
|
|
end: boolean;
|
|
|
|
messages: any[];
|
|
|
|
displayingThreads: TThreadModel[];
|
|
|
|
subscription: TSubscriptionModel;
|
|
|
|
showFilterDropdown: boolean;
|
|
|
|
currentFilter: Filter;
|
|
|
|
isSearching: boolean;
|
|
|
|
searchText: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IThreadMessagesViewProps {
|
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'ThreadMessagesView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'ThreadMessagesView'>;
|
|
|
|
user: any;
|
|
|
|
baseUrl: string;
|
|
|
|
useRealName: boolean;
|
|
|
|
theme: string;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
insets: EdgeInsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThreadMessagesView extends React.Component<IThreadMessagesViewProps, IThreadMessagesViewState> {
|
|
|
|
private mounted: boolean;
|
|
|
|
|
|
|
|
private rid: string;
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
private t: string;
|
|
|
|
|
|
|
|
private subSubscription: any;
|
|
|
|
|
|
|
|
private messagesSubscription?: Subscription;
|
|
|
|
|
|
|
|
private messagesObservable!: Observable<Model>;
|
|
|
|
|
|
|
|
constructor(props: IThreadMessagesViewProps) {
|
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: [],
|
2022-01-12 13:53:06 +00:00
|
|
|
subscription: {} as TSubscriptionModel,
|
2020-10-30 17:35:07 +00:00
|
|
|
showFilterDropdown: false,
|
2022-01-12 13:53:06 +00:00
|
|
|
currentFilter: Filter.All,
|
2020-10-30 17:35:07 +00:00
|
|
|
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;
|
2021-02-23 18:36:20 +00:00
|
|
|
this.init();
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
componentDidUpdate(prevProps: IThreadMessagesViewProps) {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { insets } = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
if (insets.left !== prevProps.insets.left || insets.right !== prevProps.insets.right) {
|
|
|
|
this.setHeader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
componentWillUnmount() {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.countReset(`${this.constructor.name}.render calls`);
|
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
|
|
|
}
|
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
getHeader = (): StackNavigationOptions => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { isSearching } = this.state;
|
2021-09-13 20:41:05 +00:00
|
|
|
const { navigation, isMasterDetail, insets, theme } = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
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>
|
2021-09-13 20:41:05 +00:00
|
|
|
<HeaderButton.Item iconName='close' onPress={this.onCancelSearchPress} />
|
2020-10-30 17:35:07 +00:00
|
|
|
</HeaderButton.Container>
|
|
|
|
),
|
2021-09-17 19:15:26 +00:00
|
|
|
headerTitle: () => (
|
2022-01-24 15:51:31 +00:00
|
|
|
<SearchHeader onSearchChangeText={this.onSearchChangeText} testID='thread-messages-view-search-header' />
|
2021-09-17 19:15:26 +00:00
|
|
|
),
|
2020-10-30 17:35:07 +00:00
|
|
|
headerTitleContainerStyle: {
|
|
|
|
left: headerTitlePosition.left,
|
|
|
|
right: headerTitlePosition.right
|
|
|
|
},
|
|
|
|
headerRight: () => null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
const options: StackNavigationOptions = {
|
2020-10-30 17:35:07 +00:00
|
|
|
headerLeft: () => (
|
2021-09-13 20:41:05 +00:00
|
|
|
<HeaderBackButton labelVisible={false} onPress={() => navigation.pop()} tintColor={themes[theme].headerTintColor} />
|
2020-10-30 17:35:07 +00:00
|
|
|
),
|
|
|
|
headerTitleAlign: 'center',
|
2021-07-21 21:22:03 +00:00
|
|
|
headerTitle: I18n.t('Threads'),
|
2020-10-30 17:35:07 +00:00
|
|
|
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;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
setHeader = () => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
const options = this.getHeader();
|
|
|
|
navigation.setOptions(options);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
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
|
2022-01-12 13:53:06 +00:00
|
|
|
const subscription = (await db.collections.get('subscriptions').find(this.rid)) as TSubscriptionModel;
|
2020-04-06 19:07:40 +00:00
|
|
|
const observable = subscription.observe();
|
2021-09-13 20:41:05 +00:00
|
|
|
this.subSubscription = observable.subscribe(data => {
|
|
|
|
this.setState({ subscription: data });
|
|
|
|
});
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
this.subscribeMessages(subscription);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
subscribeMessages = (subscription?: TSubscriptionModel, searchText?: string) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
|
|
|
|
if (this.messagesSubscription && this.messagesSubscription.unsubscribe) {
|
|
|
|
this.messagesSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const whereClause = [Q.where('rid', this.rid), Q.experimentalSortBy('tlm', Q.desc)];
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
if (searchText?.trim()) {
|
2021-09-13 20:41:05 +00:00
|
|
|
whereClause.push(Q.where('msg', Q.like(`%${sanitizeLikeString(searchText.trim())}%`)));
|
2020-10-30 17:35:07 +00:00
|
|
|
}
|
|
|
|
|
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']);
|
2022-01-12 13:53:06 +00:00
|
|
|
|
|
|
|
// TODO: Refactor when migrate messages
|
|
|
|
this.messagesSubscription = this.messagesObservable.subscribe((messages: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { currentFilter } = this.state;
|
2022-01-12 13:53:06 +00:00
|
|
|
const displayingThreads = this.getFilteredThreads(messages, subscription!, currentFilter);
|
2021-09-13 20:41:05 +00:00
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ messages, displayingThreads });
|
|
|
|
} else {
|
2022-01-12 13:53:06 +00:00
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +00:00
|
|
|
this.state.messages = messages;
|
2022-01-12 13:53:06 +00:00
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +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
|
|
|
}
|
2021-09-13 20:41:05 +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);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-03 12:53:06 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
updateThreads = async ({
|
|
|
|
update,
|
|
|
|
remove,
|
|
|
|
lastThreadSync
|
|
|
|
}: {
|
|
|
|
update: IThreadResult[];
|
|
|
|
remove?: IThreadResult[];
|
|
|
|
lastThreadSync: Date;
|
|
|
|
}) => {
|
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 {
|
2022-01-12 13:53:06 +00:00
|
|
|
const db: Database = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const threadsCollection = db.get('threads');
|
2022-01-12 13:53:06 +00:00
|
|
|
// TODO: Refactor when migrate room
|
|
|
|
// @ts-ignore
|
|
|
|
const allThreadsRecords = (await subscription.threads.fetch()) as TThreadModel[];
|
|
|
|
let threadsToCreate: any[] = [];
|
|
|
|
let threadsToUpdate: any[] = [];
|
|
|
|
let threadsToDelete: any[] = [];
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
if (update && update.length) {
|
|
|
|
update = update.map(m => buildMessage(m));
|
|
|
|
// filter threads
|
2022-01-19 20:54:12 +00:00
|
|
|
threadsToCreate = update.filter(i1 => !allThreadsRecords.find((i2: { id: string }) => i1._id === i2.id));
|
2022-01-12 13:53:06 +00:00
|
|
|
threadsToUpdate = allThreadsRecords.filter((i1: { id: string }) => update.find(i2 => i1.id === i2._id));
|
2021-09-13 20:41:05 +00:00
|
|
|
threadsToCreate = threadsToCreate.map(thread =>
|
|
|
|
threadsCollection.prepareCreate(
|
2022-01-12 13:53:06 +00:00
|
|
|
protectedFunction((t: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
t._raw = sanitizedRaw({ id: thread._id }, threadsCollection.schema);
|
|
|
|
t.subscription.set(subscription);
|
|
|
|
Object.assign(t, thread);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
threadsToUpdate = threadsToUpdate.map(thread => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const newThread = update.find(t => t._id === thread.id);
|
2021-09-13 20:41:05 +00:00
|
|
|
return thread.prepareUpdate(
|
2022-01-12 13:53:06 +00:00
|
|
|
protectedFunction((t: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(t, newThread);
|
|
|
|
})
|
|
|
|
);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove && remove.length) {
|
2022-01-12 13:53:06 +00:00
|
|
|
threadsToDelete = allThreadsRecords.filter((i1: { id: string }) => remove.find(i2 => i1.id === i2._id));
|
2019-09-16 20:26:32 +00:00
|
|
|
threadsToDelete = threadsToDelete.map(t => t.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
await db.write(async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.batch(
|
|
|
|
...threadsToCreate,
|
|
|
|
...threadsToUpdate,
|
|
|
|
...threadsToDelete,
|
2022-01-12 13:53:06 +00:00
|
|
|
subscription.prepareUpdate((s: any) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
s.lastThreadSync = lastThreadSync;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-24 18:36:29 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
2022-01-12 13:53:06 +00:00
|
|
|
load = debounce(async (lastThreadSync: Date) => {
|
2021-09-13 20:41:05 +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 });
|
|
|
|
|
2021-07-21 21:22:03 +00:00
|
|
|
try {
|
2022-01-12 13:53:06 +00:00
|
|
|
const result: IResultFetch = await RocketChat.getThreadsList({
|
2021-09-13 20:41:05 +00:00
|
|
|
rid: this.rid,
|
|
|
|
count: API_FETCH_COUNT,
|
|
|
|
offset: messages.length,
|
|
|
|
text: searchText
|
2021-07-21 21:22:03 +00:00
|
|
|
});
|
|
|
|
if (result.success) {
|
|
|
|
this.updateThreads({ update: result.threads, lastThreadSync });
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
end: result.count < API_FETCH_COUNT
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
}
|
2021-07-21 21:22:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
this.setState({ loading: false, end: true });
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
}, 300);
|
2019-04-24 18:36:29 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
2022-01-12 13:53:06 +00:00
|
|
|
sync = async (updatedSince: Date) => {
|
2019-04-24 18:36:29 +00:00
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.getSyncThreadsList({
|
2021-09-13 20:41:05 +00:00
|
|
|
rid: this.rid,
|
|
|
|
updatedSince: updatedSince.toISOString()
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
if (result.success && result.threads) {
|
2021-02-23 18:36:20 +00:00
|
|
|
const { update, remove } = result.threads;
|
|
|
|
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 });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
onSearchPress = () => {
|
|
|
|
this.setState({ isSearching: true }, () => this.setHeader());
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
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);
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-01-28 13:41:06 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
onSearchChangeText = debounce((searchText: string) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { subscription } = this.state;
|
|
|
|
this.setState({ searchText }, () => this.subscribeMessages(subscription, searchText));
|
2021-09-13 20:41:05 +00:00
|
|
|
}, 300);
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
onThreadPress = debounce(
|
2022-01-12 13:53:06 +00:00
|
|
|
(item: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { subscription } = this.state;
|
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.pop();
|
|
|
|
}
|
|
|
|
navigation.push('RoomView', {
|
|
|
|
rid: item.subscription.id,
|
|
|
|
tmid: item.id,
|
|
|
|
name: makeThreadName(item),
|
2022-01-12 13:53:06 +00:00
|
|
|
t: SubscriptionType.THREAD,
|
2021-09-13 20:41:05 +00:00
|
|
|
roomUserId: RocketChat.getUidDirectMessage(subscription)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
1000,
|
|
|
|
true
|
|
|
|
);
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
getBadgeColor = (item: TThreadModel) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
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 });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
// helper to query threads
|
2022-01-12 13:53:06 +00:00
|
|
|
getFilteredThreads = (messages: any, subscription: TSubscriptionModel, currentFilter?: Filter): TThreadModel[] => {
|
2020-10-30 17:35:07 +00:00
|
|
|
// const { currentFilter } = this.state;
|
|
|
|
const { user } = this.props;
|
2022-01-12 13:53:06 +00:00
|
|
|
if (currentFilter === Filter.Following) {
|
|
|
|
return messages?.filter((item: { replies: any[] }) => item?.replies?.find(u => u === user.id));
|
|
|
|
}
|
|
|
|
if (currentFilter === Filter.Unread) {
|
|
|
|
return messages?.filter((item: { id: string }) => subscription?.tunread?.includes(item?.id));
|
2020-10-30 17:35:07 +00:00
|
|
|
}
|
|
|
|
return messages;
|
2021-09-13 20:41:05 +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 });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
showFilterDropdown = () => this.setState({ showFilterDropdown: true });
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
closeFilterDropdown = () => this.setState({ showFilterDropdown: false });
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
onFilterSelected = (filter: Filter) => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { messages, subscription } = this.state;
|
|
|
|
const displayingThreads = this.getFilteredThreads(messages, subscription, filter);
|
|
|
|
this.setState({ currentFilter: filter, displayingThreads });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-11-13 19:04:49 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
toggleFollowThread = async (isFollowingThread: boolean, tmid: string) => {
|
2021-01-14 17:06:19 +00:00
|
|
|
try {
|
|
|
|
await RocketChat.toggleFollowMessage(tmid, !isFollowingThread);
|
|
|
|
EventEmitter.emit(LISTENER, { message: isFollowingThread ? I18n.t('Unfollowed_thread') : I18n.t('Following_thread') });
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2021-01-14 17:06:19 +00:00
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
renderItem = ({ item }: { item: TThreadModel }) => {
|
2021-09-13 20:41:05 +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-14 17:06:19 +00:00
|
|
|
toggleFollowThread={this.toggleFollowThread}
|
2019-09-16 20:26:32 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
const { messages, currentFilter } = this.state;
|
2021-07-21 21:22:03 +00:00
|
|
|
if (!messages.length) {
|
2020-10-30 17:35:07 +00:00
|
|
|
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 />
|
|
|
|
</>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
renderContent = () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { loading, messages, displayingThreads, currentFilter } = this.state;
|
2020-10-30 17:35:07 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (!messages?.length || !displayingThreads?.length) {
|
|
|
|
let text;
|
2022-01-12 13:53:06 +00:00
|
|
|
if (currentFilter === Filter.Following) {
|
2020-10-30 17:35:07 +00:00
|
|
|
text = I18n.t('No_threads_following');
|
2022-01-12 13:53:06 +00:00
|
|
|
} else if (currentFilter === Filter.Unread) {
|
2020-10-30 17:35:07 +00:00
|
|
|
text = I18n.t('No_threads_unread');
|
|
|
|
} else {
|
2021-07-21 21:22:03 +00:00
|
|
|
text = I18n.t('No_threads');
|
2020-10-30 17:35:07 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{this.renderHeader()}
|
2021-04-07 18:31:25 +00:00
|
|
|
<BackgroundContainer text={text} />
|
2020-10-30 17:35:07 +00:00
|
|
|
</>
|
|
|
|
);
|
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
|
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
render() {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.count(`${this.constructor.name}.render calls`);
|
2020-10-30 17:35:07 +00:00
|
|
|
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()}
|
2021-09-13 20:41:05 +00:00
|
|
|
{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
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
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,
|
2022-01-17 20:15:58 +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)));
|