Skip to content

Commit 8580326

Browse files
feat(mobile): show project favicons in the iOS thread filter menu
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 059c7eb commit 8580326

9 files changed

Lines changed: 210 additions & 16 deletions

‎apps/mobile/src/features/home/HomeHeader.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../layout/native-mail-search-toolbar";
1111
import { buildHomeListFilterMenu } from "./home-list-filter-menu";
1212
import type { HomeHeaderProps } from "./HomeHeader.types";
13+
import { useProjectFilterMenuIcons } from "./useProjectFilterMenuIcons";
1314

1415
export type { HomeHeaderEnvironment } from "./HomeHeader.types";
1516

@@ -26,7 +27,8 @@ export function HomeHeader(props: HomeHeaderProps) {
2627
return searchBarRef.current !== null;
2728
}, []);
2829
useHardwareKeyboardCommand("focusSearch", focusSearch);
29-
const filterMenu = buildHomeListFilterMenu(props);
30+
const projects = useProjectFilterMenuIcons(props.projects);
31+
const filterMenu = buildHomeListFilterMenu({ ...props, projects });
3032

3133
return (
3234
<>
@@ -118,7 +120,7 @@ export function HomeHeader(props: HomeHeaderProps) {
118120
))}
119121
</NativeHeaderToolbar.Menu>
120122

121-
{props.projects.length > 0 ? (
123+
{projects.length > 0 ? (
122124
<NativeHeaderToolbar.Menu title="Project">
123125
<NativeHeaderToolbar.Label>Project</NativeHeaderToolbar.Label>
124126
<NativeHeaderToolbar.MenuAction
@@ -128,9 +130,10 @@ export function HomeHeader(props: HomeHeaderProps) {
128130
>
129131
<NativeHeaderToolbar.Label>All projects</NativeHeaderToolbar.Label>
130132
</NativeHeaderToolbar.MenuAction>
131-
{props.projects.map((project) => (
133+
{projects.map((project) => (
132134
<NativeHeaderToolbar.MenuAction
133135
key={project.key}
136+
icon={project.icon}
134137
isOn={props.selectedProjectKey === project.key}
135138
onPress={() => props.onProjectChange(project.key)}
136139
>

‎apps/mobile/src/features/home/home-list-filter-menu.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,25 @@ describe("buildHomeListFilterMenu", () => {
3636
expect(onProjectChange).toHaveBeenNthCalledWith(1, null);
3737
expect(onProjectChange).toHaveBeenNthCalledWith(2, "environment-1:project-2");
3838
});
39+
40+
it("keeps a project's native menu icon on its action only", () => {
41+
const icon = { type: "sfSymbol" as const, name: "folder.fill" };
42+
const menu = buildHomeListFilterMenu({
43+
environments: [],
44+
projects: [
45+
{ key: "environment-1:project-1", label: "Codething", icon },
46+
{ key: "environment-1:project-2", label: "Website" },
47+
],
48+
selectedEnvironmentId: null,
49+
selectedProjectKey: null,
50+
onEnvironmentChange: vi.fn(),
51+
onProjectChange: vi.fn(),
52+
});
53+
54+
const projectMenu = menu.items.find(
55+
(item) => item.type === "submenu" && item.title === "Project",
56+
);
57+
if (projectMenu?.type !== "submenu") throw new Error("Expected project submenu");
58+
expect(projectMenu.items.map((item) => item.icon)).toEqual([undefined, icon, undefined]);
59+
});
3960
});

‎apps/mobile/src/features/home/home-list-filter-menu.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@ export interface HomeListFilterMenuEnvironment {
66
readonly label: string;
77
}
88

9+
/**
10+
* Row image for the native iOS menu. UIMenu only takes images, so a favicon
11+
* arrives as a sized image source and everything else uses an SF Symbol.
12+
*/
13+
export type HomeListFilterMenuIcon =
14+
| { readonly type: "sfSymbol"; readonly name: string }
15+
| {
16+
readonly type: "imageSource";
17+
readonly imageSource: {
18+
readonly uri: string;
19+
readonly width: number;
20+
readonly height: number;
21+
readonly scale: number;
22+
};
23+
};
24+
925
export interface HomeListFilterMenuProject {
1026
readonly key: string;
1127
readonly label: string;
1228
/** Project whose favicon stands for the scope; Android draws it beside the label. */
1329
readonly representative?: EnvironmentProject;
30+
/** iOS counterpart of `representative`, resolved by `useProjectFilterMenuIcons`. */
31+
readonly icon?: HomeListFilterMenuIcon;
1432
}
1533

1634
type HomeListFilterMenuAction = {
1735
readonly type: "action";
1836
readonly title: string;
1937
readonly subtitle?: string;
2038
readonly state?: "on" | "off";
39+
readonly icon?: HomeListFilterMenuIcon;
2140
readonly onPress: () => void;
2241
};
2342

@@ -81,6 +100,7 @@ export function buildHomeListFilterMenu(props: {
81100
type: "action" as const,
82101
title: project.label,
83102
state: props.selectedProjectKey === project.key ? ("on" as const) : ("off" as const),
103+
...(project.icon ? { icon: project.icon } : {}),
84104
onPress: () => props.onProjectChange(project.key),
85105
})),
86106
],
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import { isProjectFaviconFallbackUrl } from "@t3tools/shared/projectFavicon";
3+
import { Atom } from "effect/unstable/reactivity";
4+
import { useEffect, useMemo, useState } from "react";
5+
import { PixelRatio } from "react-native";
6+
import { resolveProjectIconGlyph } from "../../lib/projectIcon";
7+
import { projectFaviconUrlAtom } from "../../state/assets";
8+
import type { HomeListFilterMenuIcon, HomeListFilterMenuProject } from "./home-list-filter-menu";
9+
10+
const MENU_ICON_SIZE = 20;
11+
const FOLDER_ICON: HomeListFilterMenuIcon = { type: "sfSymbol", name: "folder.fill" };
12+
13+
// UIKit builds the menu synchronously, so the favicon has to be handed over as
14+
// bytes (a data URI) instead of a URL the native image loader would fetch later.
15+
const faviconDataUris = new Map<string, string | null>();
16+
const pendingFavicons = new Map<string, Promise<void>>();
17+
18+
function loadFaviconDataUri(url: string): Promise<void> {
19+
const pending = pendingFavicons.get(url);
20+
if (pending) return pending;
21+
const request = fetch(url)
22+
.then((response) => (response.ok ? response.blob() : null))
23+
.then(
24+
(blob) =>
25+
new Promise<string | null>((resolve) => {
26+
if (!blob) return resolve(null);
27+
const reader = new FileReader();
28+
reader.addEventListener("error", () => resolve(null));
29+
reader.addEventListener("load", () =>
30+
resolve(typeof reader.result === "string" ? reader.result : null),
31+
);
32+
reader.readAsDataURL(blob);
33+
}),
34+
)
35+
.catch(() => null)
36+
.then((dataUri) => {
37+
faviconDataUris.set(url, dataUri);
38+
pendingFavicons.delete(url);
39+
});
40+
pendingFavicons.set(url, request);
41+
return request;
42+
}
43+
44+
export function useProjectFilterMenuIcons<T extends HomeListFilterMenuProject>(
45+
projects: ReadonlyArray<T>,
46+
): ReadonlyArray<T> {
47+
const faviconUrls = useAtomValue(
48+
useMemo(
49+
() =>
50+
Atom.make((get) =>
51+
projects.map((project) => {
52+
const representative = project.representative;
53+
if (
54+
!representative ||
55+
resolveProjectIconGlyph(representative.projectIcon, project.label) !== null
56+
) {
57+
return null;
58+
}
59+
const url = get(
60+
projectFaviconUrlAtom({
61+
environmentId: representative.environmentId,
62+
cwd: representative.workspaceRoot,
63+
faviconPath: representative.faviconPath,
64+
}),
65+
);
66+
return isProjectFaviconFallbackUrl(url) ? null : url;
67+
}),
68+
),
69+
[projects],
70+
),
71+
);
72+
73+
const [dataUris, setDataUris] = useState<ReadonlyMap<string, string | null>>(faviconDataUris);
74+
useEffect(() => {
75+
let active = true;
76+
for (const url of faviconUrls) {
77+
if (!url || faviconDataUris.has(url)) continue;
78+
void loadFaviconDataUri(url).then(() => {
79+
if (active) setDataUris(new Map(faviconDataUris));
80+
});
81+
}
82+
return () => {
83+
active = false;
84+
};
85+
}, [faviconUrls]);
86+
87+
return useMemo(
88+
() =>
89+
projects.map((project, index) => {
90+
const url = faviconUrls[index];
91+
const uri = url ? dataUris.get(url) : null;
92+
return {
93+
...project,
94+
icon: uri
95+
? {
96+
type: "imageSource" as const,
97+
imageSource: {
98+
uri,
99+
width: MENU_ICON_SIZE,
100+
height: MENU_ICON_SIZE,
101+
scale: PixelRatio.get(),
102+
},
103+
}
104+
: FOLDER_ICON,
105+
};
106+
}),
107+
[dataUris, faviconUrls, projects],
108+
);
109+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { HomeListFilterMenuProject } from "./home-list-filter-menu";
2+
3+
/** Android draws `representative` itself; see the `.ios.ts` sibling. */
4+
export function useProjectFilterMenuIcons<T extends HomeListFilterMenuProject>(
5+
projects: ReadonlyArray<T>,
6+
): ReadonlyArray<T> {
7+
return projects;
8+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands
4242
import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts";
4343
import { useHomeListOptions } from "../home/home-list-options";
4444
import { buildHomeListFilterMenu } from "../home/home-list-filter-menu";
45+
import { useProjectFilterMenuIcons } from "../home/useProjectFilterMenuIcons";
4546
import { buildHomeProjectScopes } from "../home/homeThreadList";
4647
import { SwipeableScrollGateProvider, useSwipeableScrollGate } from "../home/thread-swipe-actions";
4748
import { usePendingTaskListActions } from "../home/usePendingTaskListActions";
@@ -891,17 +892,18 @@ function ThreadNavigationSidebarPane(
891892
const filterIcon = filterCustomized
892893
? "line.3.horizontal.decrease.circle.fill"
893894
: "line.3.horizontal.decrease.circle";
895+
const projectFilterMenuOptions = useProjectFilterMenuIcons(projectFilterOptions);
894896
const filterMenu = useMemo(
895897
() =>
896898
buildHomeListFilterMenu({
897899
environments,
898-
projects: projectFilterOptions,
900+
projects: projectFilterMenuOptions,
899901
selectedEnvironmentId: options.selectedEnvironmentId,
900902
selectedProjectKey,
901903
onEnvironmentChange: setSelectedEnvironmentId,
902904
onProjectChange: setSelectedProjectKey,
903905
}),
904-
[environments, options, projectFilterOptions, selectedProjectKey, setSelectedEnvironmentId],
906+
[environments, options, projectFilterMenuOptions, selectedProjectKey, setSelectedEnvironmentId],
905907
);
906908
const nativeHeaderItems = useMemo(
907909
() =>

‎apps/mobile/src/native/NativeHeaderToolbar.tsx‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type ReactElement,
1414
type ReactNode,
1515
} from "react";
16-
import type { ColorValue } from "react-native";
16+
import type { ColorValue, ImageURISource } from "react-native";
1717

1818
function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null {
1919
return useNavigation<NativeStackNavigationProp<ParamListBase>>();
@@ -34,15 +34,23 @@ function labelFromChildren(children: ReactNode): string {
3434
type NativeStackHeaderIcon = NonNullable<
3535
Extract<NativeStackHeaderItem, { type: "button" }>["icon"]
3636
>;
37+
/** Menu action icon prop: an SF Symbol name, or any native icon such as a sized image source. */
38+
type NativeHeaderToolbarIconProp =
39+
| string
40+
| { readonly type: "sfSymbol"; readonly name: string }
41+
| { readonly type: "imageSource"; readonly imageSource: ImageURISource };
3742
type NativeStackOptionsWithToolbar = NativeStackNavigationOptions & {
3843
unstable_headerToolbarItems?: () => NativeStackHeaderItem[];
3944
};
4045

4146
function iconFromProp(icon: unknown): NativeStackHeaderIcon | undefined {
42-
if (typeof icon !== "string") {
43-
return undefined;
47+
if (typeof icon === "string") {
48+
return { type: "sfSymbol", name: icon as never };
4449
}
45-
return { type: "sfSymbol", name: icon as never };
50+
if (typeof icon === "object" && icon !== null && "type" in icon) {
51+
return icon as NativeStackHeaderIcon;
52+
}
53+
return undefined;
4654
}
4755

4856
type ToolbarElementProps = Record<string, unknown> & { readonly children?: ReactNode };
@@ -254,7 +262,7 @@ function NativeHeaderToolbarMenuAction(_props: {
254262
readonly destructive?: boolean;
255263
readonly disabled?: boolean;
256264
readonly discoverabilityLabel?: string;
257-
readonly icon?: string;
265+
readonly icon?: NativeHeaderToolbarIconProp;
258266
readonly isOn?: boolean;
259267
readonly onPress?: () => void;
260268
readonly subtitle?: string;

‎patches/[email protected]‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,3 +2224,26 @@ index 9c3ab4c3653ee1769addda8a6464cdf49695a601..6f242ad2bbd7152ea9f873ef7754ad47
22242224
| HeaderBarButtonItemSpacing;
22252225

22262226
/**
2227+
diff --git a/ios/helpers/image/RNSImageLoadingHelper.mm b/ios/helpers/image/RNSImageLoadingHelper.mm
2228+
--- a/ios/helpers/image/RNSImageLoadingHelper.mm
2229+
+++ b/ios/helpers/image/RNSImageLoadingHelper.mm
2230+
@@ -13,6 +13,19 @@
2231+
RCTImageSource *imageSource = [RCTConvert RCTImageSource:jsonImageSource];
2232+
RCTAssert(imageSource != nil, @"[RNScreens] Expected nonnil image source");
2233+
2234+
+ // Inline `data:` sources decode synchronously so UIKit menus, which are built
2235+
+ // before the async image loader finishes, can show them on the first open.
2236+
+ if ([imageSource.request.URL.scheme isEqualToString:@"data"]) {
2237+
+ UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageSource.request.URL]];
2238+
+ if (image != nil && imageSource.size.width > 0 && image.size.width > 0) {
2239+
+ image = [UIImage imageWithCGImage:image.CGImage
2240+
+ scale:image.scale * image.size.width / imageSource.size.width
2241+
+ orientation:image.imageOrientation];
2242+
+ }
2243+
+ imageLoadingCompletionBlock([RNSImageLoadingHelper handleRenderingModeForImage:image isTemplate:isTemplate]);
2244+
+ return;
2245+
+ }
2246+
+
2247+
#if !defined(NDEBUG) // We're in debug mode here
2248+
if (imageSource.packagerAsset) {
2249+
// We use `+ [RCTConvert UIImage:]` only in debug mode, because it is deprecated, however

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)