2023-03-16 23:30:58 +00:00
|
|
|
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
2022-01-24 19:18:59 +00:00
|
|
|
import { FlatList, StyleSheet } from 'react-native';
|
2022-06-01 19:46:37 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { HeaderBackButton } from '@react-navigation/elements';
|
2021-10-13 21:09:50 +00:00
|
|
|
import { RouteProp } from '@react-navigation/core';
|
2021-07-20 19:25:50 +00:00
|
|
|
|
2022-07-06 13:23:02 +00:00
|
|
|
import { IMessageFromServer, TThreadModel } from '../../definitions';
|
2022-01-17 19:55:33 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
2021-09-17 19:15:26 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from '../../lib/methods/helpers/log';
|
2022-09-16 15:19:07 +00:00
|
|
|
import { isIOS, useDebounce } from '../../lib/methods/helpers';
|
2021-09-17 19:15:26 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
|
|
|
import * as List from '../../containers/List';
|
|
|
|
import BackgroundContainer from '../../containers/BackgroundContainer';
|
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import SearchHeader from '../../containers/SearchHeader';
|
2022-01-14 17:38:08 +00:00
|
|
|
import Item from './Item';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2022-05-13 15:01:34 +00:00
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
2021-09-03 19:50:18 +00:00
|
|
|
|
2021-09-16 18:16:02 +00:00
|
|
|
const API_FETCH_COUNT = 50;
|
2021-07-20 19:25:50 +00:00
|
|
|
|
2022-01-24 19:18:59 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
contentContainer: {
|
|
|
|
marginBottom: 30
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-17 19:15:26 +00:00
|
|
|
interface IDiscussionsViewProps {
|
2022-01-17 19:55:33 +00:00
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'DiscussionsView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'DiscussionsView'>;
|
|
|
|
item: TThreadModel;
|
2021-09-17 19:15:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 12:23:57 +00:00
|
|
|
const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.ReactElement => {
|
2021-07-20 19:25:50 +00:00
|
|
|
const rid = route.params?.rid;
|
2022-01-24 18:35:06 +00:00
|
|
|
const t = route.params?.t;
|
2021-09-08 23:56:35 +00:00
|
|
|
|
2022-05-13 15:01:34 +00:00
|
|
|
const baseUrl = useAppSelector(state => state.server?.server);
|
|
|
|
const isMasterDetail = useAppSelector(state => state.app?.isMasterDetail);
|
2021-09-08 23:56:35 +00:00
|
|
|
|
2021-07-20 19:25:50 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-03-08 17:09:45 +00:00
|
|
|
const [discussions, setDiscussions] = useState<IMessageFromServer[]>([]);
|
|
|
|
const [search, setSearch] = useState<IMessageFromServer[]>([]);
|
2021-07-20 19:25:50 +00:00
|
|
|
const [isSearching, setIsSearching] = useState(false);
|
2023-03-16 23:30:58 +00:00
|
|
|
const total = useRef(0);
|
|
|
|
const searchText = useRef('');
|
|
|
|
const offset = useRef(0);
|
2021-09-08 23:56:35 +00:00
|
|
|
|
2022-05-02 12:23:57 +00:00
|
|
|
const { colors } = useTheme();
|
2021-07-20 19:25:50 +00:00
|
|
|
|
2023-03-16 23:30:58 +00:00
|
|
|
const load = async () => {
|
2021-07-21 17:29:22 +00:00
|
|
|
if (loading) {
|
2021-07-20 19:25:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getDiscussions({
|
2021-09-03 19:50:18 +00:00
|
|
|
roomId: rid,
|
2023-03-16 23:30:58 +00:00
|
|
|
offset: offset.current,
|
2021-09-08 23:56:35 +00:00
|
|
|
count: API_FETCH_COUNT,
|
2023-03-16 23:30:58 +00:00
|
|
|
text: searchText.current
|
2021-09-03 19:50:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result.success) {
|
2023-03-16 23:30:58 +00:00
|
|
|
offset.current += result.count;
|
|
|
|
total.current = result.total;
|
2021-09-03 19:50:18 +00:00
|
|
|
if (isSearching) {
|
2023-03-16 23:30:58 +00:00
|
|
|
setSearch(prevState => (offset.current ? [...prevState, ...result.messages] : result.messages));
|
2021-09-03 19:50:18 +00:00
|
|
|
} else {
|
|
|
|
setDiscussions(result.messages);
|
|
|
|
}
|
|
|
|
}
|
2021-07-20 19:25:50 +00:00
|
|
|
setLoading(false);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2021-09-03 19:50:18 +00:00
|
|
|
setLoading(false);
|
2021-07-20 19:25:50 +00:00
|
|
|
}
|
2021-07-21 17:29:22 +00:00
|
|
|
};
|
|
|
|
|
2023-03-16 23:30:58 +00:00
|
|
|
const onSearchChangeText = useDebounce((text: string) => {
|
2021-09-08 23:56:35 +00:00
|
|
|
setIsSearching(true);
|
2023-03-16 23:30:58 +00:00
|
|
|
setSearch([]);
|
|
|
|
searchText.current = text;
|
|
|
|
offset.current = 0;
|
|
|
|
load();
|
2022-09-16 15:19:07 +00:00
|
|
|
}, 500);
|
2021-07-20 19:25:50 +00:00
|
|
|
|
2021-07-21 17:29:22 +00:00
|
|
|
const onCancelSearchPress = () => {
|
|
|
|
setIsSearching(false);
|
2021-10-13 21:09:50 +00:00
|
|
|
setSearch([]);
|
2023-03-16 23:30:58 +00:00
|
|
|
searchText.current = '';
|
|
|
|
offset.current = 0;
|
2021-07-21 17:29:22 +00:00
|
|
|
};
|
2021-07-20 19:25:50 +00:00
|
|
|
|
|
|
|
const onSearchPress = () => {
|
|
|
|
setIsSearching(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setHeader = () => {
|
2021-10-13 21:09:50 +00:00
|
|
|
let options: Partial<StackNavigationOptions>;
|
2021-07-20 19:25:50 +00:00
|
|
|
if (isSearching) {
|
2021-10-13 21:09:50 +00:00
|
|
|
options = {
|
2021-07-20 19:25:50 +00:00
|
|
|
headerTitleAlign: 'left',
|
2022-07-06 13:23:02 +00:00
|
|
|
headerTitleContainerStyle: { flex: 1, marginHorizontal: 0, marginRight: 15, maxWidth: undefined },
|
|
|
|
headerRightContainerStyle: { flexGrow: 0 },
|
2021-07-20 19:25:50 +00:00
|
|
|
headerLeft: () => (
|
|
|
|
<HeaderButton.Container left>
|
2021-09-16 18:16:02 +00:00
|
|
|
<HeaderButton.Item iconName='close' onPress={onCancelSearchPress} />
|
2021-07-20 19:25:50 +00:00
|
|
|
</HeaderButton.Container>
|
|
|
|
),
|
2021-09-08 23:56:35 +00:00
|
|
|
headerTitle: () => (
|
2022-01-24 15:51:31 +00:00
|
|
|
<SearchHeader onSearchChangeText={onSearchChangeText} testID='discussion-messages-view-search-header' />
|
2021-09-08 23:56:35 +00:00
|
|
|
),
|
2021-07-20 19:25:50 +00:00
|
|
|
headerRight: () => null
|
|
|
|
};
|
2021-10-13 21:09:50 +00:00
|
|
|
return options;
|
2021-07-20 19:25:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 21:09:50 +00:00
|
|
|
options = {
|
2022-07-06 13:23:02 +00:00
|
|
|
headerTitleAlign: 'center',
|
|
|
|
headerTitle: I18n.t('Discussions'),
|
2024-01-19 20:04:16 +00:00
|
|
|
headerTitleContainerStyle: {},
|
2022-07-06 13:23:02 +00:00
|
|
|
headerRightContainerStyle: { flexGrow: 1 },
|
2021-07-20 19:25:50 +00:00
|
|
|
headerLeft: () => (
|
2022-11-16 13:26:37 +00:00
|
|
|
<HeaderBackButton
|
|
|
|
labelVisible={false}
|
|
|
|
onPress={() => navigation.pop()}
|
|
|
|
tintColor={colors.headerTintColor}
|
|
|
|
testID='header-back'
|
|
|
|
/>
|
2021-07-20 19:25:50 +00:00
|
|
|
),
|
2021-09-17 19:15:26 +00:00
|
|
|
headerRight: () => (
|
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item iconName='search' onPress={onSearchPress} />
|
|
|
|
</HeaderButton.Container>
|
|
|
|
)
|
2021-07-20 19:25:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (isMasterDetail) {
|
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} />;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-07-21 17:29:22 +00:00
|
|
|
load();
|
2021-07-21 20:23:34 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-01-24 15:51:31 +00:00
|
|
|
useLayoutEffect(() => {
|
2021-07-21 17:29:22 +00:00
|
|
|
const options = setHeader();
|
|
|
|
navigation.setOptions(options);
|
2021-07-21 20:23:34 +00:00
|
|
|
}, [navigation, isSearching]);
|
2021-07-20 19:25:50 +00:00
|
|
|
|
2022-09-16 15:19:07 +00:00
|
|
|
const onDiscussionPress = (item: TThreadModel) => {
|
|
|
|
if (item.drid && item.t) {
|
|
|
|
navigation.push('RoomView', {
|
|
|
|
rid: item.drid,
|
|
|
|
prid: item.rid,
|
|
|
|
name: item.msg,
|
|
|
|
t
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2021-07-20 19:25:50 +00:00
|
|
|
|
2022-03-08 17:09:45 +00:00
|
|
|
const renderItem = ({ item }: { item: IMessageFromServer }) => (
|
2021-09-17 19:15:26 +00:00
|
|
|
<Item
|
|
|
|
{...{
|
|
|
|
item,
|
2022-01-14 17:38:08 +00:00
|
|
|
baseUrl
|
2021-09-17 19:15:26 +00:00
|
|
|
}}
|
|
|
|
onPress={onDiscussionPress}
|
2021-07-21 17:29:22 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-17 19:15:26 +00:00
|
|
|
|
2021-07-20 19:25:50 +00:00
|
|
|
if (!discussions?.length) {
|
2021-09-16 18:16:02 +00:00
|
|
|
return <BackgroundContainer loading={loading} text={I18n.t('No_discussions')} />;
|
2021-07-20 19:25:50 +00:00
|
|
|
}
|
2022-01-31 21:21:26 +00:00
|
|
|
|
2021-07-20 19:25:50 +00:00
|
|
|
return (
|
2021-09-17 19:26:48 +00:00
|
|
|
<SafeAreaView testID='discussions-view'>
|
2021-07-20 19:25:50 +00:00
|
|
|
<StatusBar />
|
|
|
|
<FlatList
|
2021-09-03 19:50:18 +00:00
|
|
|
data={isSearching ? search : discussions}
|
2021-07-20 19:25:50 +00:00
|
|
|
renderItem={renderItem}
|
2023-03-16 23:30:58 +00:00
|
|
|
keyExtractor={(item: any) => item._id}
|
2022-05-02 12:23:57 +00:00
|
|
|
style={{ backgroundColor: colors.backgroundColor }}
|
2021-09-17 19:15:26 +00:00
|
|
|
contentContainerStyle={styles.contentContainer}
|
2021-07-20 19:25:50 +00:00
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
removeClippedSubviews={isIOS}
|
2023-03-16 23:30:58 +00:00
|
|
|
onEndReached={() => isSearching && offset.current < total.current && load()}
|
2021-07-20 19:25:50 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2022-03-18 02:37:10 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator /> : null}
|
2021-08-23 01:51:22 +00:00
|
|
|
scrollIndicatorInsets={{ right: 1 }}
|
2021-07-20 19:25:50 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-16 18:16:02 +00:00
|
|
|
export default DiscussionsView;
|