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