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