| ... | @@ -1,11 +1,6 @@ | ... | @@ -1,11 +1,6 @@ |
| 1 | import type { EnvironmentId, SidebarThreadSortOrder } from "@t3tools/contracts"; | | |
| 2 | import type { MenuAction } from "@react-native-menu/menu"; | | |
| 3 | | | |
| 4 | import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader"; | 1 | import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader"; |
| 5 | import { useCallback, useMemo, useRef } from "react"; | 2 | import { useCallback, useRef } from "react"; |
| 6 | import { Platform } from "react-native"; | | |
| 7 | import type { SearchBarCommands } from "react-native-screens"; | 3 | import type { SearchBarCommands } from "react-native-screens"; |
| 8 | | | |
| 9 | import { useUniwindTheme } from "../../lib/useUniwindTheme"; | 4 | import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 10 | import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled"; | 5 | import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled"; |
| 11 | import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands"; | 6 | import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands"; |
| ... | @@ -14,196 +9,17 @@ import { | ... | @@ -14,196 +9,17 @@ import { |
| 14 | createNativeMailSearchToolbarItem, | 9 | createNativeMailSearchToolbarItem, |
| 15 | NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED, | 10 | NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED, |
| 16 | } from "../layout/native-mail-search-toolbar"; | 11 | } from "../layout/native-mail-search-toolbar"; |
| 17 | import type { HomeProjectSortOrder } from "./homeThreadList"; | 12 | import { buildHomeListFilterMenu } from "./home-list-filter-menu"; |
| 18 | import { MaterialThreadListToolbar } from "./MaterialThreadListToolbar"; | | |
| 19 | import { | | |
| 20 | buildHomeListFilterMenu, | | |
| 21 | type HomeListFilterMenuEnvironment, | | |
| 22 | type HomeListFilterMenuProject, | | |
| 23 | } from "./home-list-filter-menu"; | | |
| 24 | import { | 13 | import { |
| 25 | hasCustomHomeListOptions, | 14 | hasCustomHomeListOptions, |
| 26 | PROJECT_SORT_OPTIONS, | 15 | PROJECT_SORT_OPTIONS, |
| 27 | THREAD_SORT_OPTIONS, | 16 | THREAD_SORT_OPTIONS, |
| 28 | } from "./home-list-options"; | 17 | } from "./home-list-options"; |
| | 18 | import type { HomeHeaderProps } from "./HomeHeader.types"; |
| 29 | | 19 | |
| 30 | export type HomeHeaderEnvironment = HomeListFilterMenuEnvironment; | 20 | export type { HomeHeaderEnvironment } from "./HomeHeader.types"; |
| 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 | } | | |
| 61 | | 21 | |
| 62 | function AndroidHomeHeader(props: HomeHeaderProps) { | 22 | export function HomeHeader(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) { | | |
| 207 | const searchBarRef = useRef<SearchBarCommands>(null); | 23 | const searchBarRef = useRef<SearchBarCommands>(null); |
| 208 | const iconColor = useUniwindTheme()["--color-icon"]; | 24 | const iconColor = useUniwindTheme()["--color-icon"]; |
| 209 | // Thread List v2 lays the list out in fixed creation order, so the | 25 | // Thread List v2 lays the list out in fixed creation order, so the |
| ... | @@ -231,19 +47,16 @@ function IosHomeHeader(props: HomeHeaderProps) { | ... | @@ -231,19 +47,16 @@ function IosHomeHeader(props: HomeHeaderProps) { |
| 231 | // Static header config (glass, title, fonts) lives in Stack.tsx | 47 | // Static header config (glass, title, fonts) lives in Stack.tsx |
| 232 | // (GLASS_HEADER_OPTIONS). Only dynamic values are set here. | 48 | // (GLASS_HEADER_OPTIONS). Only dynamic values are set here. |
| 233 | headerTintColor: iconColor, | 49 | headerTintColor: iconColor, |
| 234 | unstable_headerRightItems: | 50 | unstable_headerRightItems: () => [ |
| 235 | Platform.OS === "ios" | 51 | withNativeGlassHeaderItem({ |
| 236 | ? () => [ | 52 | accessibilityLabel: "Open settings", |
| 237 | withNativeGlassHeaderItem({ | 53 | icon: { name: "ellipsis", type: "sfSymbol" } as const, |
| 238 | accessibilityLabel: "Open settings", | 54 | identifier: "home-settings", |
| 239 | icon: { name: "ellipsis", type: "sfSymbol" } as const, | 55 | label: "", |
| 240 | identifier: "home-settings", | 56 | onPress: props.onOpenSettings, |
| 241 | label: "", | 57 | type: "button", |
| 242 | onPress: props.onOpenSettings, | 58 | }), |
| 243 | type: "button", | 59 | ], |
| 244 | }), | | |
| 245 | ] | | |
| 246 | : undefined, | | |
| 247 | // The keys below are set per-branch (not `undefined`) so a later | 60 | // The keys below are set per-branch (not `undefined`) so a later |
| 248 | // reapply cannot clobber options owned by NativeHeaderToolbar. | 61 | // reapply cannot clobber options owned by NativeHeaderToolbar. |
| 249 | ...(NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED | 62 | ...(NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED |