Open History to see the last commit
1import type {
2 EnvironmentId,
3 SidebarProjectGroupingMode,
4 SidebarThreadSortOrder,
5} from "@t3tools/contracts";
6import type { MenuAction } from "@react-native-menu/menu";
7import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
8import { useCallback, useMemo, useRef } from "react";
9import { Platform, Pressable, Text as RNText, TextInput, View } from "react-native";
10import type { SearchBarCommands } from "react-native-screens";
11import { useSafeAreaInsets } from "react-native-safe-area-context";
12
13import { ControlPillMenu } from "../../components/ControlPill";
14import { SymbolView } from "../../components/AppSymbol";
15import { T3Wordmark } from "../../components/T3Wordmark";
16import { useThemeColor } from "../../lib/useThemeColor";
17import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
18import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
19import { createNativeMailSearchToolbarItem } from "../layout/native-mail-search-toolbar";
20import type { HomeProjectSortOrder } from "./homeThreadList";
21import {
22 buildHomeListFilterMenu,
23 type HomeListFilterMenuEnvironment,
24} from "./home-list-filter-menu";
25import {
26 hasCustomHomeListOptions,
27 PROJECT_GROUPING_OPTIONS,
28 PROJECT_SORT_OPTIONS,
29 THREAD_SORT_OPTIONS,
30} from "./home-list-options";
31
32export type HomeHeaderEnvironment = HomeListFilterMenuEnvironment;
33
34export function HomeHeader(props: {
35 readonly environments: ReadonlyArray<HomeHeaderEnvironment>;
36 readonly searchQuery: string;
37 readonly selectedEnvironmentId: EnvironmentId | null;
38 readonly projectSortOrder: HomeProjectSortOrder;
39 readonly threadSortOrder: SidebarThreadSortOrder;
40 readonly projectGroupingMode: SidebarProjectGroupingMode;
41 readonly onSearchQueryChange: (query: string) => void;
42 readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void;
43 readonly onProjectSortOrderChange: (sortOrder: HomeProjectSortOrder) => void;
44 readonly onThreadSortOrderChange: (sortOrder: SidebarThreadSortOrder) => void;
45 readonly onProjectGroupingModeChange: (mode: SidebarProjectGroupingMode) => void;
46 readonly onOpenSettings: () => void;
47 readonly onStartNewTask: () => void;
48}) {
49 if (Platform.OS === "android") {
50 return <AndroidHomeHeader {...props} />;
51 }
52
53 return <IosHomeHeader {...props} />;
54}
55
56type HomeHeaderProps = Parameters<typeof HomeHeader>[0];
57
58function checkedMenuState(checked: boolean) {
59 return checked ? ("on" as const) : undefined;
60}
61
62function AndroidHomeHeader(props: HomeHeaderProps) {
63 const insets = useSafeAreaInsets();
64 const iconColor = useThemeColor("--color-icon");
65 const mutedColor = useThemeColor("--color-foreground-muted");
66 const hasCustomListOptions = hasCustomHomeListOptions(props);
67 const menuActions = useMemo<MenuAction[]>(
68 () => [
69 {
70 id: "environment",
71 title: "Environment",
72 subactions: [
73 {
74 id: "environment:all",
75 title: "All environments",
76 state: checkedMenuState(props.selectedEnvironmentId === null),
77 },
78 ...props.environments.map((environment) => ({
79 id: `environment:${environment.environmentId}`,
80 title: environment.label,
81 state: checkedMenuState(props.selectedEnvironmentId === environment.environmentId),
82 })),
83 ],
84 },
85 {
86 id: "project-sort",
87 title: "Sort projects",
88 subactions: PROJECT_SORT_OPTIONS.map((option) => ({
89 id: `project-sort:${option.value}`,
90 title: option.label,
91 state: checkedMenuState(props.projectSortOrder === option.value),
92 })),
93 },
94 {
95 id: "thread-sort",
96 title: "Sort threads",
97 subactions: THREAD_SORT_OPTIONS.map((option) => ({
98 id: `thread-sort:${option.value}`,
99 title: option.label,
100 state: checkedMenuState(props.threadSortOrder === option.value),
101 })),
102 },
103 {
104 id: "project-grouping",
105 title: "Group projects",
106 subactions: PROJECT_GROUPING_OPTIONS.map((option) => ({
107 id: `project-grouping:${option.value}`,
108 title: option.label,
109 state: checkedMenuState(props.projectGroupingMode === option.value),
110 })),
111 },
112 ],
113 [
114 props.environments,
115 props.projectGroupingMode,
116 props.projectSortOrder,
117 props.selectedEnvironmentId,
118 props.threadSortOrder,
119 ],
120 );
121 const handleMenuAction = useCallback(
122 (event: { nativeEvent: { event: string } }) => {
123 const id = event.nativeEvent.event;
124 if (id === "environment:all") {
125 props.onEnvironmentChange(null);
126 return;
127 }
128
129 if (id.startsWith("environment:")) {
130 const environmentId = id.slice("environment:".length);
131 const environment = props.environments.find(
132 (candidate) => candidate.environmentId === environmentId,
133 );
134 if (environment) {
135 props.onEnvironmentChange(environment.environmentId);
136 }
137 return;
138 }
139
140 const projectSort = PROJECT_SORT_OPTIONS.find(
141 (option) => id === `project-sort:${option.value}`,
142 );
143 if (projectSort) {
144 props.onProjectSortOrderChange(projectSort.value);
145 return;
146 }
147
148 const threadSort = THREAD_SORT_OPTIONS.find((option) => id === `thread-sort:${option.value}`);
149 if (threadSort) {
150 props.onThreadSortOrderChange(threadSort.value);
151 return;
152 }
153
154 const grouping = PROJECT_GROUPING_OPTIONS.find(
155 (option) => id === `project-grouping:${option.value}`,
156 );
157 if (grouping) {
158 props.onProjectGroupingModeChange(grouping.value);
159 }
160 },
161 [props],
162 );
163
164 return (
165 <>
166 <NativeStackScreenOptions options={{ headerShown: false }} />
167 <View
168 className="border-b border-header-border bg-header px-4 pb-3"
169 style={{
170 paddingTop: Math.max(insets.top, 12),
171 }}
172 >
173 <View className="w-full max-w-[720px] self-center gap-3">
174 <View className="flex-row items-center gap-2.5">
175 <View className="flex-1 flex-row items-center gap-2">
176 {/* Mirrors the desktop SidebarBrand: T3 mark + muted "Code". */}
177 <T3Wordmark color={iconColor} height={15} />
178 <RNText className="-ml-0.5 text-[21px] font-t3-medium tracking-[-0.5px] text-foreground-muted">
179 Code
180 </RNText>
181 <View className="rounded-full bg-subtle px-2 py-0.75">
182 <RNText className="text-[11px] font-t3-bold tracking-[1.1px] text-foreground-muted uppercase">
183 Alpha
184 </RNText>
185 </View>
186 </View>
187
188 <ControlPillMenu
189 actions={menuActions}
190 isAnchoredToRight
191 onPressAction={handleMenuAction}
192 >
193 <Pressable
194 accessibilityLabel="Filter and sort threads"
195 accessibilityRole="button"
196 className="size-11 items-center justify-center rounded-full bg-subtle"
197 >
198 <SymbolView
199 name={
200 hasCustomListOptions
201 ? "line.3.horizontal.decrease.circle.fill"
202 : "line.3.horizontal.decrease.circle"
203 }
204 size={16}
205 tintColor={iconColor}
206 type="monochrome"
207 />
208 </Pressable>
209 </ControlPillMenu>
210 {/* Built identically to the filter button so the two circles
211 match exactly (ControlPill sizes via Tailwind classes and
212 resolves to a different box). */}
213 <Pressable
214 accessibilityLabel="Open settings"
215 accessibilityRole="button"
216 onPress={props.onOpenSettings}
217 className="size-11 items-center justify-center rounded-full bg-subtle"
218 >
219 <SymbolView name="gearshape" size={18} tintColor={iconColor} type="monochrome" />
220 </Pressable>
221 </View>
222
223 <View className="min-h-12 flex-row items-center gap-2.5 rounded-2xl border border-input-border bg-input px-3.5">
224 <SymbolView name="magnifyingglass" size={17} tintColor={mutedColor} type="monochrome" />
225 <TextInput
226 accessibilityLabel="Search threads"
227 autoCapitalize="none"
228 onChangeText={props.onSearchQueryChange}
229 placeholder="Search threads"
230 placeholderTextColorClassName="accent-placeholder"
231 className="flex-1 py-2.5 text-base font-sans text-foreground"
232 value={props.searchQuery}
233 />
234 {props.searchQuery.length > 0 ? (
235 <Pressable
236 accessibilityLabel="Clear search"
237 hitSlop={10}
238 onPress={() => props.onSearchQueryChange("")}
239 >
240 <SymbolView
241 name="xmark.circle.fill"
242 size={17}
243 tintColor={mutedColor}
244 type="monochrome"
245 />
246 </Pressable>
247 ) : null}
248 </View>
249 </View>
250 </View>
251 </>
252 );
253}
254
255function IosHomeHeader(props: HomeHeaderProps) {
256 const searchBarRef = useRef<SearchBarCommands>(null);
257 const iconColor = useThemeColor("--color-icon");
258 const hasCustomListOptions = hasCustomHomeListOptions(props);
259 const focusSearch = useCallback(() => {
260 searchBarRef.current?.focus();
261 return searchBarRef.current !== null;
262 }, []);
263 useHardwareKeyboardCommand("focusSearch", focusSearch);
264 const filterMenu = buildHomeListFilterMenu(props);
265
266 return (
267 <>
268 <NativeStackScreenOptions
269 options={{
270 // Static header config (glass, title, fonts) lives in Stack.tsx
271 // (GLASS_HEADER_OPTIONS). Only dynamic values are set here.
272 headerTintColor: iconColor,
273 unstable_headerRightItems:
274 Platform.OS === "ios"
275 ? () => [
276 withNativeGlassHeaderItem({
277 accessibilityLabel: "Open settings",
278 icon: { name: "ellipsis", type: "sfSymbol" } as const,
279 identifier: "home-settings",
280 label: "",
281 onPress: props.onOpenSettings,
282 type: "button",
283 }),
284 ]
285 : undefined,
286 unstable_headerToolbarItems:
287 Platform.OS === "ios"
288 ? () => [
289 createNativeMailSearchToolbarItem({
290 composeButtonId: "home-new-task",
291 composeSystemImageName: "square.and.pencil",
292 filterMenu,
293 filterButtonId: "home-filter",
294 filterSystemImageName: hasCustomListOptions
295 ? "line.3.horizontal.decrease.circle.fill"
296 : "line.3.horizontal.decrease",
297 onComposePress: props.onStartNewTask,
298 onSearchTextChange: props.onSearchQueryChange,
299 placeholder: "Search",
300 searchTextChangeId: "home-search-text",
301 }),
302 ]
303 : undefined,
304 headerSearchBarOptions:
305 Platform.OS === "ios"
306 ? undefined
307 : {
308 ref: searchBarRef,
309 allowToolbarIntegration: true,
310 hideNavigationBar: false,
311 placeholder: "Search",
312 onCancelButtonPress: () => {
313 props.onSearchQueryChange("");
314 },
315 onChangeText: (event) => {
316 props.onSearchQueryChange(event.nativeEvent.text);
317 },
318 },
319 }}
320 />
321
322 {Platform.OS === "ios" ? null : (
323 <NativeHeaderToolbar placement="right">
324 <NativeHeaderToolbar.Button
325 accessibilityLabel="Open settings"
326 icon="gearshape"
327 onPress={props.onOpenSettings}
328 separateBackground
329 />
330 </NativeHeaderToolbar>
331 )}
332
333 {Platform.OS === "ios" ? null : (
334 <NativeHeaderToolbar placement="bottom">
335 <NativeHeaderToolbar.Menu
336 accessibilityLabel="Filter and sort threads"
337 icon={
338 hasCustomListOptions
339 ? "line.3.horizontal.decrease.circle.fill"
340 : "line.3.horizontal.decrease.circle"
341 }
342 title="Thread list options"
343 separateBackground
344 >
345 <NativeHeaderToolbar.MenuAction onPress={props.onOpenSettings}>
346 <NativeHeaderToolbar.Label>Settings</NativeHeaderToolbar.Label>
347 </NativeHeaderToolbar.MenuAction>
348
349 <NativeHeaderToolbar.Menu title="Environment">
350 <NativeHeaderToolbar.Label>Environment</NativeHeaderToolbar.Label>
351 <NativeHeaderToolbar.MenuAction
352 isOn={props.selectedEnvironmentId === null}
353 onPress={() => props.onEnvironmentChange(null)}
354 subtitle="Show threads from every environment"
355 >
356 <NativeHeaderToolbar.Label>All environments</NativeHeaderToolbar.Label>
357 </NativeHeaderToolbar.MenuAction>
358 {props.environments.map((environment) => (
359 <NativeHeaderToolbar.MenuAction
360 key={environment.environmentId}
361 isOn={props.selectedEnvironmentId === environment.environmentId}
362 onPress={() => props.onEnvironmentChange(environment.environmentId)}
363 >
364 <NativeHeaderToolbar.Label>{environment.label}</NativeHeaderToolbar.Label>
365 </NativeHeaderToolbar.MenuAction>
366 ))}
367 </NativeHeaderToolbar.Menu>
368
369 <NativeHeaderToolbar.Menu title="Sort projects">
370 <NativeHeaderToolbar.Label>Sort projects</NativeHeaderToolbar.Label>
371 {PROJECT_SORT_OPTIONS.map((option) => (
372 <NativeHeaderToolbar.MenuAction
373 key={option.value}
374 isOn={props.projectSortOrder === option.value}
375 onPress={() => props.onProjectSortOrderChange(option.value)}
376 >
377 <NativeHeaderToolbar.Label>{option.label}</NativeHeaderToolbar.Label>
378 </NativeHeaderToolbar.MenuAction>
379 ))}
380 </NativeHeaderToolbar.Menu>
381
382 <NativeHeaderToolbar.Menu title="Sort threads">
383 <NativeHeaderToolbar.Label>Sort threads</NativeHeaderToolbar.Label>
384 {THREAD_SORT_OPTIONS.map((option) => (
385 <NativeHeaderToolbar.MenuAction
386 key={option.value}
387 isOn={props.threadSortOrder === option.value}
388 onPress={() => props.onThreadSortOrderChange(option.value)}
389 >
390 <NativeHeaderToolbar.Label>{option.label}</NativeHeaderToolbar.Label>
391 </NativeHeaderToolbar.MenuAction>
392 ))}
393 </NativeHeaderToolbar.Menu>
394
395 <NativeHeaderToolbar.Menu title="Group projects">
396 <NativeHeaderToolbar.Label>Group projects</NativeHeaderToolbar.Label>
397 {PROJECT_GROUPING_OPTIONS.map((option) => (
398 <NativeHeaderToolbar.MenuAction
399 key={option.value}
400 isOn={props.projectGroupingMode === option.value}
401 onPress={() => props.onProjectGroupingModeChange(option.value)}
402 subtitle={option.subtitle}
403 >
404 <NativeHeaderToolbar.Label>{option.label}</NativeHeaderToolbar.Label>
405 </NativeHeaderToolbar.MenuAction>
406 ))}
407 </NativeHeaderToolbar.Menu>
408 </NativeHeaderToolbar.Menu>
409 <NativeHeaderToolbar.Spacer width={8} sharesBackground={false} />
410 <NativeHeaderToolbar.SearchBarSlot />
411 <NativeHeaderToolbar.Spacer width={8} sharesBackground={false} />
412 <NativeHeaderToolbar.Button
413 accessibilityLabel="New task"
414 icon="square.and.pencil"
415 onPress={props.onStartNewTask}
416 separateBackground
417 />
418 </NativeHeaderToolbar>
419 )}
420 </>
421 );
422}