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

HomeHeader.tsx

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