forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add state diff for navigation in rhp
- Loading branch information
1 parent
3abdd8b
commit c4b9e41
Showing
4 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/libs/Navigation/AppNavigator/getActionsFromPartialDiff.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {getActionFromState, StackActions} from '@react-navigation/native'; | ||
import type {NavigationAction} from '@react-navigation/native'; | ||
import linkingConfig from '@libs/Navigation/linkingConfig'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import type {GetPartialStateDiffReturnType} from './getPartialStateDiff'; | ||
|
||
/** | ||
* @param diff - Diff generated by getPartialDiff. | ||
* @returns Array of actions to dispatch to apply diff. | ||
*/ | ||
function getActionsFromPartialDiff(diff: GetPartialStateDiffReturnType): NavigationAction[] { | ||
const actions: NavigationAction[] = []; | ||
|
||
const bottomTabDiff = diff[NAVIGATORS.BOTTOM_TAB_NAVIGATOR]; | ||
const centralPaneDiff = diff[NAVIGATORS.CENTRAL_PANE_NAVIGATOR]; | ||
const fullScreenDiff = diff[NAVIGATORS.FULL_SCREEN_NAVIGATOR]; | ||
|
||
// There is only one bottom tab navigator so we can just push this route. | ||
if (bottomTabDiff) { | ||
actions.push(StackActions.push(bottomTabDiff.name, bottomTabDiff.params)); | ||
} | ||
|
||
if (centralPaneDiff) { | ||
// In this case we have to wrap the inner central pane route with central pane navigator. | ||
actions.push( | ||
StackActions.push(NAVIGATORS.CENTRAL_PANE_NAVIGATOR, { | ||
screen: centralPaneDiff.name, | ||
params: centralPaneDiff.params, | ||
}), | ||
); | ||
} | ||
|
||
if (fullScreenDiff) { | ||
const action = getActionFromState({routes: [fullScreenDiff]}, linkingConfig.config); | ||
if (action) { | ||
actions.push(action); | ||
} | ||
} | ||
|
||
return actions; | ||
} | ||
|
||
export default getActionsFromPartialDiff; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import getTopmostBottomTabRoute from '@libs/Navigation/getTopmostBottomTabRoute'; | ||
import getTopmostCentralPaneRoute from '@libs/Navigation/getTopmostCentralPaneRoute'; | ||
import type {Metainfo} from '@libs/Navigation/linkingConfig/getAdaptedStateFromPath'; | ||
import type {NavigationPartialRoute, RootStackParamList, State} from '@libs/Navigation/types'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const shallowCompare = (obj1?: object, obj2?: object) => { | ||
if (!obj1 && !obj2) { | ||
return true; | ||
} | ||
if (obj1 && obj2) { | ||
// @ts-expect-error we know that obj1 and obj2 are params of a route. | ||
return Object.keys(obj1).length === Object.keys(obj2).length && Object.keys(obj1).every((key) => obj1[key] === obj2[key]); | ||
} | ||
return false; | ||
}; | ||
|
||
type GetPartialStateDiffReturnType = { | ||
[NAVIGATORS.BOTTOM_TAB_NAVIGATOR]?: NavigationPartialRoute; | ||
[NAVIGATORS.CENTRAL_PANE_NAVIGATOR]?: NavigationPartialRoute; | ||
[NAVIGATORS.FULL_SCREEN_NAVIGATOR]?: NavigationPartialRoute; | ||
}; | ||
|
||
/** | ||
* This function returns partial additive diff beteween the two states. | ||
* The partial diff have information which bottom tab, central pane and full screen screens we need to push to go from state to templateState | ||
* @param state - Current state. | ||
* @param templateState - Desired state generated with getAdaptedStateFromPath. | ||
* @param metainfo - Additional info from getAdaptedStateFromPath funciton. | ||
* @returns The screen options object | ||
*/ | ||
function getPartialStateDiff(state: State<RootStackParamList>, templateState: State<RootStackParamList>, metainfo: Metainfo): GetPartialStateDiffReturnType { | ||
const diff: GetPartialStateDiffReturnType = {}; | ||
|
||
// If it is mandatory we need to compare both central pane and bottom tab of states. | ||
if (metainfo.isCentralPaneAndBottomTabMandatory) { | ||
const stateTopmostBottomTab = getTopmostBottomTabRoute(state); | ||
const templateStateTopmostBottomTab = getTopmostBottomTabRoute(templateState); | ||
|
||
// Bottom tab navigator | ||
if (stateTopmostBottomTab && templateStateTopmostBottomTab && stateTopmostBottomTab.name !== templateStateTopmostBottomTab.name) { | ||
diff[NAVIGATORS.BOTTOM_TAB_NAVIGATOR] = templateStateTopmostBottomTab; | ||
} | ||
|
||
const stateTopmostCentralPane = getTopmostCentralPaneRoute(state); | ||
const templateStateTopmostCentralPane = getTopmostCentralPaneRoute(templateState); | ||
|
||
if ( | ||
// If the central pane is only in the template state, it's diff. | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
(!stateTopmostCentralPane && templateStateTopmostCentralPane) || | ||
(stateTopmostCentralPane && | ||
templateStateTopmostCentralPane && | ||
stateTopmostCentralPane.name !== templateStateTopmostCentralPane.name && | ||
!shallowCompare(stateTopmostCentralPane.params, templateStateTopmostCentralPane.params)) | ||
) { | ||
// We need to wrap central pane routes in the central pane navigator. | ||
diff[NAVIGATORS.CENTRAL_PANE_NAVIGATOR] = templateStateTopmostCentralPane; | ||
} | ||
} | ||
|
||
// This one is heurestic and may need to improved if we will be able to navigate from modal screen with full screen in background to another modal screen with full screen in background. | ||
// For now this simple check is enought. | ||
if (metainfo.isFullScreenNavigatorMandatory) { | ||
const stateTopmostFullScreen = state.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1); | ||
const templateStateTopmostFullScreen = templateState.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1) as NavigationPartialRoute; | ||
if (!stateTopmostFullScreen && templateStateTopmostFullScreen) { | ||
diff[NAVIGATORS.FULL_SCREEN_NAVIGATOR] = templateStateTopmostFullScreen; | ||
} | ||
} | ||
|
||
return diff; | ||
} | ||
|
||
export default getPartialStateDiff; | ||
export type {GetPartialStateDiffReturnType}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters