Skip to content

Commit 847a43f

Browse files
committed
fix(web): fall back when pull request avatars fail
1 parent 8ef478e commit 847a43f

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { act } from "react";
2+
import { create, type ReactTestRenderer } from "react-test-renderer";
3+
import { afterEach, expect, it, vi } from "vite-plus/test";
4+
5+
import { PullRequestActorAvatar } from "./pullRequestPresentation";
6+
7+
let renderer: ReactTestRenderer | undefined;
8+
9+
afterEach(async () => {
10+
await act(async () => renderer?.unmount());
11+
vi.unstubAllGlobals();
12+
});
13+
14+
it("falls back to the actor initial when a remote avatar fails", async () => {
15+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16+
await act(async () => {
17+
renderer = create(
18+
<PullRequestActorAvatar
19+
actor={{ login: "octocat", name: null, avatarUrl: "https://ghe.example/octocat.png" }}
20+
/>,
21+
);
22+
});
23+
24+
await act(async () => renderer!.root.findByType("img").props.onError());
25+
26+
expect(renderer!.root.findAllByType("img")).toHaveLength(0);
27+
expect(renderer!.root.findByType("span").children).toEqual(["O"]);
28+
});

‎apps/web/src/components/pullRequest/pullRequestPresentation.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
TriangleAlertIcon,
2020
UserCheckIcon,
2121
} from "lucide-react";
22-
import { Children, isValidElement, type ReactNode } from "react";
22+
import { Children, isValidElement, type ReactNode, useState } from "react";
2323

2424
import { cn } from "~/lib/utils";
2525

@@ -346,8 +346,9 @@ export function PullRequestActorAvatar({
346346
}) {
347347
const login = actor?.login ?? "ghost";
348348
const avatarUrl = actor?.avatarUrl ?? null;
349-
return avatarUrl === null ? (
350-
// Not every host reports an avatar, so the initial stands in where none arrives.
349+
const [failedAvatarUrl, setFailedAvatarUrl] = useState<string | null>(null);
350+
return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
351+
// Not every host reports an avatar, and a private host may refuse the browser's request.
351352
<span
352353
aria-hidden
353354
className={cn(
@@ -364,6 +365,7 @@ export function PullRequestActorAvatar({
364365
src={avatarUrl}
365366
loading="lazy"
366367
className={cn("size-4 shrink-0 rounded-full bg-muted object-cover", className)}
368+
onError={() => setFailedAvatarUrl(avatarUrl)}
367369
/>
368370
);
369371
}

0 commit comments

Comments
 (0)