Rocket.Chat.ReactNative/app/views/DiscussionsView/index.tsx

209 lines
5.8 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useLayoutEffect, useState } from 'react';
2022-01-24 19:18:59 +00:00
import { FlatList, StyleSheet } from 'react-native';
import { useSelector } from 'react-redux';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2021-10-13 21:09:50 +00:00
import { HeaderBackButton, StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/core';
import { IApplicationState, IMessageFromServer } from '../../definitions';
import { ChatsStackParamList } from '../../stacks/types';
import ActivityIndicator from '../../containers/ActivityIndicator';
import I18n from '../../i18n';
import StatusBar from '../../containers/StatusBar';
import log from '../../utils/log';
import debounce from '../../utils/debounce';
import { themes } from '../../lib/constants';
import SafeAreaView from '../../containers/SafeAreaView';
import * as HeaderButton from '../../containers/HeaderButton';
import * as List from '../../containers/List';
import BackgroundContainer from '../../containers/BackgroundContainer';
import { isIOS } from '../../utils/deviceInfo';
import { getHeaderTitlePosition } from '../../containers/Header';
import { useTheme } from '../../theme';
import RocketChat from '../../lib/rocketchat';
import SearchHeader from '../../containers/SearchHeader';
import { TThreadModel } from '../../definitions/IThread';
import Item from './Item';
2021-09-03 19:50:18 +00:00
2021-09-16 18:16:02 +00:00
const API_FETCH_COUNT = 50;
2022-01-24 19:18:59 +00:00
const styles = StyleSheet.create({
contentContainer: {
marginBottom: 30
}
});
interface IDiscussionsViewProps {
navigation: StackNavigationProp<ChatsStackParamList, 'DiscussionsView'>;
route: RouteProp<ChatsStackParamList, 'DiscussionsView'>;
item: TThreadModel;
}
const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): JSX.Element => {
const rid = route.params?.rid;
const t = route.params?.t;
const baseUrl = useSelector((state: IApplicationState) => state.server?.server);
const isMasterDetail = useSelector((state: IApplicationState) => state.app?.isMasterDetail);
const [loading, setLoading] = useState(false);
const [discussions, setDiscussions] = useState<IMessageFromServer[]>([]);
const [search, setSearch] = useState<IMessageFromServer[]>([]);
const [isSearching, setIsSearching] = useState(false);
2021-09-16 18:16:02 +00:00
const [total, setTotal] = useState(0);
const [searchTotal, setSearchTotal] = useState(0);
2021-07-21 17:29:22 +00:00
const { theme } = useTheme();
const insets = useSafeAreaInsets();
2021-09-16 18:16:02 +00:00
const load = async (text = '') => {
2021-07-21 17:29:22 +00:00
if (loading) {
return;
}
setLoading(true);
try {
const result = await RocketChat.getDiscussions({
2021-09-03 19:50:18 +00:00
roomId: rid,
offset: isSearching ? search.length : discussions.length,
count: API_FETCH_COUNT,
text
2021-09-03 19:50:18 +00:00
});
if (result.success) {
if (isSearching) {
setSearch(result.messages);
setSearchTotal(result.total);
2021-09-03 19:50:18 +00:00
} else {
setDiscussions(result.messages);
2021-09-16 18:16:02 +00:00
setTotal(result.total);
2021-09-03 19:50:18 +00:00
}
}
setLoading(false);
} catch (e) {
log(e);
2021-09-03 19:50:18 +00:00
setLoading(false);
}
2021-07-21 17:29:22 +00:00
};
const onSearchChangeText = debounce(async (text: string) => {
setIsSearching(true);
await load(text);
}, 300);
2021-07-21 17:29:22 +00:00
const onCancelSearchPress = () => {
setIsSearching(false);
2021-10-13 21:09:50 +00:00
setSearch([]);
setSearchTotal(0);
2021-07-21 17:29:22 +00:00
};
const onSearchPress = () => {
setIsSearching(true);
};
const setHeader = () => {
2021-10-13 21:09:50 +00:00
let options: Partial<StackNavigationOptions>;
if (isSearching) {
const headerTitlePosition = getHeaderTitlePosition({ insets, numIconsRight: 1 });
2021-10-13 21:09:50 +00:00
options = {
headerTitleAlign: 'left',
headerLeft: () => (
<HeaderButton.Container left>
2021-09-16 18:16:02 +00:00
<HeaderButton.Item iconName='close' onPress={onCancelSearchPress} />
</HeaderButton.Container>
),
headerTitle: () => (
<SearchHeader onSearchChangeText={onSearchChangeText} testID='discussion-messages-view-search-header' />
),
headerTitleContainerStyle: {
left: headerTitlePosition.left,
right: headerTitlePosition.right
},
headerRight: () => null
};
2021-10-13 21:09:50 +00:00
return options;
}
2021-10-13 21:09:50 +00:00
options = {
headerLeft: () => (
2022-01-24 19:18:59 +00:00
<HeaderBackButton labelVisible={false} onPress={() => navigation.pop()} tintColor={themes[theme].headerTintColor} />
),
headerTitleAlign: 'center',
2021-07-21 17:29:22 +00:00
headerTitle: I18n.t('Discussions'),
headerTitleContainerStyle: {
left: 0,
right: 0
},
headerRight: () => (
<HeaderButton.Container>
<HeaderButton.Item iconName='search' onPress={onSearchPress} />
</HeaderButton.Container>
)
};
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
}, []);
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-09-16 18:16:02 +00:00
const onDiscussionPress = debounce(
(item: TThreadModel) => {
if (item.drid && item.t) {
navigation.push('RoomView', {
rid: item.drid,
prid: item.rid,
name: item.msg,
t
});
}
2021-09-16 18:16:02 +00:00
},
1000,
true
);
const renderItem = ({ item }: { item: IMessageFromServer }) => (
<Item
{...{
item,
baseUrl
}}
onPress={onDiscussionPress}
2021-07-21 17:29:22 +00:00
/>
);
if (!discussions?.length) {
2021-09-16 18:16:02 +00:00
return <BackgroundContainer loading={loading} text={I18n.t('No_discussions')} />;
}
2022-01-31 21:21:26 +00:00
return (
2021-09-17 19:26:48 +00:00
<SafeAreaView testID='discussions-view'>
<StatusBar />
<FlatList
2021-09-03 19:50:18 +00:00
data={isSearching ? search : discussions}
renderItem={renderItem}
keyExtractor={(item: any) => item.msg}
2022-01-24 19:18:59 +00:00
style={{ backgroundColor: themes[theme].backgroundColor }}
contentContainerStyle={styles.contentContainer}
onEndReachedThreshold={0.5}
removeClippedSubviews={isIOS}
onEndReached={() => (isSearching ? searchTotal : total) > API_FETCH_COUNT ?? load()}
ItemSeparatorComponent={List.Separator}
ListFooterComponent={loading ? <ActivityIndicator /> : null}
2021-08-23 01:51:22 +00:00
scrollIndicatorInsets={{ right: 1 }}
/>
</SafeAreaView>
);
};
2021-09-16 18:16:02 +00:00
export default DiscussionsView;