Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

HomeHeader.tsx

460 lines18.0 KB

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