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