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

HomeHeader.tsx

189 lines8.4 KB

1import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
2import { use, useCallback, useRef } from "react";
3import { Platform } from "react-native";
4import type { SearchBarCommands } from "react-native-screens";
5import { NativePrimaryColumnContext } from "../../native/v5-workspace-context";
6import { useUniwindTheme } from "../../lib/useUniwindTheme";
7import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
8import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
9import {
10 createNativeMailSearchToolbarItem,
11 NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED,
12} from "../layout/native-mail-search-toolbar";
13import { buildHomeListFilterMenu } from "./home-list-filter-menu";
14import { createSidebarHeaderItems } from "../threads/sidebar-native-header-items";
15import type { HomeHeaderProps } from "./HomeHeader.types";
16​
17export type { HomeHeaderEnvironment } from "./HomeHeader.types";
18​
19export function HomeHeader(props: HomeHeaderProps) {
20 const primaryColumn = use(NativePrimaryColumnContext);
21 const iPadSidebar = Platform.OS === "ios" && Platform.isPad && primaryColumn !== null;
22 const searchBarRef = useRef<SearchBarCommands>(null);
23 const iconColor = useUniwindTheme()["--color-icon"];
24 // The list uses a fixed creation order and ignores sort/group options, so
25 // the filter menu only carries the filters and the "customized" icon state
26 // keys off those alone.
27 const hasCustomListOptions =
28 props.selectedEnvironmentId !== null || props.selectedProjectKey !== null;
29 const focusSearch = useCallback(() => {
30 searchBarRef.current?.focus();
31 return searchBarRef.current !== null;
32 }, []);
33 useHardwareKeyboardCommand("focusSearch", focusSearch);
34 const filterMenu = buildHomeListFilterMenu(props);
35​
36 return (
37 <>
38 <NativeStackScreenOptions
39 optionsVersion={filterMenu.items}
40 options={{
41 // Static header config (glass, title, fonts) lives in Stack.tsx
42 // (GLASS_HEADER_OPTIONS). Only dynamic values are set here.
43 headerTintColor: iconColor,
44 unstable_headerRightItems: () =>
45 iPadSidebar
46 ? createSidebarHeaderItems({
47 filterIcon: hasCustomListOptions
48 ? "line.3.horizontal.decrease.circle.fill"
49 : "line.3.horizontal.decrease.circle",
50 filterMenu,
51 onOpenSettings: props.onOpenSettings,
52 })
53 : [
54 withNativeGlassHeaderItem({
55 accessibilityLabel: "Open settings",
56 icon: { name: "ellipsis", type: "sfSymbol" } as const,
57 identifier: "home-settings",
58 label: "",
59 onPress: props.onOpenSettings,
60 type: "button",
61 }),
62 ],
63 // The keys below are set per-branch (not `undefined`) so a later
64 // reapply cannot clobber options owned by NativeHeaderToolbar.
65 ...(iPadSidebar
66 ? {
67 headerSearchBarOptions: {
68 ref: searchBarRef,
69 autoCapitalize: "none" as const,
70 hideNavigationBar: false,
71 hideWhenScrolling: false,
72 obscureBackground: false,
73 placement: "stacked" as const,
74 allowToolbarIntegration: false,
75 placeholder: "Search",
76 onCancelButtonPress: () => props.onSearchQueryChange(""),
77 onChangeText: (event) => props.onSearchQueryChange(event.nativeEvent.text),
78 },
79 unstable_headerToolbarItems: () => [],
80 }
81 : NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED
82 ? {
83 headerSearchBarOptions: {
84 ref: searchBarRef,
85 autoCapitalize: "none" as const,
86 onCancelButtonPress: () => props.onSearchQueryChange(""),
87 },
88 unstable_headerToolbarItems: () => [
89 createNativeMailSearchToolbarItem({
90 composeButtonId: "home-new-task",
91 composeSystemImageName: "square.and.pencil",
92 filterMenu,
93 filterButtonId: "home-filter",
94 filterSystemImageName: hasCustomListOptions
95 ? "line.3.horizontal.decrease.circle.fill"
96 : "line.3.horizontal.decrease",
97 onComposePress: props.onStartNewTask,
98 onSearchTextChange: props.onSearchQueryChange,
99 placeholder: "Search",
100 searchTextChangeId: "home-search-text",
101 showsSearchDismissButton: true,
102 }),
103 ],
104 }
105 : {
106 // Pre-Liquid-Glass iOS: standard pull-down search in the nav
107 // bar; create + sort live in the plain bottom toolbar below.
108 headerSearchBarOptions: {
109 ref: searchBarRef,
110 autoCapitalize: "none" as const,
111 hideNavigationBar: false,
112 placeholder: "Search",
113 onCancelButtonPress: () => {
114 props.onSearchQueryChange("");
115 },
116 onChangeText: (event) => {
117 props.onSearchQueryChange(event.nativeEvent.text);
118 },
119 },
120 }),
121 }}
122 />
123​
124 {iPadSidebar || NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED ? null : (
125 <NativeHeaderToolbar placement="bottom">
126 <NativeHeaderToolbar.Menu
127 accessibilityLabel="Filter threads"
128 icon={
129 hasCustomListOptions
130 ? "line.3.horizontal.decrease.circle.fill"
131 : "line.3.horizontal.decrease.circle"
132 }
133 title="Thread list options"
134 separateBackground
135 >
136 <NativeHeaderToolbar.Menu title="Environment">
137 <NativeHeaderToolbar.Label>Environment</NativeHeaderToolbar.Label>
138 <NativeHeaderToolbar.MenuAction
139 isOn={props.selectedEnvironmentId === null}
140 onPress={() => props.onEnvironmentChange(null)}
141 subtitle="Show threads from every environment"
142 >
143 <NativeHeaderToolbar.Label>All environments</NativeHeaderToolbar.Label>
144 </NativeHeaderToolbar.MenuAction>
145 {props.environments.map((environment) => (
146 <NativeHeaderToolbar.MenuAction
147 key={environment.environmentId}
148 isOn={props.selectedEnvironmentId === environment.environmentId}
149 onPress={() => props.onEnvironmentChange(environment.environmentId)}
150 >
151 <NativeHeaderToolbar.Label>{environment.label}</NativeHeaderToolbar.Label>
152 </NativeHeaderToolbar.MenuAction>
153 ))}
154 </NativeHeaderToolbar.Menu>
155​
156 {props.projects.length > 0 ? (
157 <NativeHeaderToolbar.Menu title="Project">
158 <NativeHeaderToolbar.Label>Project</NativeHeaderToolbar.Label>
159 <NativeHeaderToolbar.MenuAction
160 isOn={props.selectedProjectKey === null}
161 onPress={() => props.onProjectChange(null)}
162 subtitle="Show threads from every project"
163 >
164 <NativeHeaderToolbar.Label>All projects</NativeHeaderToolbar.Label>
165 </NativeHeaderToolbar.MenuAction>
166 {props.projects.map((project) => (
167 <NativeHeaderToolbar.MenuAction
168 key={project.key}
169 isOn={props.selectedProjectKey === project.key}
170 onPress={() => props.onProjectChange(project.key)}
171 >
172 <NativeHeaderToolbar.Label>{project.label}</NativeHeaderToolbar.Label>
173 </NativeHeaderToolbar.MenuAction>
174 ))}
175 </NativeHeaderToolbar.Menu>
176 ) : null}
177 </NativeHeaderToolbar.Menu>
178 <NativeHeaderToolbar.Spacer flexible />
179 <NativeHeaderToolbar.Button
180 accessibilityLabel="New task"
181 icon="square.and.pencil"
182 onPress={props.onStartNewTask}
183 separateBackground
184 />
185 </NativeHeaderToolbar>
186 )}
187 </>
188 );