diff --git a/.changeset/repo-icon-fallback.md b/.changeset/repo-icon-fallback.md new file mode 100644 index 000000000..2279ae42e --- /dev/null +++ b/.changeset/repo-icon-fallback.md @@ -0,0 +1,5 @@ +--- +"helmor": patch +--- + +Fix repo avatars rendering blank instead of falling back to the name initials when a repository's icon file can't be displayed (e.g. an empty or undecodable `favicon.ico`). diff --git a/src-tauri/src/workspace/helpers.rs b/src-tauri/src/workspace/helpers.rs index 63eca453e..5a726fc8a 100644 --- a/src-tauri/src/workspace/helpers.rs +++ b/src-tauri/src/workspace/helpers.rs @@ -263,6 +263,13 @@ pub fn repo_icon_src_for_root_path(root_path: Option<&str>) -> Option { let data_uri = icon_path.as_deref().and_then(|path| { let mime_type = repo_icon_mime_type(Path::new(path)); let bytes = fs::read(path).ok()?; + // A 0-byte icon file (e.g. the empty `public/favicon.ico` placeholder + // scaffolded API repos ship) would otherwise become a valid-looking but + // image-less `data:` URI — the frontend can't render it and the avatar + // ends up blank. Treat it as "no icon" so the UI falls back to initials. + if bytes.is_empty() { + return None; + } Some(format!( "data:{mime_type};base64,{}", BASE64_STANDARD.encode(bytes) @@ -1368,6 +1375,22 @@ mod tests { assert!(repo_icon_src_for_root_path(Some(&root_str)).is_none()); } + #[test] + fn repo_icon_src_returns_none_for_empty_icon_file() { + // A matched-but-empty icon file (e.g. a 0-byte `public/favicon.ico` + // placeholder) must NOT produce a blank `data:` URI — the avatar has to + // fall back to initials instead of rendering nothing. + let dir = tempfile::tempdir().unwrap(); + let root_str = dir.path().to_str().unwrap().to_string(); + + fs::create_dir_all(dir.path().join("public")).unwrap(); + fs::write(dir.path().join("public/favicon.ico"), b"").unwrap(); + + assert!(repo_icon_src_for_root_path(Some(&root_str)).is_none()); + // Second call exercises the cached path. + assert!(repo_icon_src_for_root_path(Some(&root_str)).is_none()); + } + // ---- Workspace naming tests ---- fn test_db() -> (rusqlite::Connection, tempfile::TempDir) { diff --git a/src/features/navigation/avatar.test.tsx b/src/features/navigation/avatar.test.tsx index 500c8b52b..9893c9593 100644 --- a/src/features/navigation/avatar.test.tsx +++ b/src/features/navigation/avatar.test.tsx @@ -1,8 +1,55 @@ -import { render } from "@testing-library/react"; -import { describe, expect, it } from "vitest"; +import { render, waitFor } from "@testing-library/react"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { WorkspaceAvatar } from "./avatar"; describe("WorkspaceAvatar", () => { + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("falls back to initials when the icon src fails to load", async () => { + // Radix's never mounts the underlying on error — it + // renders null — so a native `onError` never fires. jsdom also never + // loads images, leaving Radix's internal probe stuck in "loading" + // forever. Stub window.Image so setting a src dispatches an error, which + // drives our `onLoadingStatusChange` path. Without the fix this repo + // (a non-empty but undecodable `data:image/x-icon;base64,` URI) would + // render a permanently blank avatar. + class FailingImage { + #src = ""; + complete = false; + naturalWidth = 0; + #onError: (() => void) | null = null; + addEventListener(type: string, cb: () => void) { + if (type === "error") this.#onError = cb; + } + removeEventListener() {} + set src(value: string) { + this.#src = value; + queueMicrotask(() => this.#onError?.()); + } + get src() { + return this.#src; + } + } + vi.stubGlobal("Image", FailingImage); + + const { container } = render( + , + ); + + await waitFor(() => { + const fallback = container.querySelector('[data-slot="avatar-fallback"]'); + expect(fallback).toBeInTheDocument(); + expect(fallback).toHaveTextContent("RA"); + }); + }); + it("renders fallback immediately when switching from an icon repo to a repo without an icon", () => { const { container, rerender } = render( never mounts the underlying unless its own + // probe reports "loaded" — on failure it just renders `null`. A native + // therefore never fires, so we drive the fallback from + // Radix's `onLoadingStatusChange` instead. Without this, a broken or + // undecodable icon (an empty `favicon.ico`, an `.ico` WebKit can't render, + // a corrupt file) leaves the avatar permanently blank instead of showing + // the initials. + const [status, setStatus] = useState("idle"); + // Reset when the icon source changes so a new icon gets a fresh chance to + // load before we decide to fall back. useEffect(() => { - setHasImage(Boolean(src)); + setStatus("idle"); }, [src]); - const showFallback = !src || !hasImage; + + // Stay optimistic while idle/loading (no initials flash for good icons); + // only fall back once the load actually errors or there's no src at all. + const showFallback = !src || status === "error"; return ( { - setHasImage(false); - }} - onLoad={() => { - setHasImage(true); - }} + onLoadingStatusChange={(nextStatus) => setStatus(nextStatus)} /> ) : null} {showFallback ? ( diff --git a/src/features/navigation/row-item.tsx b/src/features/navigation/row-item.tsx index dcc91c0c2..3f5673190 100644 --- a/src/features/navigation/row-item.tsx +++ b/src/features/navigation/row-item.tsx @@ -470,7 +470,7 @@ export const WorkspaceRowItem = memo(