Open History to see the last commit
1import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
2import { useCallback, useRef } from "react";
3import type { SearchBarCommands } from "react-native-screens";
4import { useUniwindTheme } from "../../lib/useUniwindTheme";
5import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
6import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
7import {
8 createNativeMailSearchToolbarItem,
9 NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED,
10} from "../layout/native-mail-search-toolbar";
11import { buildHomeListFilterMenu } from "./home-list-filter-menu";
12import type { HomeHeaderProps } from "./HomeHeader.types";
13
14export type { HomeHeaderEnvironment } from "./HomeHeader.types";
15
16export function HomeHeader(props: HomeHeaderProps) {
17 const searchBarRef = useRef<SearchBarCommands>(null);
18 const iconColor = useUniwindTheme()["--color-icon"];
19 // The list uses a fixed creation order and ignores sort/group options, so
20 // the filter menu only carries the filters and the "customized" icon state
21 // keys off those alone.
22 const hasCustomListOptions =
23 props.selectedEnvironmentId !== null || props.selectedProjectKey !== null;
24 const focusSearch = useCallback(() => {
25 searchBarRef.current?.focus();
26 return searchBarRef.current !== null;
27 }, []);
28 useHardwareKeyboardCommand("focusSearch", focusSearch);
29 const filterMenu = buildHomeListFilterMenu(props);
30
31 return (
32 <>
33 <NativeStackScreenOptions
34 optionsVersion={filterMenu.items}
35 options={{
36 // Static header config (glass, title, fonts) lives in Stack.tsx
37 // (GLASS_HEADER_OPTIONS). Only dynamic values are set here.
38 headerTintColor: iconColor,
39 unstable_headerRightItems: () => [
40 withNativeGlassHeaderItem({
41 accessibilityLabel: "Open settings",
42 icon: { name: "ellipsis", type: "sfSymbol" } as const,
43 identifier: "home-settings",
44 label: "",
45 onPress: props.onOpenSettings,
46 type: "button",
47 }),
48 ],
49 // The keys below are set per-branch (not `undefined`) so a later
50 // reapply cannot clobber options owned by NativeHeaderToolbar.
51 ...(NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED
52 ? {
53 unstable_headerToolbarItems: () => [
54 createNativeMailSearchToolbarItem({
55 composeButtonId: "home-new-task",
56 composeSystemImageName: "square.and.pencil",
57 filterMenu,
58 filterButtonId: "home-filter",
59 filterSystemImageName: hasCustomListOptions
60 ? "line.3.horizontal.decrease.circle.fill"
61 : "line.3.horizontal.decrease",
62 onComposePress: props.onStartNewTask,
63 onSearchTextChange: props.onSearchQueryChange,
64 placeholder: "Search",
65 searchTextChangeId: "home-search-text",
66 showsSearchDismissButton: true,
67 }),
68 ],
69 }
70 : {
71 // Pre-Liquid-Glass iOS: standard pull-down search in the nav
72 // bar; create + sort live in the plain bottom toolbar below.
73 headerSearchBarOptions: {
74 ref: searchBarRef,
75 autoCapitalize: "none" as const,
76 hideNavigationBar: false,
77 placeholder: "Search",
78 onCancelButtonPress: () => {
79 props.onSearchQueryChange("");
80 },
81 onChangeText: (event) => {
82 props.onSearchQueryChange(event.nativeEvent.text);
83 },
84 },
85 }),
86 }}
87 />
88
89 {NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED ? null : (
90 <NativeHeaderToolbar placement="bottom">
91 <NativeHeaderToolbar.Menu
92 accessibilityLabel="Filter threads"
93 icon={
94 hasCustomListOptions
95 ? "line.3.horizontal.decrease.circle.fill"
96 : "line.3.horizontal.decrease.circle"
97 }
98 title="Thread list options"
99 separateBackground
100 >
101 <NativeHeaderToolbar.Menu title="Environment">
102 <NativeHeaderToolbar.Label>Environment</NativeHeaderToolbar.Label>
103 <NativeHeaderToolbar.MenuAction
104 isOn={props.selectedEnvironmentId === null}
105 onPress={() => props.onEnvironmentChange(null)}
106 subtitle="Show threads from every environment"
107 >
108 <NativeHeaderToolbar.Label>All environments</NativeHeaderToolbar.Label>
109 </NativeHeaderToolbar.MenuAction>
110 {props.environments.map((environment) => (
111 <NativeHeaderToolbar.MenuAction
112 key={environment.environmentId}
113 isOn={props.selectedEnvironmentId === environment.environmentId}
114 onPress={() => props.onEnvironmentChange(environment.environmentId)}
115 >
116 <NativeHeaderToolbar.Label>{environment.label}</NativeHeaderToolbar.Label>
117 </NativeHeaderToolbar.MenuAction>
118 ))}
119 </NativeHeaderToolbar.Menu>
120
121 {props.projects.length > 0 ? (
122 <NativeHeaderToolbar.Menu title="Project">
123 <NativeHeaderToolbar.Label>Project</NativeHeaderToolbar.Label>
124 <NativeHeaderToolbar.MenuAction
125 isOn={props.selectedProjectKey === null}
126 onPress={() => props.onProjectChange(null)}
127 subtitle="Show threads from every project"
128 >
129 <NativeHeaderToolbar.Label>All projects</NativeHeaderToolbar.Label>
130 </NativeHeaderToolbar.MenuAction>
131 {props.projects.map((project) => (
132 <NativeHeaderToolbar.MenuAction
133 key={project.key}
134 isOn={props.selectedProjectKey === project.key}
135 onPress={() => props.onProjectChange(project.key)}
136 >
137 <NativeHeaderToolbar.Label>{project.label}</NativeHeaderToolbar.Label>
138 </NativeHeaderToolbar.MenuAction>
139 ))}
140 </NativeHeaderToolbar.Menu>
141 ) : null}
142 </NativeHeaderToolbar.Menu>
143 <NativeHeaderToolbar.Spacer flexible />
144 <NativeHeaderToolbar.Button
145 accessibilityLabel="New task"
146 icon="square.and.pencil"
147 onPress={props.onStartNewTask}
148 separateBackground
149 />
150 </NativeHeaderToolbar>
151 )}
152 </>
153 );
154}