Open History to see the last commit
1import type { MenuAction } from "@react-native-menu/menu";
2import type { AppBarMenuItem } from "./AppBar.types";
3
4export function androidHeaderMenuActions(items: ReadonlyArray<AppBarMenuItem>): 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 imageColor: item.iconColor,
24 state: item.selected ? "on" : undefined,
25 attributes:
26 item.disabled || item.destructive
27 ? { disabled: item.disabled, destructive: item.destructive }
28 : undefined,
29 },
30 ],
31 );
32}
33
34export function findHeaderMenuAction(
35 items: ReadonlyArray<AppBarMenuItem>,
36 id: string,
37): Extract<AppBarMenuItem, { readonly onPress: () => void }> | undefined {
38 for (const item of items) {
39 if ("items" in item) {
40 const action = findHeaderMenuAction(item.items, id);
41 if (action) return action;
42 } else if (item.id === id) {
43 return item;
44 }
45 }
46 return undefined;
47}
48
49export function splitAndroidHeaderActions<Action extends { readonly overflowOnly?: boolean }>(
50 actions: ReadonlyArray<Action>,
51 wide: boolean,
52) {
53 const inline = actions.filter((action) => !action.overflowOnly);
54 const directCount = actions.length > 2 ? (wide ? 3 : 1) : inline.length;
55 const visibleActions = inline.slice(0, directCount);
56 return {
57 visibleActions,
58 overflowActions: actions.filter((action) => !visibleActions.includes(action)),
59 };
60}