* Render SVG project icons on native and fall back for ICO React Native's <Image> silently decodes neither SVG nor ICO, so any project whose discovered icon was one of those formats showed a blank slot on iOS/Android while web/Electron rendered it fine. Route SVG data URIs through react-native-svg on native and let ICO fall through to the initial-letter avatar instead of rendering nothing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * Import Buffer explicitly so SVG icons actually decode on device React Native has no global Buffer and the app installs no global polyfill, so projectIconSvgXml threw ReferenceError inside its try/catch and returned null on every SVG — the exact blank-slot bug the previous commit meant to fix. Vitest runs in Node where Buffer is global, which is why the tests passed anyway; the new regression test deletes globalThis.Buffer to keep the import honest. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Prefer renderable icon formats and unwrap PNG-bearing ICOs favicon.ico ranked first in ICON_PATTERNS, so any repo with a standard public/ layout served ICO — which native <Image> cannot decode — even when an SVG or PNG sat in the same directory. Rank SVG and PNG above ICO for each name, sniff magic bytes instead of trusting the extension (renamed PNGs shipped as image/x-icon are common), and extract the largest PNG-encoded frame from real ICO containers so native clients get a decodable image instead of falling back to the letter avatar. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Render SVG icons with CSS support and fall back on parse failure SvgXml ignores <style> blocks, which favicon SVGs commonly use, leaving paths with a default black fill — invisible on dark themes. SvgCss inlines the styles before rendering. Pass the letter avatar as the fallback so a malformed SVG degrades to it instead of an empty slot, and fold the workspace setup dialog's hand-rolled icon cascade into ProjectIconView so the format handling lives in one place. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Take the platform as an explicit parameter in icon source helpers The tests previously pinned isNative to true with a module-level vi.mock, which coupled them to import initialization and left the web pass-through paths uncovered. Defaulting an explicit `native` parameter to the real constant keeps call sites unchanged while letting tests exercise both variants directly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(app): isolate native project icon rendering --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { Buffer } from "buffer";
|
|
import { type ReactElement, useMemo } from "react";
|
|
import { Image, type ImageStyle, type StyleProp, View } from "react-native";
|
|
import { SvgCss } from "react-native-svg/css";
|
|
|
|
const SVG_MIME_TYPE = "image/svg+xml";
|
|
const ICO_MIME_TYPES = new Set(["image/x-icon", "image/vnd.microsoft.icon"]);
|
|
const SVG_CONTAINER = { overflow: "hidden" } as const;
|
|
|
|
export interface ProjectIconImageProps {
|
|
dataUri: string;
|
|
fallback: ReactElement;
|
|
style: StyleProp<ImageStyle>;
|
|
}
|
|
|
|
function parseDataUri(dataUri: string): { mimeType: string; payload: string } | null {
|
|
const match = /^data:([^;,]+);base64,(.*)$/s.exec(dataUri);
|
|
if (!match) return null;
|
|
|
|
const [, mimeType, payload] = match;
|
|
return mimeType && payload ? { mimeType: mimeType.toLowerCase(), payload } : null;
|
|
}
|
|
|
|
function ignoreSvgParseError() {
|
|
// Invalid repository SVGs use the same fallback as unsupported icon formats.
|
|
}
|
|
|
|
export function ProjectIconImage({ dataUri, fallback, style }: ProjectIconImageProps) {
|
|
const source = useMemo(() => ({ uri: dataUri }), [dataUri]);
|
|
const parts = useMemo(() => parseDataUri(dataUri), [dataUri]);
|
|
const svgXml = useMemo(
|
|
() =>
|
|
parts?.mimeType === SVG_MIME_TYPE
|
|
? Buffer.from(parts.payload, "base64").toString("utf8")
|
|
: null,
|
|
[parts],
|
|
);
|
|
const svgContainerStyle = useMemo(() => [style, SVG_CONTAINER], [style]);
|
|
|
|
if (svgXml) {
|
|
return (
|
|
<View style={svgContainerStyle}>
|
|
<SvgCss
|
|
xml={svgXml}
|
|
width="100%"
|
|
height="100%"
|
|
fallback={fallback}
|
|
onError={ignoreSvgParseError}
|
|
/>
|
|
</View>
|
|
);
|
|
}
|
|
if (parts && ICO_MIME_TYPES.has(parts.mimeType)) return fallback;
|
|
return <Image source={source} style={style} />;
|
|
}
|