Skip to content

Commit

Permalink
Merge pull request #42532 from rezkiy37/refactor/report-screen-and-lhn
Browse files Browse the repository at this point in the history
perf: Optimize AppNavigator
  • Loading branch information
mountiny authored Jun 3, 2024
2 parents 83beda4 + 7d334e0 commit a015db7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
38 changes: 38 additions & 0 deletions src/libs/Navigation/AppNavigator/index.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, {memo, useContext, useEffect} from 'react';
import {NativeModules} from 'react-native';
import {InitialURLContext} from '@components/InitialURLContextProvider';
import Navigation from '@libs/Navigation/Navigation';

type AppNavigatorProps = {
/** If we have an authToken this is true */
authenticated: boolean;
};

function AppNavigator({authenticated}: AppNavigatorProps) {
const initUrl = useContext(InitialURLContext);

useEffect(() => {
if (!NativeModules.HybridAppModule || !initUrl) {
return;
}

Navigation.isNavigationReady().then(() => {
Navigation.navigate(initUrl);
});
}, [initUrl]);

if (authenticated) {
const AuthScreens = require('./AuthScreens').default;

// These are the protected screens and only accessible when an authToken is present
return <AuthScreens />;
}

const PublicScreens = require('./PublicScreens').default;

return <PublicScreens />;
}

AppNavigator.displayName = 'AppNavigator';

export default memo(AppNavigator);
24 changes: 17 additions & 7 deletions src/libs/Navigation/AppNavigator/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, {useContext, useEffect} from 'react';
import React, {lazy, memo, Suspense, useContext, useEffect} from 'react';
import {NativeModules} from 'react-native';
import {InitialURLContext} from '@components/InitialURLContextProvider';
import Navigation from '@libs/Navigation/Navigation';

const AuthScreens = lazy(() => import('./AuthScreens'));
const PublicScreens = lazy(() => import('./PublicScreens'));

type AppNavigatorProps = {
/** If we have an authToken this is true */
authenticated: boolean;
Expand All @@ -22,14 +25,21 @@ function AppNavigator({authenticated}: AppNavigatorProps) {
}, [initUrl]);

if (authenticated) {
const AuthScreens = require('./AuthScreens').default;

// These are the protected screens and only accessible when an authToken is present
return <AuthScreens />;
return (
<Suspense fallback={null}>
<AuthScreens />
</Suspense>
);
}
const PublicScreens = require('./PublicScreens').default;
return <PublicScreens />;

return (
<Suspense fallback={null}>
<PublicScreens />
</Suspense>
);
}

AppNavigator.displayName = 'AppNavigator';
export default AppNavigator;

export default memo(AppNavigator);

0 comments on commit a015db7

Please sign in to comment.