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

HomeHeader.tsx

228 lines9.6 KB

1import { Toolbar } from "../../components/ui/Toolbar";
2import { NativeStackScreenOptions } from "../../native/StackHeader";
3import { use, useCallback, useEffect, useRef } from "react";
4import { useNavigation, type ParamListBase } from "@react-navigation/native";
5import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
6import { Platform } from "react-native";
7import type { SearchBarCommands } from "react-native-screens";
8import { NativePrimaryColumnContext } from "../../native/v5-workspace-context";
9import { useUniwindTheme } from "../../lib/useUniwindTheme";
10import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
11import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
12import { createNativeMailSearchToolbarItem } from "../layout/native-mail-search-toolbar";
13import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
14import { useDeviceType } from "../../native/native-layout-metrics";
15import { buildHomeListFilterMenu } from "./home-list-filter-menu";
16import { createSidebarHeaderItems } from "../threads/sidebar-native-header-items";
17import type { HomeHeaderProps } from "./HomeHeader.types";
18import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
19​
20export type { HomeHeaderEnvironment } from "./HomeHeader.types";
21​
22export function HomeHeader(props: HomeHeaderProps) {
23 const navigation = useNavigation<NativeStackNavigationProp<ParamListBase>>();
24 const { layout, panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
25 const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();
26 const deviceType = useDeviceType();
27 const primaryColumn = use(NativePrimaryColumnContext);
28 const sidebarHeader =
29 Platform.OS === "ios" &&
30 primaryColumn !== null &&
31 (deviceType === "pad" || !usesNativeMailSearchToolbar);
32 const showsCompactCompose = deviceType === "pad" && !layout.usesSplitView;
33 const searchBarRef = useRef<SearchBarCommands>(null);
34 const focusAfterReveal = useRef(false);
35 useEffect(
36 () =>
37 navigation.addListener("transitionEnd", (event) => {
38 if (focusAfterReveal.current && !event.data.closing) {
39 focusAfterReveal.current = false;
40 searchBarRef.current?.focus();
41 }
42 }),
43 [navigation],
44 );
45 const iconColor = useUniwindTheme()["--color-icon"];
46 const hasCustomListOptions =
47 props.selectedEnvironmentId !== null || props.selectedProjectKey !== null;
48 const focusSearch = useCallback(() => {
49 if (primaryColumn && !panes.primarySidebarVisible) {
50 focusAfterReveal.current = true;
51 togglePrimarySidebar();
52 return true;
53 }
54 searchBarRef.current?.focus();
55 return searchBarRef.current !== null;
56 }, [primaryColumn, panes.primarySidebarVisible, togglePrimarySidebar]);
57 useHardwareKeyboardCommand("focusSearch", focusSearch);
58 const filterMenu = buildHomeListFilterMenu(props);
59​
60 if (props.usesWorkspaceToolbar) {
61 return null;
62 }
63​
64 return (
65 <>
66 <NativeStackScreenOptions
67 optionsVersion={[filterMenu.items, showsCompactCompose]}
68 options={{
69 headerTintColor: iconColor,
70 unstable_headerRightItems: () =>
71 sidebarHeader
72 ? [
73 ...createSidebarHeaderItems({
74 filterIcon: hasCustomListOptions
75 ? "line.3.horizontal.decrease.circle.fill"
76 : "line.3.horizontal.decrease.circle",
77 filterMenu,
78 onOpenSettings: props.onOpenSettings,
79 }),
80 ...(showsCompactCompose
81 ? [
82 withNativeGlassHeaderItem({
83 accessibilityLabel: "New task",
84 axisBehavior: "horizontalOnly",
85 icon: { name: "square.and.pencil", type: "sfSymbol" } as const,
86 identifier: "home-new-task",
87 label: "",
88 onPress: props.onStartNewTask,
89 type: "button",
90 }),
91 ]
92 : []),
93 ]
94 : [
95 withNativeGlassHeaderItem({
96 accessibilityLabel: "Open settings",
97 icon: { name: "ellipsis", type: "sfSymbol" } as const,
98 identifier: "home-settings",
99 label: "",
100 onPress: props.onOpenSettings,
101 type: "button",
102 }),
103 ],
104 // The keys below are set per-branch (not `undefined`) so a later
105 // reapply cannot clobber options owned by Toolbar.
106 ...(sidebarHeader
107 ? {
108 headerSearchBarOptions: {
109 ref: searchBarRef,
110 autoCapitalize: "none" as const,
111 hideNavigationBar: false,
112 hideWhenScrolling: false,
113 obscureBackground: false,
114 placement: "stacked" as const,
115 allowToolbarIntegration: false,
116 placeholder: "Search",
117 onCancelButtonPress: () => props.onSearchQueryChange(""),
118 onChangeText: (event) => props.onSearchQueryChange(event.nativeEvent.text),
119 },
120 unstable_headerToolbarItems: () => [],
121 }
122 : usesNativeMailSearchToolbar
123 ? {
124 headerSearchBarOptions: {
125 ref: searchBarRef,
126 autoCapitalize: "none" as const,
127 onCancelButtonPress: () => props.onSearchQueryChange(""),
128 },
129 unstable_headerToolbarItems: () => [
130 createNativeMailSearchToolbarItem({
131 composeButtonId: "home-new-task",
132 composeSystemImageName: "square.and.pencil",
133 filterMenu,
134 filterButtonId: "home-filter",
135 filterSystemImageName: hasCustomListOptions
136 ? "line.3.horizontal.decrease.circle.fill"
137 : "line.3.horizontal.decrease",
138 onComposePress: props.onStartNewTask,
139 onSearchTextChange: props.onSearchQueryChange,
140 placeholder: "Search",
141 searchTextChangeId: "home-search-text",
142 showsSearchDismissButton: true,
143 }),
144 ],
145 }
146 : {
147 headerSearchBarOptions: {
148 ref: searchBarRef,
149 autoCapitalize: "none" as const,
150 hideNavigationBar: false,
151 placeholder: "Search",
152 onCancelButtonPress: () => {
153 props.onSearchQueryChange("");
154 },
155 onChangeText: (event) => {
156 props.onSearchQueryChange(event.nativeEvent.text);
157 },
158 },
159 }),
160 }}
161 />
162​
163 {sidebarHeader || usesNativeMailSearchToolbar ? null : (
164 <Toolbar placement="bottom">
165 <Toolbar.Menu
166 accessibilityLabel="Filter threads"
167 icon={
168 hasCustomListOptions
169 ? "line.3.horizontal.decrease.circle.fill"
170 : "line.3.horizontal.decrease.circle"
171 }
172 title="Thread list options"
173 separateBackground
174 >
175 <Toolbar.Menu title="Environment">
176 <Toolbar.Label>Environment</Toolbar.Label>
177 <Toolbar.MenuAction
178 isOn={props.selectedEnvironmentId === null}
179 onPress={() => props.onEnvironmentChange(null)}
180 subtitle="Show threads from every environment"
181 >
182 <Toolbar.Label>All environments</Toolbar.Label>
183 </Toolbar.MenuAction>
184 {props.environments.map((environment) => (
185 <Toolbar.MenuAction
186 key={environment.environmentId}
187 isOn={props.selectedEnvironmentId === environment.environmentId}
188 onPress={() => props.onEnvironmentChange(environment.environmentId)}
189 >
190 <Toolbar.Label>{environment.label}</Toolbar.Label>
191 </Toolbar.MenuAction>
192 ))}
193 </Toolbar.Menu>
194​
195 {props.projects.length > 0 ? (
196 <Toolbar.Menu title="Project">
197 <Toolbar.Label>Project</Toolbar.Label>
198 <Toolbar.MenuAction
199 isOn={props.selectedProjectKey === null}
200 onPress={() => props.onProjectChange(null)}
201 subtitle="Show threads from every project"
202 >
203 <Toolbar.Label>All projects</Toolbar.Label>
204 </Toolbar.MenuAction>
205 {props.projects.map((project) => (
206 <Toolbar.MenuAction
207 key={project.key}
208 isOn={props.selectedProjectKey === project.key}
209 onPress={() => props.onProjectChange(project.key)}
210 >
211 <Toolbar.Label>{project.label}</Toolbar.Label>
212 </Toolbar.MenuAction>
213 ))}
214 </Toolbar.Menu>
215 ) : null}
216 </Toolbar.Menu>
217 <Toolbar.Spacer flexible />
218 <Toolbar.Button
219 accessibilityLabel="New task"
220 icon="square.and.pencil"
221 onPress={props.onStartNewTask}
222 separateBackground
223 />
224 </Toolbar>
225 )}
226 </>
227 );