| ... | ... | @@ -1,11 +1,6 @@ |
| 1 | | import type { EnvironmentId, SidebarThreadSortOrder } from "@t3tools/contracts"; |
| 2 | | import type { MenuAction } from "@react-native-menu/menu"; |
| 3 | | |
| 4 | 1 | import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader"; |
| 5 | | import { useCallback, useMemo, useRef } from "react"; |
| 6 | | import { Platform } from "react-native"; |
| 2 | import { useCallback, useRef } from "react"; |
| 7 | 3 | import type { SearchBarCommands } from "react-native-screens"; |
| 8 | | |
| 9 | 4 | import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 10 | 5 | import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled"; |
| 11 | 6 | import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands"; |
| ... | ... | @@ -14,196 +9,17 @@ import { |
| 14 | 9 | createNativeMailSearchToolbarItem, |
| 15 | 10 | NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED, |
| 16 | 11 | } from "../layout/native-mail-search-toolbar"; |
| 17 | | import type { HomeProjectSortOrder } from "./homeThreadList"; |
| 18 | | import { MaterialThreadListToolbar } from "./MaterialThreadListToolbar"; |
| 19 | | import { |
| 20 | | buildHomeListFilterMenu, |
| 21 | | type HomeListFilterMenuEnvironment, |
| 22 | | type HomeListFilterMenuProject, |
| 23 | | } from "./home-list-filter-menu"; |
| 12 | import { buildHomeListFilterMenu } from "./home-list-filter-menu"; |
| 24 | 13 | import { |
| 25 | 14 | hasCustomHomeListOptions, |
| 26 | 15 | PROJECT_SORT_OPTIONS, |
| 27 | 16 | THREAD_SORT_OPTIONS, |
| 28 | 17 | } from "./home-list-options"; |
| 18 | import type { HomeHeaderProps } from "./HomeHeader.types"; |
| 29 | 19 | |
| 30 | | export type HomeHeaderEnvironment = HomeListFilterMenuEnvironment; |
| 31 | | |
| 32 | | export function HomeHeader(props: { |
| 33 | | readonly environments: ReadonlyArray<HomeHeaderEnvironment>; |
| 34 | | readonly projects: ReadonlyArray<HomeListFilterMenuProject>; |
| 35 | | readonly searchQuery: string; |
| 36 | | readonly selectedEnvironmentId: EnvironmentId | null; |
| 37 | | readonly selectedProjectKey: string | null; |
| 38 | | readonly projectSortOrder: HomeProjectSortOrder; |
| 39 | | readonly threadSortOrder: SidebarThreadSortOrder; |
| 40 | | readonly onSearchQueryChange: (query: string) => void; |
| 41 | | readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void; |
| 42 | | readonly onProjectChange: (projectKey: string | null) => void; |
| 43 | | readonly onProjectSortOrderChange: (sortOrder: HomeProjectSortOrder) => void; |
| 44 | | readonly onThreadSortOrderChange: (sortOrder: SidebarThreadSortOrder) => void; |
| 45 | | readonly onOpenEnvironments: () => void; |
| 46 | | readonly onOpenSettings: () => void; |
| 47 | | readonly onStartNewTask: () => void; |
| 48 | | }) { |
| 49 | | if (Platform.OS === "android") { |
| 50 | | return <AndroidHomeHeader {...props} />; |
| 51 | | } |
| 52 | | |
| 53 | | return <IosHomeHeader {...props} />; |
| 54 | | } |
| 55 | | |
| 56 | | type HomeHeaderProps = Parameters<typeof HomeHeader>[0]; |
| 57 | | |
| 58 | | function checkedMenuState(checked: boolean) { |
| 59 | | return checked ? ("on" as const) : undefined; |
| 60 | | } |
| 20 | export type { HomeHeaderEnvironment } from "./HomeHeader.types"; |
| 61 | 21 | |
| 62 | | function AndroidHomeHeader(props: HomeHeaderProps) { |
| 63 | | // Thread List v2 lays the list out in fixed creation order, so the |
| 64 | | // sort/group filter controls would be silently ignored — hide them and |
| 65 | | // key the "customized" icon state off the environment filter alone. |
| 66 | | const threadListV2Enabled = useThreadListV2Enabled(); |
| 67 | | const hasCustomListOptions = threadListV2Enabled |
| 68 | | ? props.selectedEnvironmentId !== null || props.selectedProjectKey !== null |
| 69 | | : hasCustomHomeListOptions(props); |
| 70 | | const menuActions = useMemo<MenuAction[]>( |
| 71 | | () => [ |
| 72 | | { |
| 73 | | id: "environment", |
| 74 | | title: "Environment", |
| 75 | | subactions: [ |
| 76 | | { |
| 77 | | id: "environment:all", |
| 78 | | title: "All environments", |
| 79 | | state: checkedMenuState(props.selectedEnvironmentId === null), |
| 80 | | }, |
| 81 | | ...props.environments.map((environment) => ({ |
| 82 | | id: `environment:${environment.environmentId}`, |
| 83 | | title: environment.label, |
| 84 | | state: checkedMenuState(props.selectedEnvironmentId === environment.environmentId), |
| 85 | | })), |
| 86 | | ], |
| 87 | | }, |
| 88 | | ...(props.projects.length === 0 |
| 89 | | ? [] |
| 90 | | : ([ |
| 91 | | { |
| 92 | | id: "project", |
| 93 | | title: "Project", |
| 94 | | subactions: [ |
| 95 | | { |
| 96 | | id: "project:all", |
| 97 | | title: "All projects", |
| 98 | | state: checkedMenuState(props.selectedProjectKey === null), |
| 99 | | }, |
| 100 | | ...props.projects.map((project) => ({ |
| 101 | | id: `project:${project.key}`, |
| 102 | | title: project.label, |
| 103 | | state: checkedMenuState(props.selectedProjectKey === project.key), |
| 104 | | })), |
| 105 | | ], |
| 106 | | }, |
| 107 | | ] satisfies MenuAction[])), |
| 108 | | ...(threadListV2Enabled |
| 109 | | ? [] |
| 110 | | : ([ |
| 111 | | { |
| 112 | | id: "project-sort", |
| 113 | | title: "Sort projects", |
| 114 | | subactions: PROJECT_SORT_OPTIONS.map((option) => ({ |
| 115 | | id: `project-sort:${option.value}`, |
| 116 | | title: option.label, |
| 117 | | state: checkedMenuState(props.projectSortOrder === option.value), |
| 118 | | })), |
| 119 | | }, |
| 120 | | { |
| 121 | | id: "thread-sort", |
| 122 | | title: "Sort threads", |
| 123 | | subactions: THREAD_SORT_OPTIONS.map((option) => ({ |
| 124 | | id: `thread-sort:${option.value}`, |
| 125 | | title: option.label, |
| 126 | | state: checkedMenuState(props.threadSortOrder === option.value), |
| 127 | | })), |
| 128 | | }, |
| 129 | | ] satisfies MenuAction[])), |
| 130 | | ], |
| 131 | | [ |
| 132 | | props.environments, |
| 133 | | props.projectSortOrder, |
| 134 | | props.projects, |
| 135 | | props.selectedEnvironmentId, |
| 136 | | props.selectedProjectKey, |
| 137 | | props.threadSortOrder, |
| 138 | | threadListV2Enabled, |
| 139 | | ], |
| 140 | | ); |
| 141 | | const handleMenuAction = useCallback( |
| 142 | | (event: { nativeEvent: { event: string } }) => { |
| 143 | | const id = event.nativeEvent.event; |
| 144 | | if (id === "environment:all") { |
| 145 | | props.onEnvironmentChange(null); |
| 146 | | return; |
| 147 | | } |
| 148 | | |
| 149 | | if (id.startsWith("environment:")) { |
| 150 | | const environmentId = id.slice("environment:".length); |
| 151 | | const environment = props.environments.find( |
| 152 | | (candidate) => candidate.environmentId === environmentId, |
| 153 | | ); |
| 154 | | if (environment) { |
| 155 | | props.onEnvironmentChange(environment.environmentId); |
| 156 | | } |
| 157 | | return; |
| 158 | | } |
| 159 | | |
| 160 | | if (id === "project:all") { |
| 161 | | props.onProjectChange(null); |
| 162 | | return; |
| 163 | | } |
| 164 | | |
| 165 | | if (id.startsWith("project:")) { |
| 166 | | const projectKey = id.slice("project:".length); |
| 167 | | if (props.projects.some((project) => project.key === projectKey)) { |
| 168 | | props.onProjectChange(projectKey); |
| 169 | | } |
| 170 | | return; |
| 171 | | } |
| 172 | | |
| 173 | | const projectSort = PROJECT_SORT_OPTIONS.find( |
| 174 | | (option) => id === `project-sort:${option.value}`, |
| 175 | | ); |
| 176 | | if (projectSort) { |
| 177 | | props.onProjectSortOrderChange(projectSort.value); |
| 178 | | return; |
| 179 | | } |
| 180 | | |
| 181 | | const threadSort = THREAD_SORT_OPTIONS.find((option) => id === `thread-sort:${option.value}`); |
| 182 | | if (threadSort) { |
| 183 | | props.onThreadSortOrderChange(threadSort.value); |
| 184 | | return; |
| 185 | | } |
| 186 | | }, |
| 187 | | [props], |
| 188 | | ); |
| 189 | | |
| 190 | | return ( |
| 191 | | <> |
| 192 | | <NativeStackScreenOptions options={{ headerShown: false }} /> |
| 193 | | <MaterialThreadListToolbar |
| 194 | | searchQuery={props.searchQuery} |
| 195 | | onSearchQueryChange={props.onSearchQueryChange} |
| 196 | | filterActions={menuActions} |
| 197 | | filterCustomized={hasCustomListOptions} |
| 198 | | onFilterAction={handleMenuAction} |
| 199 | | onOpenSettings={props.onOpenSettings} |
| 200 | | onOpenEnvironments={props.onOpenEnvironments} |
| 201 | | /> |
| 202 | | </> |
| 203 | | ); |
| 204 | | } |
| 205 | | |
| 206 | | function IosHomeHeader(props: HomeHeaderProps) { |
| 22 | export function HomeHeader(props: HomeHeaderProps) { |
| 207 | 23 | const searchBarRef = useRef<SearchBarCommands>(null); |
| 208 | 24 | const iconColor = useUniwindTheme()["--color-icon"]; |
| 209 | 25 | // Thread List v2 lays the list out in fixed creation order, so the |
| ... | ... | @@ -231,19 +47,16 @@ function IosHomeHeader(props: HomeHeaderProps) { |
| 231 | 47 | // Static header config (glass, title, fonts) lives in Stack.tsx |
| 232 | 48 | // (GLASS_HEADER_OPTIONS). Only dynamic values are set here. |
| 233 | 49 | headerTintColor: iconColor, |
| 234 | | unstable_headerRightItems: |
| 235 | | Platform.OS === "ios" |
| 236 | | ? () => [ |
| 237 | | withNativeGlassHeaderItem({ |
| 238 | | accessibilityLabel: "Open settings", |
| 239 | | icon: { name: "ellipsis", type: "sfSymbol" } as const, |
| 240 | | identifier: "home-settings", |
| 241 | | label: "", |
| 242 | | onPress: props.onOpenSettings, |
| 243 | | type: "button", |
| 244 | | }), |
| 245 | | ] |
| 246 | | : undefined, |
| 50 | unstable_headerRightItems: () => [ |
| 51 | withNativeGlassHeaderItem({ |
| 52 | accessibilityLabel: "Open settings", |
| 53 | icon: { name: "ellipsis", type: "sfSymbol" } as const, |
| 54 | identifier: "home-settings", |
| 55 | label: "", |
| 56 | onPress: props.onOpenSettings, |
| 57 | type: "button", |
| 58 | }), |
| 59 | ], |
| 247 | 60 | // The keys below are set per-branch (not `undefined`) so a later |
| 248 | 61 | // reapply cannot clobber options owned by NativeHeaderToolbar. |
| 249 | 62 | ...(NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED |