Skip to content

Commit

Permalink
Refactor createCustomFullScreenNavigator
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Mar 1, 2024
1 parent 1dee0ea commit 62ed5db
Showing 1 changed file with 2 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,12 @@ import type {ParamListBase, StackActionHelpers, StackNavigationState} from '@rea
import {createNavigatorFactory, useNavigationBuilder} from '@react-navigation/native';
import type {StackNavigationEventMap, StackNavigationOptions} from '@react-navigation/stack';
import {StackView} from '@react-navigation/stack';
import React, {useEffect, useMemo} from 'react';
import React, {useEffect} from 'react';
import useWindowDimensions from '@hooks/useWindowDimensions';
import navigationRef from '@libs/Navigation/navigationRef';
import SCREENS from '@src/SCREENS';
import CustomFullScreenRouter from './CustomFullScreenRouter';
import type {FullScreenNavigatorProps, FullScreenNavigatorRouterOptions} from './types';

type Routes = StackNavigationState<ParamListBase>['routes'];
function reduceReportRoutes(routes: Routes): Routes {
const result: Routes = [];
let count = 0;
const reverseRoutes = [...routes].reverse();

reverseRoutes.forEach((route) => {
if (route.name === SCREENS.WORKSPACES_CENTRAL_PANE) {
// Remove all report routes except the last 3. This will improve performance.
if (count < 3) {
result.push(route);
count++;
}
} else {
result.push(route);
}
});

return result.reverse();
}

function CustomFullScreenNavigator(props: FullScreenNavigatorProps) {
const {navigation, state, descriptors, NavigationContent} = useNavigationBuilder<
StackNavigationState<ParamListBase>,
Expand All @@ -45,16 +23,6 @@ function CustomFullScreenNavigator(props: FullScreenNavigatorProps) {

const {isSmallScreenWidth} = useWindowDimensions();

const stateToRender = useMemo(() => {
const result = reduceReportRoutes(state.routes);

return {
...state,
index: result.length - 1,
routes: [...result],
};
}, [state]);

useEffect(() => {
if (!navigationRef.isReady()) {
return;
Expand All @@ -69,7 +37,7 @@ function CustomFullScreenNavigator(props: FullScreenNavigatorProps) {
<StackView
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
state={stateToRender}
state={state}
descriptors={descriptors}
navigation={navigation}
/>
Expand Down

0 comments on commit 62ed5db

Please sign in to comment.