Open History to see the last commit
1import { Toolbar } from "./Toolbar";
2import { AppActivityIndicator } from "./AppActivityIndicator";
3import type { HeaderBarButtonMailSearchToolbarItem, SearchBarCommands } from "react-native-screens";
4import { NavigationContext, NavigationRouteContext } from "@react-navigation/native";
5import { use, useId, useLayoutEffect, useRef, useSyncExternalStore } from "react";
6import { View } from "react-native";
7import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios";
8import { ScreenHeaderButton } from "./ScreenHeaderButton";
9import { NativeStackScreenOptions, useScreenOptionsWriter } from "../../native/StackHeader";
10import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns";
11import { useDeviceType } from "../../native/native-layout-metrics";
12import { isDuoDevice } from "../../lib/device-type";
13import { useAdaptiveWorkspaceLayout } from "../../features/layout/AdaptiveWorkspaceLayout";
14import { useSidebarToggle } from "../../features/layout/useSidebarToggle";
15import { InspectorCloseContext } from "../../features/layout/inspector-bar-store";
16import {
17 createNativeMailSearchToolbarItem,
18 NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED,
19} from "../../features/layout/native-mail-search-toolbar";
20import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
21import type { AppBarMenuItem, AppBarProps } from "./AppBar.types";
22import type { AppSymbolName } from "./AppSymbol";
23import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
24import { useControlTheme } from "./useControlTheme";
25
26function iosIcon(icon: AppSymbolName) {
27 return typeof icon === "string" ? icon : icon.ios;
28}
29
30type MailMenu = NonNullable<HeaderBarButtonMailSearchToolbarItem["filterMenu"]>;
31function mailMenuItems(items: ReadonlyArray<AppBarMenuItem>): MailMenu["items"] {
32 return items.map((item) =>
33 "items" in item
34 ? { type: "submenu", title: item.title ?? "", items: mailMenuItems(item.items) }
35 : {
36 type: "action",
37 title: item.title,
38 state: item.selected ? "on" : "off",
39 disabled: item.disabled,
40 onPress: item.onPress,
41 },
42 );
43}
44
45export function AppBar(props: AppBarProps) {
46 const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();
47 const deviceType = useDeviceType();
48 const headerId = useId();
49 const { layout } = useAdaptiveWorkspaceLayout();
50 const sidebarToggle = useSidebarToggle();
51 const closeInspector = use(InspectorCloseContext);
52 const setOptions = useScreenOptionsWriter();
53 const ownsInspectorBar = closeInspector !== null && !props.embedded && !props.sheet;
54 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- an inspector pane that stops drawing its bar must not leave it in the out-of-tree inspector bar
55 useLayoutEffect(() => {
56 if (!ownsInspectorBar || !setOptions) return;
57 setOptions({ headerShown: true });
58 return () => setOptions({ headerShown: false });
59 }, [ownsInspectorBar, setOptions]);
60 const splitBackPress = props.backInSplitView?.onPress ?? props.onBack;
61 const splitBack =
62 layout.usesSplitView && props.backInSplitView && splitBackPress
63 ? { ...props.backInSplitView, onPress: splitBackPress }
64 : null;
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;
77 const rootBack =
78 !layout.usesSplitView && props.onBackToList && navigation?.canGoBack() === false
79 ? props.onBackToList
80 : props.guardBack && props.onBack
81 ? props.onBack
82 : null;
83 const compose =
84 layout.usesSplitView && !(usesNativeWorkspaceColumns && isDuoDevice(deviceType))
85 ? props.onCompose
86 : undefined;
87 const { themeVariables } = useAppearancePreferences();
88 const { colors } = useControlTheme();
89 const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();
90 const { search, menus } = props;
91 const menu = menus?.[0];
92 const searchBarRef = useRef<SearchBarCommands>(null);
93 const nativeSearchText = useRef(search?.value ?? "");
94 const searchValue = search?.value;
95 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the native search bar keeps its own text; a value the caller changes (a reset) has to be pushed into it
96 useLayoutEffect(() => {
97 if (searchValue === undefined || searchValue === nativeSearchText.current) return;
98 nativeSearchText.current = searchValue;
99 searchBarRef.current?.setText(searchValue);
100 }, [searchValue]);
101 const reportSearchText = (text: string) => {
102 nativeSearchText.current = text;
103 search?.onChangeText(text);
104 };
105 const compactSearch =
106 search !== undefined &&
107 (search.compactToolbar ?? !layout.usesSplitView) &&
108 usesNativeMailSearchToolbar;
109 const visibleMenus = compactSearch ? (menus?.slice(1) ?? []) : (menus ?? []);
110 // Where the mail toolbar isn't used, the Duo keeps search at the bottom of the pane like its
111 // workspace lists; elsewhere inline search is a button in the bar.
112 const duoBottomSearch =
113 search !== undefined &&
114 !compactSearch &&
115 NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED &&
116 isDuoDevice(deviceType);
117 if (props.embedded || props.sheet) return null;
118 return (
119 <>
120 <NativeStackScreenOptions
121 optionsVersion={[props.optionsVersion, compactSearch ? menu : undefined]}
122 options={{
123 headerShown: true,
124 headerBackVisible:
125 props.onClose === undefined &&
126 splitBack === null &&
127 !listBeside &&
128 !(props.guardBack && props.onBack),
129 ...(props.guardBack ? { gestureEnabled: false } : undefined),
130 title: props.title,
131 unstable_headerSubtitle: props.subtitle || undefined,
132 ...(props.matchSearchSurface
133 ? { contentStyle: { backgroundColor: themeVariables["--color-sheet-solid"] } }
134 : undefined),
135 ...(search
136 ? {
137 unstable_headerToolbarItems: compactSearch
138 ? () => [
139 createNativeMailSearchToolbarItem({
140 showsSearchDismissButton: true,
141 placeholder: search.compactPlaceholder ?? search.placeholder,
142 onSearchTextChange: search.onChangeText,
143 searchTextChangeId: `${headerId}-search-text`,
144 ...(menu
145 ? {
146 filterButtonId: `${headerId}-filter`,
147 filterSystemImageName: iosIcon(menu.icon),
148 filterMenu: { title: menu.title, items: mailMenuItems(menu.items) },
149 }
150 : undefined),
151 }),
152 ]
153 : () => [],
154 headerSearchBarOptions: compactSearch
155 ? undefined
156 : {
157 allowToolbarIntegration: true,
158 ...(duoBottomSearch
159 ? { placement: "integrated" as const }
160 : NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED && search.mode === "inline"
161 ? { placement: "integratedButton" as const }
162 : undefined),
163 autoCapitalize: "none",
164 hideNavigationBar: false,
165 ...(search.mode === "inline" || duoBottomSearch
166 ? { obscureBackground: false }
167 : undefined),
168 placeholder: search.placeholder,
169 ref: searchBarRef,
170 onChangeText: (event) => reportSearchText(event.nativeEvent.text),
171 onCancelButtonPress: () => reportSearchText(""),
172 },
173 }
174 : undefined),
175 }}
176 />
177 {props.onClose ? (
178 <Toolbar placement="left">
179 <Toolbar.Button
180 icon="xmark"
181 accessibilityLabel={props.closeAccessibilityLabel ?? "Close"}
182 onPress={props.onClose}
183 />
184 </Toolbar>
185 ) : rootBack || splitBack || sidebarToggle || compose ? (
186 <Toolbar placement="left">
187 {rootBack ? (
188 <ScreenHeaderButton
189 accessibilityLabel="Back"
190 icon="chevron.left"
191 onPress={rootBack}
192 separateBackground
193 />
194 ) : null}
195 {splitBack ? <ScreenHeaderButton {...splitBack} /> : null}
196 {sidebarToggle ? (
197 <ScreenHeaderButton
198 axisBehavior={usesNativeWorkspaceColumns ? "horizontalOnly" : undefined}
199 accessibilityLabel={sidebarToggle.accessibilityLabel}
200 icon={sidebarToggle.icon}
201 onPress={sidebarToggle.onPress}
202 separateBackground={splitBack ? splitBack.separateBackground : true}
203 />
204 ) : null}
205 {compose ? (
206 <ScreenHeaderButton
207 accessibilityLabel="New task"
208 label="New task"
209 icon="square.and.pencil"
210 onPress={compose}
211 separateBackground
212 />
213 ) : null}
214 </Toolbar>
215 ) : null}
216 {props.actions?.length ||
217 visibleMenus.length ||
218 props.trailing ||
219 props.confirm ||
220 closeInspector ? (
221 <Toolbar placement="right">
222 {props.actions?.map(({ loading, ...action }) =>
223 loading ? (
224 <Toolbar.Custom key={action.accessibilityLabel}>
225 <View
226 accessibilityLabel={action.accessibilityLabel}
227 className="size-9 items-center justify-center"
228 >
229 <AppActivityIndicator color={action.tintColor} />
230 </View>
231 </Toolbar.Custom>
232 ) : (
233 <ScreenHeaderButton
234 key={action.accessibilityLabel}
235 {...action}
236 icon={iosIcon(action.icon)}
237 separateBackground={action.separateBackground ?? true}
238 />
239 ),
240 )}
241 {visibleMenus.map(createNativeHeaderMenu)}
242 {props.trailing}
243 {props.confirm?.loading ? (
244 <Toolbar.Custom>
245 <View
246 className="size-11 items-center justify-center"
247 accessible
248 accessibilityLabel={props.confirm.label}
249 accessibilityState={{ busy: true }}
250 >
251 <AppActivityIndicator colorClassName="accent-prominent-action" />
252 </View>
253 </Toolbar.Custom>
254 ) : props.confirm ? (
255 <Toolbar.Button
256 icon="checkmark"
257 variant="prominent"
258 visibilityPriority="high"
259 tintColor={colors["--color-prominent-action"]}
260 accessibilityLabel={props.confirm.label}
261 disabled={props.confirm.disabled}
262 onPress={props.confirm.onPress}
263 />
264 ) : null}
265 {closeInspector ? (
266 <ScreenHeaderButton
267 accessibilityLabel="Close inspector"
268 icon="xmark"
269 onPress={closeInspector}
270 separateBackground
271 />
272 ) : null}
273 </Toolbar>
274 ) : null}
275 {duoBottomSearch ? (
276 <Toolbar placement="bottom">
277 <Toolbar.SearchBarSlot />
278 </Toolbar>
279 ) : null}
280 </>
281 );
282}
283
284export type {
285 AppBarAction,
286 AppBarConfirm,
287 AppBarMenu,
288 AppBarMenuItem,
289 AppBarProps,
290 AppBarSearch,
291} from "./AppBar.types";