2020-06-15 14:00:46 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { NavigationContainer } from '@react-navigation/native';
|
|
|
|
import { createStackNavigator } from '@react-navigation/stack';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2022-01-17 16:10:39 +00:00
|
|
|
import { SetUsernameStackParamList, StackParamList } from './definitions/navigationTypes';
|
2020-06-15 14:00:46 +00:00
|
|
|
import Navigation from './lib/Navigation';
|
|
|
|
import { defaultHeader, getActiveRouteName, navigationTheme } from './utils/navigation';
|
2021-09-23 14:17:53 +00:00
|
|
|
import { ROOT_INSIDE, ROOT_LOADING, ROOT_OUTSIDE, ROOT_SET_USERNAME } from './actions/app';
|
2020-06-15 14:00:46 +00:00
|
|
|
// Stacks
|
|
|
|
import AuthLoadingView from './views/AuthLoadingView';
|
|
|
|
// SetUsername Stack
|
|
|
|
import SetUsernameView from './views/SetUsernameView';
|
|
|
|
import OutsideStack from './stacks/OutsideStack';
|
|
|
|
import InsideStack from './stacks/InsideStack';
|
|
|
|
import MasterDetailStack from './stacks/MasterDetailStack';
|
|
|
|
import { ThemeContext } from './theme';
|
|
|
|
import { setCurrentScreen } from './utils/log';
|
|
|
|
|
|
|
|
// SetUsernameStack
|
2021-12-03 19:27:57 +00:00
|
|
|
const SetUsername = createStackNavigator<SetUsernameStackParamList>();
|
2020-06-15 14:00:46 +00:00
|
|
|
const SetUsernameStack = () => (
|
|
|
|
<SetUsername.Navigator screenOptions={defaultHeader}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<SetUsername.Screen name='SetUsernameView' component={SetUsernameView} />
|
2020-06-15 14:00:46 +00:00
|
|
|
</SetUsername.Navigator>
|
|
|
|
);
|
|
|
|
|
|
|
|
// App
|
2021-12-03 19:27:57 +00:00
|
|
|
const Stack = createStackNavigator<StackParamList>();
|
2021-09-13 20:41:05 +00:00
|
|
|
const App = React.memo(({ root, isMasterDetail }: { root: string; isMasterDetail: boolean }) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
if (!root) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { theme } = React.useContext(ThemeContext);
|
|
|
|
const navTheme = navigationTheme(theme);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2020-07-02 17:10:11 +00:00
|
|
|
const state = Navigation.navigationRef.current?.getRootState();
|
2020-06-15 14:00:46 +00:00
|
|
|
const currentRouteName = getActiveRouteName(state);
|
|
|
|
Navigation.routeNameRef.current = currentRouteName;
|
|
|
|
setCurrentScreen(currentRouteName);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2020-06-16 20:32:30 +00:00
|
|
|
<NavigationContainer
|
|
|
|
theme={navTheme}
|
|
|
|
ref={Navigation.navigationRef}
|
2021-09-13 20:41:05 +00:00
|
|
|
onStateChange={state => {
|
2020-06-16 20:32:30 +00:00
|
|
|
const previousRouteName = Navigation.routeNameRef.current;
|
|
|
|
const currentRouteName = getActiveRouteName(state);
|
|
|
|
if (previousRouteName !== currentRouteName) {
|
|
|
|
setCurrentScreen(currentRouteName);
|
|
|
|
}
|
|
|
|
Navigation.routeNameRef.current = currentRouteName;
|
2021-09-13 20:41:05 +00:00
|
|
|
}}>
|
2020-06-16 20:32:30 +00:00
|
|
|
<Stack.Navigator screenOptions={{ headerShown: false, animationEnabled: false }}>
|
|
|
|
<>
|
2021-09-13 20:41:05 +00:00
|
|
|
{root === ROOT_LOADING ? <Stack.Screen name='AuthLoading' component={AuthLoadingView} /> : null}
|
2021-09-23 14:17:53 +00:00
|
|
|
{root === ROOT_OUTSIDE ? <Stack.Screen name='OutsideStack' component={OutsideStack} /> : null}
|
2020-06-16 20:32:30 +00:00
|
|
|
{root === ROOT_INSIDE && isMasterDetail ? (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Stack.Screen name='MasterDetailStack' component={MasterDetailStack} />
|
2020-06-16 20:32:30 +00:00
|
|
|
) : null}
|
2021-09-13 20:41:05 +00:00
|
|
|
{root === ROOT_INSIDE && !isMasterDetail ? <Stack.Screen name='InsideStack' component={InsideStack} /> : null}
|
|
|
|
{root === ROOT_SET_USERNAME ? <Stack.Screen name='SetUsernameStack' component={SetUsernameStack} /> : null}
|
2020-06-16 20:32:30 +00:00
|
|
|
</>
|
|
|
|
</Stack.Navigator>
|
|
|
|
</NavigationContainer>
|
2020-06-15 14:00:46 +00:00
|
|
|
);
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
root: state.app.root,
|
|
|
|
isMasterDetail: state.app.isMasterDetail
|
|
|
|
});
|
|
|
|
|
|
|
|
const AppContainer = connect(mapStateToProps)(App);
|
|
|
|
export default AppContainer;
|