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

HomeHeader.tsx

497 lines19.4 KB

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