Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

HomeHeader.tsx

233 lines10.0 KB

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 // The list uses a fixed creation order and ignores sort/group options, so
45 // the filter menu only carries the filters and the "customized" icon state
46 // keys off those alone.
47 const hasCustomListOptions =
48 props.selectedEnvironmentId !== null || props.selectedProjectKey !== null;
49 const focusSearch = useCallback(() => {
50 if (primaryColumn && !panes.primarySidebarVisible) {
51 focusAfterReveal.current = true;
52 togglePrimarySidebar();
53 return true;
54 }
55 searchBarRef.current?.focus();
56 return searchBarRef.current !== null;
57 }, [primaryColumn, panes.primarySidebarVisible, togglePrimarySidebar]);
58 useHardwareKeyboardCommand("focusSearch", focusSearch);
59 const filterMenu = buildHomeListFilterMenu(props);
60​
61 if (props.usesWorkspaceToolbar) {
62 return null;
63 }
64​
65 return (
66 <>
67 <NativeStackScreenOptions
68 optionsVersion={[filterMenu.items, showsCompactCompose]}
69 options={{
70 // Static header config (glass, title, fonts) lives in Stack.tsx
71 // (GLASS_HEADER_OPTIONS). Only dynamic values are set here.
72 headerTintColor: iconColor,
73 unstable_headerRightItems: () =>
74 sidebarHeader
75 ? [
76 ...createSidebarHeaderItems({
77 filterIcon: hasCustomListOptions
78 ? "line.3.horizontal.decrease.circle.fill"
79 : "line.3.horizontal.decrease.circle",
80 filterMenu,
81 onOpenSettings: props.onOpenSettings,
82 }),
83 ...(showsCompactCompose
84 ? [
85 withNativeGlassHeaderItem({
86 accessibilityLabel: "New task",
87 axisBehavior: "horizontalOnly",
88 icon: { name: "square.and.pencil", type: "sfSymbol" } as const,
89 identifier: "home-new-task",
90 label: "",
91 onPress: props.onStartNewTask,
92 type: "button",
93 }),
94 ]
95 : []),
96 ]
97 : [
98 withNativeGlassHeaderItem({
99 accessibilityLabel: "Open settings",
100 icon: { name: "ellipsis", type: "sfSymbol" } as const,
101 identifier: "home-settings",
102 label: "",
103 onPress: props.onOpenSettings,
104 type: "button",
105 }),
106 ],
107 // The keys below are set per-branch (not `undefined`) so a later
108 // reapply cannot clobber options owned by Toolbar.
109 ...(sidebarHeader
110 ? {
111 headerSearchBarOptions: {
112 ref: searchBarRef,
113 autoCapitalize: "none" as const,
114 hideNavigationBar: false,
115 hideWhenScrolling: false,
116 obscureBackground: false,
117 placement: "stacked" as const,
118 allowToolbarIntegration: false,
119 placeholder: "Search",
120 onCancelButtonPress: () => props.onSearchQueryChange(""),
121 onChangeText: (event) => props.onSearchQueryChange(event.nativeEvent.text),
122 },
123 unstable_headerToolbarItems: () => [],
124 }
125 : usesNativeMailSearchToolbar
126 ? {
127 headerSearchBarOptions: {
128 ref: searchBarRef,
129 autoCapitalize: "none" as const,
130 onCancelButtonPress: () => props.onSearchQueryChange(""),
131 },
132 unstable_headerToolbarItems: () => [
133 createNativeMailSearchToolbarItem({
134 composeButtonId: "home-new-task",
135 composeSystemImageName: "square.and.pencil",
136 filterMenu,
137 filterButtonId: "home-filter",
138 filterSystemImageName: hasCustomListOptions
139 ? "line.3.horizontal.decrease.circle.fill"
140 : "line.3.horizontal.decrease",
141 onComposePress: props.onStartNewTask,
142 onSearchTextChange: props.onSearchQueryChange,
143 placeholder: "Search",
144 searchTextChangeId: "home-search-text",
145 showsSearchDismissButton: true,
146 }),
147 ],
148 }
149 : {
150 // Pre-Liquid-Glass iOS: standard pull-down search in the nav
151 // bar; create + sort live in the plain bottom toolbar below.
152 headerSearchBarOptions: {
153 ref: searchBarRef,
154 autoCapitalize: "none" as const,
155 hideNavigationBar: false,
156 placeholder: "Search",
157 onCancelButtonPress: () => {
158 props.onSearchQueryChange("");
159 },
160 onChangeText: (event) => {
161 props.onSearchQueryChange(event.nativeEvent.text);
162 },
163 },
164 }),
165 }}
166 />
167​
168 {sidebarHeader || usesNativeMailSearchToolbar ? null : (
169 <Toolbar placement="bottom">
170 <Toolbar.Menu
171 accessibilityLabel="Filter threads"
172 icon={
173 hasCustomListOptions
174 ? "line.3.horizontal.decrease.circle.fill"
175 : "line.3.horizontal.decrease.circle"
176 }
177 title="Thread list options"
178 separateBackground
179 >
180 <Toolbar.Menu title="Environment">
181 <Toolbar.Label>Environment</Toolbar.Label>
182 <Toolbar.MenuAction
183 isOn={props.selectedEnvironmentId === null}
184 onPress={() => props.onEnvironmentChange(null)}
185 subtitle="Show threads from every environment"
186 >
187 <Toolbar.Label>All environments</Toolbar.Label>
188 </Toolbar.MenuAction>
189 {props.environments.map((environment) => (
190 <Toolbar.MenuAction
191 key={environment.environmentId}
192 isOn={props.selectedEnvironmentId === environment.environmentId}
193 onPress={() => props.onEnvironmentChange(environment.environmentId)}
194 >
195 <Toolbar.Label>{environment.label}</Toolbar.Label>
196 </Toolbar.MenuAction>
197 ))}
198 </Toolbar.Menu>
199​
200 {props.projects.length > 0 ? (
201 <Toolbar.Menu title="Project">
202 <Toolbar.Label>Project</Toolbar.Label>
203 <Toolbar.MenuAction
204 isOn={props.selectedProjectKey === null}
205 onPress={() => props.onProjectChange(null)}
206 subtitle="Show threads from every project"
207 >
208 <Toolbar.Label>All projects</Toolbar.Label>
209 </Toolbar.MenuAction>
210 {props.projects.map((project) => (
211 <Toolbar.MenuAction
212 key={project.key}
213 isOn={props.selectedProjectKey === project.key}
214 onPress={() => props.onProjectChange(project.key)}
215 >
216 <Toolbar.Label>{project.label}</Toolbar.Label>
217 </Toolbar.MenuAction>
218 ))}
219 </Toolbar.Menu>
220 ) : null}
221 </Toolbar.Menu>
222 <Toolbar.Spacer flexible />
223 <Toolbar.Button
224 accessibilityLabel="New task"
225 icon="square.and.pencil"
226 onPress={props.onStartNewTask}
227 separateBackground
228 />
229 </Toolbar>
230 )}
231 </>
232 );