Skip to content
TypeScript

fix(web): fall back when pull request avatars fail (#11728)

Harshith Gokadf466c798100
diff --git a/apps/web/src/components/pullRequest/pullRequestPresentation.test.tsx b/apps/web/src/components/pullRequest/pullRequestPresentation.test.tsx
new file mode 100644
index 00000000000..df38d75152c
--- /dev/null
+++ b/apps/web/src/components/pullRequest/pullRequestPresentation.test.tsx
@@ -0,0 +1,28 @@
+import { act } from "react";
+import { create, type ReactTestRenderer } from "react-test-renderer";
+import { afterEach, expect, it, vi } from "vite-plus/test";
+
+import { PullRequestActorAvatar } from "./pullRequestPresentation";
+
+let renderer: ReactTestRenderer | undefined;
+
+afterEach(async () => {
+ await act(async () => renderer?.unmount());
+ vi.unstubAllGlobals();
+});
+
+it("falls back to the actor initial when a remote avatar fails", async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ await act(async () => {
+ renderer = create(
+ <PullRequestActorAvatar
+ actor={{ login: "octocat", name: null, avatarUrl: "https://ghe.example/octocat.png" }}
+ />,
+ );
+ });
+
+ await act(async () => renderer!.root.findByType("img").props.onError());
+
+ expect(renderer!.root.findAllByType("img")).toHaveLength(0);
+ expect(renderer!.root.findByType("span").children).toEqual(["O"]);
+});
diff --git a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx
index 12e2a42af98..65caae761ce 100644
--- a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx
+++ b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx
@@ -14,7 +14,7 @@ import {
CircleXIcon,
UserCheckIcon,
} from "lucide-react";
-import { Children, isValidElement, type ReactNode } from "react";
+import { Children, isValidElement, type ReactNode, useState } from "react";
import { cn } from "~/lib/utils";
@@ -352,8 +352,9 @@ export function PullRequestActorAvatar({
}) {
const login = actor?.login ?? "ghost";
const avatarUrl = actor?.avatarUrl ?? null;
- return avatarUrl === null ? (
- // Not every host reports an avatar, so the initial stands in where none arrives.
+ const [failedAvatarUrl, setFailedAvatarUrl] = useState<string | null>(null);
+ return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
+ // Not every host reports an avatar, and a private host may refuse the browser's request.
<span
aria-hidden
className={cn(
@@ -370,6 +371,7 @@ export function PullRequestActorAvatar({
src={avatarUrl}
loading="lazy"
className={cn("size-4 shrink-0 rounded-full bg-muted object-cover", className)}
+ onError={() => setFailedAvatarUrl(avatarUrl)}
/>
);
}