|
| 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 | +} |
0 commit comments