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