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.
- Loading branch information
1 parent
2d78c11
commit b57d456
Showing
3 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {StackActions, useFocusEffect} from '@react-navigation/native'; | ||
import {useCallback} from 'react'; | ||
import {BackHandler} from 'react-native'; | ||
import getTopmostCentralPaneRoute from '@libs/Navigation/getTopmostCentralPaneRoute'; | ||
import navigationRef from '@libs/Navigation/navigationRef'; | ||
import type {RootStackParamList, State} from '@libs/Navigation/types'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import SCREENS from '@src/SCREENS'; | ||
|
||
function useCustomBackHandler() { | ||
useFocusEffect( | ||
useCallback(() => { | ||
const onBackPress = () => { | ||
const rootState = navigationRef.getRootState(); | ||
|
||
const bottomTabRoute = rootState.routes.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); | ||
const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState.routes.at(-2)]} as State<RootStackParamList>); | ||
|
||
if (bottomTabRoute && bottomTabRoute.state && (!centralPaneRouteAfterPop || centralPaneRouteAfterPop.name !== SCREENS.SEARCH.CENTRAL_PANE)) { | ||
navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute.state.key}); | ||
} | ||
return false; | ||
}; | ||
|
||
const subscription = BackHandler.addEventListener('hardwareBackPress', onBackPress); | ||
|
||
return () => subscription.remove(); | ||
}, []), | ||
); | ||
} | ||
|
||
export default useCustomBackHandler; |
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,3 @@ | ||
function useCustomBackHandler() {} | ||
|
||
export default useCustomBackHandler; |