Open History to see the last commit
1import { useNavigation, useRoute } from "@react-navigation/native";
2import { Platform } from "react-native";
3
4import { resetAndroidWorkspace } from "../../lib/android-workspace-navigation";
5import { useHardwareBack } from "../../lib/backHandler";
6import { useAdaptiveWorkspaceLayout } from "../layout/adaptive-workspace-context";
7
8/**
9 * The page's Back action, or undefined for the first page beside the Settings list on wide
10 * Android: that page roots the detail column, so Back leaves Settings rather than emptying it.
11 */
12export function useSettingsPageBack() {
13 const navigation = useNavigation();
14 const route = useRoute();
15 const { layout } = useAdaptiveWorkspaceLayout();
16 const routes = navigation.getState()?.routes ?? [];
17 const below = routes[routes.findIndex((candidate) => candidate.key === route.key) - 1];
18 const columnRoot =
19 Platform.OS === "android" && layout.usesSplitView && below?.name === "Settings";
20 useHardwareBack(columnRoot, () => {
21 resetAndroidWorkspace(navigation);
22 return true;
23 });
24 return columnRoot ? undefined : () => navigation.goBack();
25}