| 1 | import type { MenuAction } from "@react-native-menu/menu"; |
| 2 | import type { ScreenHeaderMenuItem } from "./ScreenHeader.types"; |
| 3 | |
| 4 | export function androidHeaderMenuActions(items: ReadonlyArray<ScreenHeaderMenuItem>): MenuAction[] { |
| 5 | return items.flatMap((item): MenuAction[] => |
| 6 | "items" in item |
| 7 | ? item.inline && !item.title |
| 8 | ? androidHeaderMenuActions(item.items) |
| 9 | : [ |
| 10 | { |
| 11 | id: item.id, |
| 12 | title: item.title ?? "", |
| 13 | image: item.icon, |
| 14 | subactions: androidHeaderMenuActions(item.items), |
| 15 | }, |
| 16 | ] |
| 17 | : [ |
| 18 | { |
| 19 | id: item.id, |
| 20 | title: item.title, |
| 21 | subtitle: item.subtitle, |
| 22 | image: item.icon, |
| 23 | state: item.selected ? "on" : undefined, |
| 24 | attributes: item.disabled ? { disabled: true } : undefined, |
| 25 | }, |
| 26 | ], |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | export function findHeaderMenuAction( |
| 31 | items: ReadonlyArray<ScreenHeaderMenuItem>, |
| 32 | id: string, |
| 33 | ): Extract<ScreenHeaderMenuItem, { readonly onPress: () => void }> | undefined { |
| 34 | for (const item of items) { |
| 35 | if ("items" in item) { |
| 36 | const action = findHeaderMenuAction(item.items, id); |
| 37 | if (action) return action; |
| 38 | } else if (item.id === id) { |
| 39 | return item; |
| 40 | } |
| 41 | } |
| 42 | return undefined; |
| 43 | } |
| 44 | |
| 45 | export function splitAndroidHeaderActions<Action extends { readonly overflowOnly?: boolean }>( |
| 46 | actions: ReadonlyArray<Action>, |
| 47 | wide: boolean, |
| 48 | ) { |
| 49 | const inline = actions.filter((action) => !action.overflowOnly); |
| 50 | const directCount = actions.length > 2 ? (wide ? 3 : 1) : inline.length; |
| 51 | const visibleActions = inline.slice(0, directCount); |
| 52 | return { |
| 53 | visibleActions, |
| 54 | overflowActions: actions.filter((action) => !visibleActions.includes(action)), |
| 55 | }; |
| 56 | } |