Open History to see the last commit
1import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
2import { use, useCallback, useEffect, useRef } from "react";
3import { useNavigation, type ParamListBase } from "@react-navigation/native";
4import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
5import { Platform } from "react-native";
6import type { SearchBarCommands } from "react-native-screens";
7import { NativePrimaryColumnContext } from "../../native/v5-workspace-context";
8import { useUniwindTheme } from "../../lib/useUniwindTheme";
9import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
10import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
11import { createNativeMailSearchToolbarItem } from "../layout/native-mail-search-toolbar";
12import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
13import { buildHomeListFilterMenu } from "./home-list-filter-menu";
14import { createSidebarHeaderItems } from "../threads/sidebar-native-header-items";
15import type { HomeHeaderProps } from "./HomeHeader.types";
16import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
17
18export type { HomeHeaderEnvironment } from "./HomeHeader.types";
19
20export function HomeHeader(props: HomeHeaderProps) {
21 const navigation = useNavigation<NativeStackNavigationProp<ParamListBase>>();
22 const { panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
23 const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();
24 const primaryColumn = use(NativePrimaryColumnContext);
25 const sidebarHeader =
26 Platform.OS === "ios" &&
27 primaryColumn !== null &&
28 (Platform.isPad || !usesNativeMailSearchToolbar);
29 const searchBarRef = useRef<SearchBarCommands>(null);
30 const focusAfterReveal = useRef(false);
31 useEffect(
32 () =>
33 navigation.addListener("transitionEnd", (event) => {
34 if (focusAfterReveal.current && !event.data.closing) {
35 focusAfterReveal.current = false;
36 searchBarRef.current?.focus();
37 }
38 }),
39 [navigation],
40 );
41 const iconColor = useUniwindTheme()["--color-icon"];
42 // The list uses a fixed creation order and ignores sort/group options, so
43 // the filter menu only carries the filters and the "customized" icon state
44 // keys off those alone.
45 const hasCustomListOptions =
46 props.selectedEnvironmentId !== null || props.selectedProjectKey !== null;
47 const focusSearch = useCallback(() => {
48 if (primaryColumn && !panes.primarySidebarVisible) {
49 focusAfterReveal.current = true;
50 togglePrimarySidebar();
51 return true;
52 }
53 searchBarRef.current?.focus();
54 return searchBarRef.current !== null;
55 }, [primaryColumn, panes.primarySidebarVisible, togglePrimarySidebar]);
56 useHardwareKeyboardCommand("focusSearch", focusSearch);
57 const filterMenu = buildHomeListFilterMenu(props);
58
59 return (
60 <>
61 <NativeStackScreenOptions
62 optionsVersion={filterMenu.items}
63 options={{
64 // Static header config (glass, title, fonts) lives in Stack.tsx
65 // (GLASS_HEADER_OPTIONS). Only dynamic values are set here.
66 headerTintColor: iconColor,
67 unstable_headerRightItems: () =>
68 sidebarHeader
69 ? createSidebarHeaderItems({
70 filterIcon: hasCustomListOptions
71 ? "line.3.horizontal.decrease.circle.fill"
72 : "line.3.horizontal.decrease.circle",
73 filterMenu,
74 onOpenSettings: props.onOpenSettings,
75 })
76 : [
77 withNativeGlassHeaderItem({
78 accessibilityLabel: "Open settings",
79 icon: { name: "ellipsis", type: "sfSymbol" } as const,
80 identifier: "home-settings",
81 label: "",
82 onPress: props.onOpenSettings,
83 type: "button",
84 }),
85 ],
86 // The keys below are set per-branch (not `undefined`) so a later
87 // reapply cannot clobber options owned by NativeHeaderToolbar.
88 ...(sidebarHeader
89 ? {
90 headerSearchBarOptions: {
91 ref: searchBarRef,
92 autoCapitalize: "none" as const,
93 hideNavigationBar: false,
94 hideWhenScrolling: false,
95 obscureBackground: false,
96 placement: "stacked" as const,
97 allowToolbarIntegration: false,
98 placeholder: "Search",
99 onCancelButtonPress: () => props.onSearchQueryChange(""),
100 onChangeText: (event) => props.onSearchQueryChange(event.nativeEvent.text),
101 },
102 unstable_headerToolbarItems: () => [],
103 }
104 : usesNativeMailSearchToolbar
105 ? {
106 headerSearchBarOptions: {
107 ref: searchBarRef,
108 autoCapitalize: "none" as const,
109 onCancelButtonPress: () => props.onSearchQueryChange(""),
110 },
111 unstable_headerToolbarItems: () => [
112 createNativeMailSearchToolbarItem({
113 composeButtonId: "home-new-task",
114 composeSystemImageName: "square.and.pencil",
115 filterMenu,
116 filterButtonId: "home-filter",
117 filterSystemImageName: hasCustomListOptions
118 ? "line.3.horizontal.decrease.circle.fill"
119 : "line.3.horizontal.decrease",
120 onComposePress: props.onStartNewTask,
121 onSearchTextChange: props.onSearchQueryChange,
122 placeholder: "Search",
123 searchTextChangeId: "home-search-text",
124 showsSearchDismissButton: true,
125 }),
126 ],
127 }
128 : {
129 // Pre-Liquid-Glass iOS: standard pull-down search in the nav
130 // bar; create + sort live in the plain bottom toolbar below.
131 headerSearchBarOptions: {
132 ref: searchBarRef,
133 autoCapitalize: "none" as const,
134 hideNavigationBar: false,
135 placeholder: "Search",
136 onCancelButtonPress: () => {
137 props.onSearchQueryChange("");
138 },
139 onChangeText: (event) => {
140 props.onSearchQueryChange(event.nativeEvent.text);
141 },
142 },
143 }),
144 }}
145 />
146
147 {sidebarHeader || usesNativeMailSearchToolbar ? null : (
148 <NativeHeaderToolbar placement="bottom">
149 <NativeHeaderToolbar.Menu
150 accessibilityLabel="Filter threads"
151 icon={
152 hasCustomListOptions
153 ? "line.3.horizontal.decrease.circle.fill"
154 : "line.3.horizontal.decrease.circle"
155 }
156 title="Thread list options"
157 separateBackground
158 >
159 <NativeHeaderToolbar.Menu title="Environment">
160 <NativeHeaderToolbar.Label>Environment</NativeHeaderToolbar.Label>
161 <NativeHeaderToolbar.MenuAction
162 isOn={props.selectedEnvironmentId === null}
163 onPress={() => props.onEnvironmentChange(null)}
164 subtitle="Show threads from every environment"
165 >
166 <NativeHeaderToolbar.Label>All environments</NativeHeaderToolbar.Label>
167 </NativeHeaderToolbar.MenuAction>
168 {props.environments.map((environment) => (
169 <NativeHeaderToolbar.MenuAction
170 key={environment.environmentId}
171 isOn={props.selectedEnvironmentId === environment.environmentId}
172 onPress={() => props.onEnvironmentChange(environment.environmentId)}
173 >
174 <NativeHeaderToolbar.Label>{environment.label}</NativeHeaderToolbar.Label>
175 </NativeHeaderToolbar.MenuAction>
176 ))}
177 </NativeHeaderToolbar.Menu>
178
179 {props.projects.length > 0 ? (
180 <NativeHeaderToolbar.Menu title="Project">
181 <NativeHeaderToolbar.Label>Project</NativeHeaderToolbar.Label>
182 <NativeHeaderToolbar.MenuAction
183 isOn={props.selectedProjectKey === null}
184 onPress={() => props.onProjectChange(null)}
185 subtitle="Show threads from every project"
186 >
187 <NativeHeaderToolbar.Label>All projects</NativeHeaderToolbar.Label>
188 </NativeHeaderToolbar.MenuAction>
189 {props.projects.map((project) => (
190 <NativeHeaderToolbar.MenuAction
191 key={project.key}
192 isOn={props.selectedProjectKey === project.key}
193 onPress={() => props.onProjectChange(project.key)}
194 >
195 <NativeHeaderToolbar.Label>{project.label}</NativeHeaderToolbar.Label>
196 </NativeHeaderToolbar.MenuAction>
197 ))}
198 </NativeHeaderToolbar.Menu>
199 ) : null}
200 </NativeHeaderToolbar.Menu>
201 <NativeHeaderToolbar.Spacer flexible />
202 <NativeHeaderToolbar.Button
203 accessibilityLabel="New task"
204 icon="square.and.pencil"
205 onPress={props.onStartNewTask}
206 separateBackground
207 />
208 </NativeHeaderToolbar>
209 )}
210 </>
211 );
212}