Skip to content

Commit aa741fd

Browse files
refactor(mobile): compose header menus from shared definitions
1 parent a3f785a commit aa741fd

7 files changed

Lines changed: 115 additions & 86 deletions

File tree

‎apps/mobile/src/components/ScreenHeader.android.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.and
1212
import type { ScreenHeaderProps } from "./ScreenHeader.types";
1313

1414
export function ScreenHeader(props: ScreenHeaderProps) {
15-
const { search, menu } = props;
15+
const { search } = props;
1616
const insets = useSafeAreaInsets();
1717
const { themeVariables } = useAppearancePreferences();
1818
const inputRef = useRef<TextInput>(null);
@@ -32,8 +32,9 @@ export function ScreenHeader(props: ScreenHeaderProps) {
3232
});
3333
return () => subscription.remove();
3434
}, [closeSearch, searching, search?.mode]);
35-
const menuView = menu ? (
35+
const menuView = props.menus?.map((menu) => (
3636
<ControlPillMenu
37+
key={menu.title}
3738
actions={androidHeaderMenuActions(menu.items)}
3839
isAnchoredToRight
3940
title={menu.status ?? menu.title}
@@ -59,7 +60,7 @@ export function ScreenHeader(props: ScreenHeaderProps) {
5960
<AndroidHeaderIconButton accessibilityLabel={menu.title} icon={menu.icon} />
6061
)}
6162
</ControlPillMenu>
62-
) : null;
63+
));
6364
const options = (
6465
<NativeStackScreenOptions
6566
options={{

‎apps/mobile/src/components/ScreenHeader.ios.tsx‎

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { HeaderBarButtonMailSearchToolbarItem } from "react-native-screens";
22
import { useId } from "react";
3+
import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios";
34
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../native/StackHeader";
45
import { useAdaptiveWorkspaceLayout } from "../features/layout/AdaptiveWorkspaceLayout";
56
import {
@@ -14,32 +15,6 @@ function iosIcon(icon: AppSymbolName) {
1415
return typeof icon === "string" ? icon : icon.ios;
1516
}
1617

17-
function renderMenuItems(items: ReadonlyArray<ScreenHeaderMenuItem>) {
18-
return items.map((item) =>
19-
"items" in item ? (
20-
<NativeHeaderToolbar.Menu
21-
key={item.id}
22-
title={item.title}
23-
icon={item.icon}
24-
inline={item.inline}
25-
>
26-
{renderMenuItems(item.items)}
27-
</NativeHeaderToolbar.Menu>
28-
) : (
29-
<NativeHeaderToolbar.MenuAction
30-
key={item.id}
31-
icon={item.icon}
32-
subtitle={item.subtitle}
33-
disabled={item.disabled}
34-
isOn={item.selected}
35-
onPress={item.onPress}
36-
>
37-
{item.title}
38-
</NativeHeaderToolbar.MenuAction>
39-
),
40-
);
41-
}
42-
4318
type MailMenu = NonNullable<HeaderBarButtonMailSearchToolbarItem["filterMenu"]>;
4419
function mailMenuItems(items: ReadonlyArray<ScreenHeaderMenuItem>): MailMenu["items"] {
4520
return items.map((item) =>
@@ -59,11 +34,13 @@ export function ScreenHeader(props: ScreenHeaderProps) {
5934
const headerId = useId();
6035
const { layout, panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
6136
const { themeVariables } = useAppearancePreferences();
62-
const { search, menu } = props;
37+
const { search, menus } = props;
38+
const menu = menus?.[0];
6339
const compactSearch =
6440
search !== undefined &&
6541
(search.compactToolbar ?? !layout.usesSplitView) &&
6642
NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED;
43+
const visibleMenus = compactSearch ? (menus?.slice(1) ?? []) : (menus ?? []);
6744
const refresh = search?.refreshInToolbar ? search.onRefresh : undefined;
6845
return (
6946
<>
@@ -136,12 +113,16 @@ export function ScreenHeader(props: ScreenHeaderProps) {
136113
panes.primarySidebarVisible ? "arrow.up.left.and.arrow.down.right" : "sidebar.left"
137114
}
138115
onPress={togglePrimarySidebar}
139-
separateBackground
116+
separateBackground={
117+
props.backInSplitView ? props.backInSplitView.separateBackground : true
118+
}
140119
/>
141120
</NativeHeaderToolbar>
142121
) : null}
143-
{(props.actions?.length || refresh || menu || props.trailing) &&
144-
!compactSearch &&
122+
{(props.actions?.length ||
123+
(!compactSearch && refresh) ||
124+
visibleMenus.length ||
125+
props.trailing) &&
145126
props.options?.unstable_headerRightItems === undefined ? (
146127
<NativeHeaderToolbar placement="right">
147128
{props.actions?.map((action) => (
@@ -152,27 +133,15 @@ export function ScreenHeader(props: ScreenHeaderProps) {
152133
separateBackground
153134
/>
154135
))}
155-
{refresh ? (
136+
{refresh && !compactSearch ? (
156137
<NativeHeaderToolbar.Button
157138
accessibilityLabel={search?.refreshAccessibilityLabel}
158139
icon="arrow.clockwise"
159140
onPress={refresh}
160141
separateBackground
161142
/>
162143
) : null}
163-
{menu ? (
164-
<NativeHeaderToolbar.Menu
165-
title={menu.title}
166-
accessibilityLabel={menu.title}
167-
icon={iosIcon(menu.icon)}
168-
separateBackground={menu.separateBackground ?? true}
169-
>
170-
{menu.status ? (
171-
<NativeHeaderToolbar.Label>{menu.status}</NativeHeaderToolbar.Label>
172-
) : null}
173-
{renderMenuItems(menu.items)}
174-
</NativeHeaderToolbar.Menu>
175-
) : null}
144+
{visibleMenus.map(createNativeHeaderMenu)}
176145
{props.trailing}
177146
</NativeHeaderToolbar>
178147
) : null}

‎apps/mobile/src/components/ScreenHeader.types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface ScreenHeaderProps {
5656
readonly title: string;
5757
readonly subtitle?: string;
5858
readonly actions?: ReadonlyArray<ScreenHeaderAction>;
59-
readonly menu?: ScreenHeaderMenu;
59+
readonly menus?: ReadonlyArray<ScreenHeaderMenu>;
6060
readonly search?: ScreenHeaderSearch;
6161
readonly onBack?: () => void;
6262
readonly sidebar?: boolean;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { NativeHeaderToolbar } from "../native/StackHeader";
2+
import type { ScreenHeaderMenu, ScreenHeaderMenuItem } from "./ScreenHeader.types";
3+
4+
function renderMenuItems(items: ReadonlyArray<ScreenHeaderMenuItem>) {
5+
return items.map((item) =>
6+
"items" in item ? (
7+
<NativeHeaderToolbar.Menu
8+
key={item.id}
9+
title={item.title}
10+
icon={item.icon}
11+
inline={item.inline}
12+
>
13+
{renderMenuItems(item.items)}
14+
</NativeHeaderToolbar.Menu>
15+
) : (
16+
<NativeHeaderToolbar.MenuAction
17+
key={item.id}
18+
icon={item.icon}
19+
subtitle={item.subtitle}
20+
disabled={item.disabled}
21+
isOn={item.selected}
22+
onPress={item.onPress}
23+
>
24+
{item.title}
25+
</NativeHeaderToolbar.MenuAction>
26+
),
27+
);
28+
}
29+
30+
/** Returns direct native items for toolbar serialization, including nested menu data. */
31+
export function createNativeHeaderMenu(menu: ScreenHeaderMenu) {
32+
return (
33+
<NativeHeaderToolbar.Menu
34+
key={menu.title}
35+
title={menu.title}
36+
accessibilityLabel={menu.title}
37+
icon={typeof menu.icon === "string" ? menu.icon : menu.icon.ios}
38+
separateBackground={menu.separateBackground ?? true}
39+
>
40+
{menu.status ? <NativeHeaderToolbar.Label>{menu.status}</NativeHeaderToolbar.Label> : null}
41+
{renderMenuItems(menu.items)}
42+
</NativeHeaderToolbar.Menu>
43+
);
44+
}

‎apps/mobile/src/features/threads/ThreadGitControls.android.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export function ThreadGitControls(_props: ThreadGitControlsProps) {
2424
export function ThreadGitMenu(_props: Parameters<typeof IosThreadGitMenu>[0]) {
2525
return null;
2626
}
27+
28+
export function useThreadGitMenuDefinition(_props: Parameters<typeof IosThreadGitMenu>[0]) {
29+
return null;
30+
}

‎apps/mobile/src/features/threads/ThreadGitControls.ios.tsx‎

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createNativeHeaderMenu } from "../../components/nativeHeaderMenu.ios";
2+
import type { ScreenHeaderMenu } from "../../components/ScreenHeader.types";
13
import {
24
EnvironmentId,
35
type GitRunStackedActionResult,
@@ -500,43 +502,51 @@ export function ThreadGitControls(props: ThreadGitControlsProps) {
500502
* chat header and the review screen's toolbar.
501503
*/
502504
export function ThreadGitMenu(props: ThreadGitMenuProps) {
503-
const model = useThreadGitControlModel(props);
505+
const menu = useThreadGitMenuDefinition(props);
506+
return menu ? createNativeHeaderMenu(menu) : null;
507+
}
504508

505-
return (
506-
<NativeHeaderToolbar.Menu icon="point.topleft.down.curvedto.point.bottomright.up">
507-
<NativeHeaderToolbar.MenuAction
508-
icon="point.topleft.down.curvedto.point.bottomright.up"
509-
disabled
510-
onPress={() => {}}
511-
subtitle={compactMenuStatus(props.gitStatus)}
512-
>
513-
<NativeHeaderToolbar.Label>
514-
{compactMenuBranchLabel(model.currentBranchLabel)}
515-
</NativeHeaderToolbar.Label>
516-
</NativeHeaderToolbar.MenuAction>
517-
<NativeHeaderToolbar.MenuAction
518-
icon={model.quickActionIcon}
519-
disabled={model.quickAction.disabled}
520-
onPress={() => void model.runQuickAction()}
521-
subtitle={model.quickActionHint ?? undefined}
522-
>
523-
<NativeHeaderToolbar.Label>{model.quickAction.label}</NativeHeaderToolbar.Label>
524-
</NativeHeaderToolbar.MenuAction>
525-
<NativeHeaderToolbar.MenuAction
526-
icon="text.bubble"
527-
disabled={!model.isRepo}
528-
onPress={model.openReview}
529-
subtitle="Turn diffs and worktree changes"
530-
>
531-
<NativeHeaderToolbar.Label>Review changes</NativeHeaderToolbar.Label>
532-
</NativeHeaderToolbar.MenuAction>
533-
<NativeHeaderToolbar.MenuAction
534-
icon="ellipsis"
535-
onPress={model.openGitInspector}
536-
subtitle="Commit, files, branches"
537-
>
538-
<NativeHeaderToolbar.Label>More</NativeHeaderToolbar.Label>
539-
</NativeHeaderToolbar.MenuAction>
540-
</NativeHeaderToolbar.Menu>
541-
);
509+
/** Returns menu data because native toolbars serialize direct items rather than rendering component children. */
510+
export function useThreadGitMenuDefinition(props: ThreadGitMenuProps): ScreenHeaderMenu | null {
511+
const model = useThreadGitControlModel(props);
512+
return {
513+
title: "Git controls",
514+
icon: "point.topleft.down.curvedto.point.bottomright.up",
515+
separateBackground: false,
516+
items: [
517+
{
518+
id: "git-status",
519+
title: compactMenuBranchLabel(model.currentBranchLabel),
520+
icon: "point.topleft.down.curvedto.point.bottomright.up",
521+
disabled: true,
522+
subtitle: compactMenuStatus(props.gitStatus),
523+
onPress: () => {},
524+
},
525+
{
526+
id: "git-quick-action",
527+
title: model.quickAction.label,
528+
icon: model.quickActionIcon,
529+
disabled: model.quickAction.disabled,
530+
subtitle: model.quickActionHint ?? undefined,
531+
onPress: () => {
532+
void model.runQuickAction();
533+
},
534+
},
535+
{
536+
id: "git-review",
537+
title: "Review changes",
538+
icon: "text.bubble",
539+
disabled: !model.isRepo,
540+
subtitle: "Turn diffs and worktree changes",
541+
onPress: model.openReview,
542+
},
543+
{
544+
id: "git-more",
545+
title: "More",
546+
icon: "ellipsis",
547+
subtitle: "Commit, files, branches",
548+
onPress: model.openGitInspector,
549+
},
550+
],
551+
};
542552
}

‎apps/mobile/src/features/threads/ThreadGitControls.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {
22
ThreadGitControls,
33
ThreadGitMenu,
4+
useThreadGitMenuDefinition,
45
useThreadGitRightHeaderItems,
56
useThreadGitCenterHeaderItems,
67
type ThreadGitMenuProps,

0 commit comments

Comments
 (0)