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-06-01 19:46:37 +00:00
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
|
|
|
import { HeaderBackButton } from '@react-navigation/elements';
|
2022-01-12 13:53:06 +00:00
|
|
|
import { Observable, Subscription } from 'rxjs';
|
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';
|
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';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from '../../lib/methods/helpers/log';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from '../../lib/methods/helpers/protectedFunction';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, 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';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { getBadgeColor, makeThreadName } from '../../lib/methods/helpers/room';
|
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
2021-01-14 17:06:19 +00:00
|
|
|
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 { 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-05-07 02:34:58 +00:00
|
|
|
import { IApplicationState, IBaseScreen, IMessage, SubscriptionType, TSubscriptionModel, TThreadModel } from '../../definitions';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { getUidDirectMessage, debounce, isIOS } from '../../lib/methods/helpers';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2023-04-10 19:41:49 +00:00
|
|
|
import KeyboardView from '../../containers/KeyboardView';
|
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 IThreadMessagesViewState {
|
|
|
|
loading: boolean;
|
|
|
|
end: boolean;
|
|
|
|
messages: any[];
|
|
|
|
displayingThreads: TThreadModel[];
|
|
|
|
subscription: TSubscriptionModel;
|
|
|
|
showFilterDropdown: boolean;
|
|
|
|
currentFilter: Filter;
|
|
|
|
isSearching: boolean;
|
|
|
|
searchText: string;
|
|
|
|
}
|
|
|
|
|
2022-05-07 02:34:58 +00:00
|
|
|
interface IThreadMessagesViewProps extends IBaseScreen<ChatsStackParamList, 'ThreadMessagesView'> {
|
|
|
|
user: { id: string };
|
2022-01-12 13:53:06 +00:00
|
|
|
baseUrl: string;
|
|
|
|
useRealName: boolean;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-01-12 13:53:06 +00:00
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-05-07 02:34:58 +00:00
|
|
|
private subSubscription?: Subscription;
|
2022-01-12 13:53:06 +00:00
|
|
|
|
|
|
|
private messagesSubscription?: Subscription;
|
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
private messagesObservable?: Observable<TThreadModel[]>;
|
2022-01-12 13:53:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
2022-07-06 13:23:02 +00:00
|
|
|
const { navigation, isMasterDetail, theme } = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
|
|
|
|
if (isSearching) {
|
|
|
|
return {
|
|
|
|
headerTitleAlign: 'left',
|
2022-07-06 13:23:02 +00:00
|
|
|
headerTitleContainerStyle: { flex: 1, marginHorizontal: 0, marginRight: 15, maxWidth: undefined },
|
|
|
|
headerRightContainerStyle: { flexGrow: 0 },
|
2020-10-30 17:35:07 +00:00
|
|
|
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
|
|
|
headerRight: () => null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:53:06 +00:00
|
|
|
const options: StackNavigationOptions = {
|
2022-07-06 13:23:02 +00:00
|
|
|
headerTitleAlign: 'center',
|
|
|
|
headerTitle: I18n.t('Threads'),
|
|
|
|
headerRightContainerStyle: { flexGrow: 1 },
|
2020-10-30 17:35:07 +00:00
|
|
|
headerLeft: () => (
|
2022-11-16 13:26:37 +00:00
|
|
|
<HeaderBackButton
|
|
|
|
labelVisible={false}
|
|
|
|
onPress={() => navigation.pop()}
|
|
|
|
tintColor={themes[theme].headerTintColor}
|
|
|
|
testID='header-back'
|
|
|
|
/>
|
2020-10-30 17:35:07 +00:00
|
|
|
),
|
2022-07-06 13:23:02 +00:00
|
|
|
headerRight: () => (
|
|
|
|
<HeaderButton.Container>
|
2022-12-02 17:06:14 +00:00
|
|
|
<HeaderButton.Item iconName='search' onPress={this.onSearchPress} testID='thread-messages-view-search-icon' />
|
2022-07-06 13:23:02 +00:00
|
|
|
</HeaderButton.Container>
|
|
|
|
)
|
2020-10-30 17:35:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (isMasterDetail) {
|
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-01 13:39:09 +00:00
|
|
|
const subscription = await db.get('subscriptions').find(this.rid);
|
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
|
|
|
}
|
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
this.messagesObservable = db
|
2019-09-16 20:26:32 +00:00
|
|
|
.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
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
this.messagesSubscription = this.messagesObservable.subscribe(messages => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { currentFilter } = this.state;
|
2022-02-01 13:39:09 +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
|
|
|
|
}: {
|
2022-03-03 20:25:03 +00:00
|
|
|
update: IMessage[];
|
|
|
|
remove?: IMessage[];
|
2022-01-12 13:53:06 +00:00
|
|
|
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
|
2022-12-02 17:06:14 +00:00
|
|
|
if (!subscription._id) {
|
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-03-03 20:25:03 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const threadsCollection = db.get('threads');
|
2022-03-03 20:25:03 +00:00
|
|
|
const allThreadsRecords = await subscription.threads.fetch();
|
2022-05-07 02:34:58 +00:00
|
|
|
let threadsToCreate: TThreadModel[] = [];
|
|
|
|
let threadsToUpdate: (TThreadModel | null | undefined)[] = [];
|
|
|
|
let threadsToDelete: TThreadModel[] = [];
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-02-10 20:16:10 +00:00
|
|
|
if (remove && remove.length) {
|
|
|
|
threadsToDelete = allThreadsRecords.filter((i1: { id: string }) => remove.find(i2 => i1.id === i2._id));
|
|
|
|
threadsToDelete = threadsToDelete.map(t => t.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
if (update && update.length) {
|
2022-03-03 20:25:03 +00:00
|
|
|
update = update.map(m => buildMessage(m)) as IMessage[];
|
2019-09-16 20:26:32 +00:00
|
|
|
// filter threads
|
2022-05-07 02:34:58 +00:00
|
|
|
threadsToCreate = update.filter(
|
|
|
|
i1 => !allThreadsRecords.find((i2: { id: string }) => i1._id === i2.id)
|
|
|
|
) as TThreadModel[];
|
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 => {
|
2022-05-07 02:34:58 +00:00
|
|
|
const newThread = update.find(t => t._id === thread?.id);
|
2022-02-10 20:16:10 +00:00
|
|
|
try {
|
2022-05-07 02:34:58 +00:00
|
|
|
return thread?.prepareUpdate(
|
|
|
|
protectedFunction((t: TThreadModel) => {
|
2022-02-10 20:16:10 +00:00
|
|
|
Object.assign(t, newThread);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-05-07 02:34:58 +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
|
|
|
}
|
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-04-28 20:37:25 +00:00
|
|
|
const result = await Services.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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.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,
|
2022-04-28 20:37:25 +00:00
|
|
|
roomUserId: getUidDirectMessage(subscription)
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
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-02-01 13:39:09 +00:00
|
|
|
getFilteredThreads = (messages: TThreadModel[], subscription?: TSubscriptionModel, currentFilter?: Filter): TThreadModel[] => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { user } = this.props;
|
2022-01-12 13:53:06 +00:00
|
|
|
if (currentFilter === Filter.Following) {
|
2022-02-01 13:39:09 +00:00
|
|
|
return messages?.filter(item => item?.replies?.find(u => u === user.id));
|
2022-01-12 13:53:06 +00:00
|
|
|
}
|
|
|
|
if (currentFilter === Filter.Unread) {
|
2022-02-01 13:39:09 +00:00
|
|
|
return messages?.filter(item => 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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.toggleFollowMessage(tmid, !isFollowingThread);
|
2021-01-14 17:06:19 +00:00
|
|
|
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 }) => {
|
2022-02-17 15:27:01 +00:00
|
|
|
const { user, navigation, 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,
|
|
|
|
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}
|
2022-03-18 02:37:10 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator /> : null}
|
2020-10-30 17:35:07 +00:00
|
|
|
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;
|
2022-05-07 02:34:58 +00:00
|
|
|
const { theme } = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='thread-messages-view'>
|
2023-04-10 19:41:49 +00:00
|
|
|
<KeyboardView>
|
|
|
|
<StatusBar />
|
|
|
|
{this.renderContent()}
|
|
|
|
{showFilterDropdown ? (
|
|
|
|
<Dropdown
|
|
|
|
currentFilter={currentFilter}
|
|
|
|
onFilterSelected={this.onFilterSelected}
|
|
|
|
onClose={this.closeFilterDropdown}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</KeyboardView>
|
2019-04-17 17:01:03 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-05-07 02:34:58 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2022-05-07 02:34:58 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name as boolean,
|
2022-01-17 20:15:58 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2022-07-06 13:23:02 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(ThreadMessagesView));
|