Files
paseo/packages/app/src/components/project-icon-view.tsx
T
b7f137c2a0 Render SVG project icons on native and fall back for ICO (#2579)
* 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>
2026-08-10 12:01:09 +02:00

74 lines
2.4 KiB
TypeScript

import { useMemo } from "react";
import { type StyleProp, Text, type TextStyle, View } from "react-native";
import { ProjectIconImage } from "@/components/project-icon-image";
import { deriveIdentityColorName, identityColor } from "@/styles/identity-colors";
const WHITE_TEXT = { color: "#ffffff" } as const;
const FALLBACK_LAYOUT = { alignItems: "center", justifyContent: "center" } as const;
/**
* Corner radius of the *generated* project icon — the colored square with an initial — as a
* fraction of the box, so it reads as the same shape at 16pt in the sidebar and 40pt in the edit
* sheet. Fixed tokens did not give that: the radius scale is coarse at the bottom, so small icons
* landed on 2pt and looked square while large ones were visibly rounder.
*
* This is ours to shape. A **user-uploaded icon is never rounded** — it is someone's mark, and
* clipping its corners distorts branding we don't own. A square logo stays square, a round one is
* already round.
*/
const RADIUS_RATIO = 0.25;
export function projectIconRadius(size: number): number {
return Math.round(size * RADIUS_RATIO);
}
/**
* A project's icon: its chosen image, or a colored square carrying its initial.
*
* Geometry lives here, not at the call site. It used to be five copies of the same
* width/height/radius/centering block, which is how the radius drifted apart in the first
* place — pass a `size` and the shape follows.
*/
export function ProjectIconView({
iconDataUri,
initial,
projectViewKey,
size,
textStyle,
}: {
iconDataUri: string | null;
initial: string;
projectViewKey: string;
size: number;
textStyle: StyleProp<TextStyle>;
}) {
// The uploaded image is sized but never clipped — see projectIconRadius.
const box = useMemo(() => ({ width: size, height: size }), [size]);
const fallbackStyles = useMemo(
() => [
box,
{ borderRadius: projectIconRadius(size) },
FALLBACK_LAYOUT,
{ backgroundColor: identityColor(deriveIdentityColorName(projectViewKey)) },
],
[box, size, projectViewKey],
);
const textStyles = useMemo(() => [textStyle, WHITE_TEXT], [textStyle]);
const fallback = useMemo(
() => (
<View style={fallbackStyles}>
<Text style={textStyles}>{initial}</Text>
</View>
),
[fallbackStyles, initial, textStyles],
);
return iconDataUri ? (
<ProjectIconImage dataUri={iconDataUri} fallback={fallback} style={box} />
) : (
fallback
);
}