From b57d45603adfc9c0fef8acebd0bd609ff0f1c906 Mon Sep 17 00:00:00 2001 From: Adam Grzybowski Date: Wed, 10 Apr 2024 18:36:12 +0200 Subject: [PATCH] add customBackHandler --- src/pages/Search/SearchPage.tsx | 5 ++- .../useCustomBackHandler/index.android.ts | 32 +++++++++++++++++++ .../Search/useCustomBackHandler/index.ts | 3 ++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/pages/Search/useCustomBackHandler/index.android.ts create mode 100644 src/pages/Search/useCustomBackHandler/index.ts diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 488ce1dbb28a..135ab66276a2 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -4,10 +4,13 @@ import ScreenWrapper from '@components/ScreenWrapper'; import type {CentralPaneNavigatorParamList} from '@libs/Navigation/types'; import type SCREENS from '@src/SCREENS'; import SearchResults from './SearchResults'; +import useCustomBackHandler from './useCustomBackHandler'; -type SearchPageProps = StackScreenProps; +type SearchPageProps = StackScreenProps; function SearchPage({route}: SearchPageProps) { + useCustomBackHandler(); + return ( diff --git a/src/pages/Search/useCustomBackHandler/index.android.ts b/src/pages/Search/useCustomBackHandler/index.android.ts new file mode 100644 index 000000000000..cc9d5d3ca198 --- /dev/null +++ b/src/pages/Search/useCustomBackHandler/index.android.ts @@ -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); + + 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; diff --git a/src/pages/Search/useCustomBackHandler/index.ts b/src/pages/Search/useCustomBackHandler/index.ts new file mode 100644 index 000000000000..be753de818a6 --- /dev/null +++ b/src/pages/Search/useCustomBackHandler/index.ts @@ -0,0 +1,3 @@ +function useCustomBackHandler() {} + +export default useCustomBackHandler;