mattermost-mobile/app/screens/home/index.tsx
Kyriakos Z b2d838d3da
MM-37110: handle toggling of CRT feature (#6382)
* MM-37110: handle toggling of CRT feature

When a user toggles CRT on/off the app should truncate affected tables,
and re-fetch data.

Truncated tables:
- POST
- POSTS_IN_CHANNEL
- POSTS_IN_THREAD
- THREAD
- THREADS_IN_TEAM
- THREAD_PARTICIPANT
- MY_CHANNEL

After truncation `entry` is called again. We must make sure though that
we save the CRT change before calling `entry` again, or we end up with
infinite recursion.

PS the UI seems to handle the change rather good

* Fixes appEntry and popToRoot

* Small refactor

* Fixes since param on appEntry

* Further refactoring

* Delete unneeded return type

* Removes shouldPopToRoot from appEntry

* Addresses review comments
2022-07-07 12:19:02 +02:00

151 lines
5.4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {createBottomTabNavigator, BottomTabBarProps} from '@react-navigation/bottom-tabs';
import {NavigationContainer} from '@react-navigation/native';
import React, {useEffect} from 'react';
import {useIntl} from 'react-intl';
import {DeviceEventEmitter, Platform} from 'react-native';
import HWKeyboardEvent from 'react-native-hw-keyboard-event';
import {enableFreeze, enableScreens} from 'react-native-screens';
import {Events, Screens} from '@constants';
import {useTheme} from '@context/theme';
import {findChannels, popToRoot} from '@screens/navigation';
import NavigationStore from '@store/navigation_store';
import {alertChannelArchived, alertChannelRemove, alertTeamRemove} from '@utils/navigation';
import {notificationError} from '@utils/notification';
import Account from './account';
import ChannelList from './channel_list';
import RecentMentions from './recent_mentions';
import SavedMessages from './saved_messages';
import TabBar from './tab_bar';
// import Search from './search';
import type {LaunchProps} from '@typings/launch';
if (Platform.OS === 'ios') {
// We do this on iOS to avoid conflicts betwen ReactNavigation & Wix ReactNativeNavigation
enableScreens(false);
}
enableFreeze(true);
type HomeProps = LaunchProps & {
componentId: string;
time?: number;
};
const Tab = createBottomTabNavigator();
export default function HomeScreen(props: HomeProps) {
const theme = useTheme();
const intl = useIntl();
useEffect(() => {
const listener = DeviceEventEmitter.addListener(Events.NOTIFICATION_ERROR, (value: 'Team' | 'Channel') => {
notificationError(intl, value);
});
return () => {
listener.remove();
};
}, [intl.locale]);
useEffect(() => {
const leaveTeamListener = DeviceEventEmitter.addListener(Events.LEAVE_TEAM, (displayName: string) => {
alertTeamRemove(displayName, intl);
});
const leaveChannelListener = DeviceEventEmitter.addListener(Events.LEAVE_CHANNEL, (displayName: string) => {
alertChannelRemove(displayName, intl);
});
const archivedChannelListener = DeviceEventEmitter.addListener(Events.CHANNEL_ARCHIVED, (displayName: string) => {
alertChannelArchived(displayName, intl);
});
const crtToggledListener = DeviceEventEmitter.addListener(Events.CRT_TOGGLED, (isSameServer: boolean) => {
if (isSameServer) {
popToRoot();
}
});
return () => {
leaveTeamListener.remove();
leaveChannelListener.remove();
archivedChannelListener.remove();
crtToggledListener.remove();
};
}, [intl.locale]);
useEffect(() => {
const listener = HWKeyboardEvent.onHWKeyPressed((keyEvent: {pressedKey: string}) => {
const screen = NavigationStore.getAllNavigationComponents();
if (!screen.includes(Screens.FIND_CHANNELS) && keyEvent.pressedKey === 'find-channels') {
findChannels(
intl.formatMessage({id: 'find_channels.title', defaultMessage: 'Find Channels'}),
theme,
);
}
});
return () => {
listener.remove();
};
}, [intl.locale]);
return (
<NavigationContainer
theme={{
dark: false,
colors: {
primary: theme.centerChannelColor,
background: 'transparent',
card: theme.centerChannelBg,
text: theme.centerChannelColor,
border: 'white',
notification: theme.mentionHighlightBg,
},
}}
>
<Tab.Navigator
screenOptions={{headerShown: false, lazy: true, unmountOnBlur: false}}
backBehavior='none'
tabBar={(tabProps: BottomTabBarProps) => (
<TabBar
{...tabProps}
theme={theme}
/>)}
>
<Tab.Screen
name={Screens.HOME}
options={{title: 'Channel', unmountOnBlur: false, tabBarTestID: 'tab_bar.home.tab'}}
>
{() => <ChannelList {...props}/>}
</Tab.Screen>
{/* <Tab.Screen
name={Screens.SEARCH}
component={Search}
options={{unmountOnBlur: false, lazy: true, tabBarTestID: 'tab_bar.search.tab'}}
/> */}
<Tab.Screen
name={Screens.MENTIONS}
component={RecentMentions}
options={{tabBarTestID: 'tab_bar.mentions.tab', lazy: true, unmountOnBlur: false}}
/>
<Tab.Screen
name={Screens.SAVED_MESSAGES}
component={SavedMessages}
options={{unmountOnBlur: false, lazy: true, tabBarTestID: 'tab_bar.saved_messages.tab'}}
/>
<Tab.Screen
name={Screens.ACCOUNT}
component={Account}
options={{tabBarTestID: 'tab_bar.account.tab', lazy: true, unmountOnBlur: false}}
/>
</Tab.Navigator>
</NavigationContainer>
);
}