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 { WorkspaceConnectionTitle } from "./WorkspaceConnectionTitle";
25import {
26 buildHomeListFilterMenu,
27 type HomeListFilterMenuEnvironment,
28 type HomeListFilterMenuProject,
29} from "./home-list-filter-menu";
30import {
31 hasCustomHomeListOptions,
32 PROJECT_SORT_OPTIONS,
33 THREAD_SORT_OPTIONS,
34} from "./home-list-options";
35
36export type HomeHeaderEnvironment = HomeListFilterMenuEnvironment;
37
38export function HomeHeader(props: {
39 readonly environments: ReadonlyArray<HomeHeaderEnvironment>;
40 readonly projects: ReadonlyArray<HomeListFilterMenuProject>;
41 readonly searchQuery: string;
42 readonly selectedEnvironmentId: EnvironmentId | null;
43 readonly selectedProjectKey: string | null;
44 readonly projectSortOrder: HomeProjectSortOrder;
45 readonly threadSortOrder: SidebarThreadSortOrder;
46 readonly onSearchQueryChange: (query: string) => void;
47 readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void;
48 readonly onProjectChange: (projectKey: string | null) => void;
49 readonly onProjectSortOrderChange: (sortOrder: HomeProjectSortOrder) => void;
50 readonly onThreadSortOrderChange: (sortOrder: SidebarThreadSortOrder) => void;
51 readonly onOpenEnvironments: () => void;
52 readonly onOpenSettings: () => void;
53 readonly onStartNewTask: () => void;
54}) {
55 if (Platform.OS === "android") {
56 return <AndroidHomeHeader {...props} />;
57 }
58
59 return <IosHomeHeader {...props} />;
60}
61
62type HomeHeaderProps = Parameters<typeof HomeHeader>[0];
63
64function checkedMenuState(checked: boolean) {
65 return checked ? ("on" as const) : undefined;
66}
67
68function AndroidHomeHeader(props: HomeHeaderProps) {
69 const insets = useSafeAreaInsets();
70 const iconColor = useThemeColor("--color-icon");
71 const mutedColor = useThemeColor("--color-foreground-muted");
72 const stageLabel = resolveMobileStageLabel(Constants.expoConfig?.extra?.appVariant);
73 // Thread List v2 lays the list out in fixed creation order, so the
74 // sort/group filter controls would be silently ignored — hide them and
75 // key the "customized" icon state off the environment filter alone.
76 const threadListV2Enabled = useThreadListV2Enabled();
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 pb-3"
205 style={{
206 paddingHorizontal: HOME_HORIZONTAL_INSET,
207 paddingTop: Math.max(insets.top, 12),
208 }}
209 >
210 <View className="w-full max-w-[720px] self-center gap-3">
211 <View className="flex-row items-center gap-2.5">
212 {/* Brand slot doubles as the connection status surface: while an
213 environment reconnects, the lockup fades to a status label in
214 place (no layout shift in the list below). */}
215 <WorkspaceConnectionTitle
216 grow
217 onPress={props.onOpenEnvironments}
218 brand={
219 <View className="flex-row items-center gap-2">
220 {/* Mirrors the desktop SidebarBrand: T3 mark + muted "Code". */}
221 <T3Wordmark color={iconColor} height={15} />
222 <RNText className="-ml-0.5 text-[21px] font-t3-medium tracking-[-0.5px] text-foreground-muted">
223 Code
224 </RNText>
225 <View className="rounded-full bg-subtle px-2 py-0.75">
226 <RNText className="text-[11px] font-t3-bold tracking-[1.1px] text-foreground-muted uppercase">
227 {stageLabel}
228 </RNText>
229 </View>
230 </View>
231 }
232 />
233
234 <ControlPillMenu
235 actions={menuActions}
236 isAnchoredToRight
237 onPressAction={handleMenuAction}
238 >
239 <Pressable
240 accessibilityLabel="Filter and sort threads"
241 accessibilityRole="button"
242 className="size-11 items-center justify-center rounded-full bg-subtle"
243 >
244 <SymbolView
245 name={
246 hasCustomListOptions
247 ? "line.3.horizontal.decrease.circle.fill"
248 : "line.3.horizontal.decrease.circle"
249 }
250 size={16}
251 tintColor={iconColor}
252 type="monochrome"
253 />
254 </Pressable>
255 </ControlPillMenu>
256 {/* Built identically to the filter button so the two circles
257 match exactly (ControlPill sizes via Tailwind classes and
258 resolves to a different box). */}
259 <Pressable
260 accessibilityLabel="Open settings"
261 accessibilityRole="button"
262 onPress={props.onOpenSettings}
263 className="size-11 items-center justify-center rounded-full bg-subtle"
264 >
265 <SymbolView name="gearshape" size={18} tintColor={iconColor} type="monochrome" />
266 </Pressable>
267 </View>
268
269 <View className="min-h-12 flex-row items-center gap-2.5 rounded-2xl border border-input-border bg-input px-3.5">
270 <SymbolView name="magnifyingglass" size={17} tintColor={mutedColor} type="monochrome" />
271 <TextInput
272 accessibilityLabel="Search threads"
273 autoCapitalize="none"
274 onChangeText={props.onSearchQueryChange}
275 placeholder="Search threads"
276 placeholderTextColorClassName="accent-placeholder"
277 className="flex-1 py-2.5 text-base font-sans text-foreground"
278 value={props.searchQuery}
279 />
280 {props.searchQuery.length > 0 ? (
281 <Pressable
282 accessibilityLabel="Clear search"
283 hitSlop={10}
284 onPress={() => props.onSearchQueryChange("")}
285 >
286 <SymbolView
287 name="xmark.circle.fill"
288 size={17}
289 tintColor={mutedColor}
290 type="monochrome"
291 />
292 </Pressable>
293 ) : null}
294 </View>
295 </View>
296 </View>
297 </>
298 );
299}
300
301function IosHomeHeader(props: HomeHeaderProps) {
302 const searchBarRef = useRef<SearchBarCommands>(null);
303 const iconColor = useThemeColor("--color-icon");
304 // Thread List v2 lays the list out in fixed creation order, so the
305 // sort/group filter controls would be silently ignored — hide them and
306 // key the "customized" icon state off the environment filter alone.
307 const threadListV2Enabled = useThreadListV2Enabled();
308 const hasCustomListOptions = threadListV2Enabled
309 ? props.selectedEnvironmentId !== null || props.selectedProjectKey !== null
310 : hasCustomHomeListOptions(props);
311 const focusSearch = useCallback(() => {
312 searchBarRef.current?.focus();
313 return searchBarRef.current !== null;
314 }, []);
315 useHardwareKeyboardCommand("focusSearch", focusSearch);
316 const filterMenu = buildHomeListFilterMenu({
317 ...props,
318 listOrganization: !threadListV2Enabled,
319 });
320
321 return (
322 <>
323 <NativeStackScreenOptions
324 optionsVersion={filterMenu.items}
325 options={{
326 // Static header config (glass, title, fonts) lives in Stack.tsx
327 // (GLASS_HEADER_OPTIONS). Only dynamic values are set here.
328 headerTintColor: iconColor,
329 unstable_headerRightItems:
330 Platform.OS === "ios"
331 ? () => [
332 withNativeGlassHeaderItem({
333 accessibilityLabel: "Open settings",
334 icon: { name: "ellipsis", type: "sfSymbol" } as const,
335 identifier: "home-settings",
336 label: "",
337 onPress: props.onOpenSettings,
338 type: "button",
339 }),
340 ]
341 : undefined,
342 // The keys below are set per-branch (not `undefined`) so a later
343 // reapply cannot clobber options owned by NativeHeaderToolbar.
344 ...(NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED
345 ? {
346 unstable_headerToolbarItems: () => [
347 createNativeMailSearchToolbarItem({
348 composeButtonId: "home-new-task",
349 composeSystemImageName: "square.and.pencil",
350 filterMenu,
351 filterButtonId: "home-filter",
352 filterSystemImageName: hasCustomListOptions
353 ? "line.3.horizontal.decrease.circle.fill"
354 : "line.3.horizontal.decrease",
355 onComposePress: props.onStartNewTask,
356 onSearchTextChange: props.onSearchQueryChange,
357 placeholder: "Search",
358 searchTextChangeId: "home-search-text",
359 showsSearchDismissButton: true,
360 }),
361 ],
362 }
363 : {
364 // Pre-Liquid-Glass iOS: standard pull-down search in the nav
365 // bar; create + sort live in the plain bottom toolbar below.
366 headerSearchBarOptions: {
367 ref: searchBarRef,
368 autoCapitalize: "none" as const,
369 hideNavigationBar: false,
370 placeholder: "Search",
371 onCancelButtonPress: () => {
372 props.onSearchQueryChange("");
373 },
374 onChangeText: (event) => {
375 props.onSearchQueryChange(event.nativeEvent.text);
376 },
377 },
378 }),
379 }}
380 />
381
382 {NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED ? null : (
383 <NativeHeaderToolbar placement="bottom">
384 <NativeHeaderToolbar.Menu
385 accessibilityLabel="Filter and sort threads"
386 icon={
387 hasCustomListOptions
388 ? "line.3.horizontal.decrease.circle.fill"
389 : "line.3.horizontal.decrease.circle"
390 }
391 title="Thread list options"
392 separateBackground
393 >
394 <NativeHeaderToolbar.Menu title="Environment">
395 <NativeHeaderToolbar.Label>Environment</NativeHeaderToolbar.Label>
396 <NativeHeaderToolbar.MenuAction
397 isOn={props.selectedEnvironmentId === null}
398 onPress={() => props.onEnvironmentChange(null)}
399 subtitle="Show threads from every environment"
400 >
401 <NativeHeaderToolbar.Label>All environments</NativeHeaderToolbar.Label>
402 </NativeHeaderToolbar.MenuAction>
403 {props.environments.map((environment) => (
404 <NativeHeaderToolbar.MenuAction
405 key={environment.environmentId}
406 isOn={props.selectedEnvironmentId === environment.environmentId}
407 onPress={() => props.onEnvironmentChange(environment.environmentId)}
408 >
409 <NativeHeaderToolbar.Label>{environment.label}</NativeHeaderToolbar.Label>
410 </NativeHeaderToolbar.MenuAction>
411 ))}
412 </NativeHeaderToolbar.Menu>
413
414 {props.projects.length > 0 ? (
415 <NativeHeaderToolbar.Menu title="Project">
416 <NativeHeaderToolbar.Label>Project</NativeHeaderToolbar.Label>
417 <NativeHeaderToolbar.MenuAction
418 isOn={props.selectedProjectKey === null}
419 onPress={() => props.onProjectChange(null)}
420 subtitle="Show threads from every project"
421 >
422 <NativeHeaderToolbar.Label>All projects</NativeHeaderToolbar.Label>
423 </NativeHeaderToolbar.MenuAction>
424 {props.projects.map((project) => (
425 <NativeHeaderToolbar.MenuAction
426 key={project.key}
427 isOn={props.selectedProjectKey === project.key}
428 onPress={() => props.onProjectChange(project.key)}
429 >
430 <NativeHeaderToolbar.Label>{project.label}</NativeHeaderToolbar.Label>
431 </NativeHeaderToolbar.MenuAction>
432 ))}
433 </NativeHeaderToolbar.Menu>
434 ) : null}
435
436 {threadListV2Enabled ? null : (
437 <NativeHeaderToolbar.Menu title="Sort projects">
438 <NativeHeaderToolbar.Label>Sort projects</NativeHeaderToolbar.Label>
439 {PROJECT_SORT_OPTIONS.map((option) => (
440 <NativeHeaderToolbar.MenuAction
441 key={option.value}
442 isOn={props.projectSortOrder === option.value}
443 onPress={() => props.onProjectSortOrderChange(option.value)}
444 >
445 <NativeHeaderToolbar.Label>{option.label}</NativeHeaderToolbar.Label>
446 </NativeHeaderToolbar.MenuAction>
447 ))}
448 </NativeHeaderToolbar.Menu>
449 )}
450
451 {threadListV2Enabled ? null : (
452 <NativeHeaderToolbar.Menu title="Sort threads">
453 <NativeHeaderToolbar.Label>Sort threads</NativeHeaderToolbar.Label>
454 {THREAD_SORT_OPTIONS.map((option) => (
455 <NativeHeaderToolbar.MenuAction
456 key={option.value}
457 isOn={props.threadSortOrder === option.value}
458 onPress={() => props.onThreadSortOrderChange(option.value)}
459 >
460 <NativeHeaderToolbar.Label>{option.label}</NativeHeaderToolbar.Label>
461 </NativeHeaderToolbar.MenuAction>
462 ))}
463 </NativeHeaderToolbar.Menu>
464 )}
465 </NativeHeaderToolbar.Menu>
466 <NativeHeaderToolbar.Spacer flexible />
467 <NativeHeaderToolbar.Button
468 accessibilityLabel="New task"
469 icon="square.and.pencil"
470 onPress={props.onStartNewTask}
471 separateBackground
472 />
473 </NativeHeaderToolbar>
474 )}
475 </>
476 );
477}