|
1 | 1 | import { Toolbar } from "./Toolbar"; |
2 | 2 | import { AppActivityIndicator } from "./AppActivityIndicator"; |
3 | 3 | import type { HeaderBarButtonMailSearchToolbarItem, SearchBarCommands } from "react-native-screens"; |
4 | | -import { NavigationContext } from "@react-navigation/native"; |
5 | | -import { use, useId, useLayoutEffect, useRef } from "react"; |
| 4 | +import { NavigationContext, NavigationRouteContext } from "@react-navigation/native"; |
| 5 | +import { use, useId, useLayoutEffect, useRef, useSyncExternalStore } from "react"; |
6 | 6 | import { View } from "react-native"; |
7 | 7 | import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios"; |
8 | 8 | import { ScreenHeaderButton } from "./ScreenHeaderButton"; |
@@ -63,6 +63,17 @@ export function AppBar(props: AppBarProps) { |
63 | 63 | ? { ...props.backInSplitView, onPress: splitBackPress } |
64 | 64 | : null; |
65 | 65 | const navigation = use(NavigationContext); |
| 66 | + const route = use(NavigationRouteContext); |
| 67 | + // A thread opened from another thread sits on it, not beside the list, so it keeps Back. |
| 68 | + const threadBelow = useSyncExternalStore( |
| 69 | + (onChange) => navigation?.addListener("state", onChange) ?? (() => {}), |
| 70 | + () => { |
| 71 | + const routes = navigation?.getState()?.routes; |
| 72 | + const index = routes?.findIndex((candidate) => candidate.key === route?.key) ?? -1; |
| 73 | + return route?.name === "Thread" && index > 0 && routes?.[index - 1]?.name === "Thread"; |
| 74 | + }, |
| 75 | + ); |
| 76 | + const listBeside = layout.usesSplitView && props.onBackToList !== undefined && !threadBelow; |
66 | 77 | const rootBack = |
67 | 78 | !layout.usesSplitView && props.onBackToList && navigation?.canGoBack() === false |
68 | 79 | ? props.onBackToList |
@@ -113,7 +124,7 @@ export function AppBar(props: AppBarProps) { |
113 | 124 | headerBackVisible: |
114 | 125 | props.onClose === undefined && |
115 | 126 | splitBack === null && |
116 | | - !(layout.usesSplitView && props.onBackToList) && |
| 127 | + !listBeside && |
117 | 128 | !(props.guardBack && props.onBack), |
118 | 129 | ...(props.guardBack ? { gestureEnabled: false } : undefined), |
119 | 130 | title: props.title, |
|
0 commit comments