Skip to content

Commit

Permalink
fix image onerror infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ndepaola committed Mar 18, 2024
1 parent 1a907ca commit 45ed70f
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions frontend/src/features/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ function CardImage({
setImageState("loaded");
};
const onError: React.ReactEventHandler<HTMLImageElement> = (img) => {
setImageState("errored");
img.preventDefault();
img.currentTarget.onerror = null;
if (imageState !== "errored") {
setImageState("errored");
}
};
const handleShowDetailedView = () => {
if (showDetailedViewOnClick && maybeCardDocument != null) {
Expand Down Expand Up @@ -126,14 +130,8 @@ function CardImage({
imageCDNURL != null && maybeCardDocument?.source_type
? `${imageCDNURL}/images/google_drive/large/${maybeCardDocument?.identifier}.jpg`
: maybeCardDocument?.medium_thumbnail_url;
const imageSrc: string | undefined =
imageState !== "errored"
? small
? smallThumbnailURL
: mediumThumbnailURL
: small
? "/error_404.png"
: "/error_404_med.png";
const imageSrc = small ? smallThumbnailURL : mediumThumbnailURL;
const errorImageSrc = small ? "/error_404.png" : "/error_404_med.png";
const imageAlt = maybeCardDocument?.name ?? "Unnamed Card";

//# endregion
Expand All @@ -154,19 +152,32 @@ function CardImage({
fill={true}
/>
) : (
<VisibleImage
ref={image}
className="card-img card-img-fade-in"
loading="lazy"
imageIsLoading={imageState === "loading"}
showDetailedViewOnClick={showDetailedViewOnClick}
src={imageSrc}
onLoadingComplete={onLoadingComplete}
onErrorCapture={onError}
onClick={handleShowDetailedView}
alt={imageAlt}
fill={true}
/>
<>
{imageState === "errored" ? (
<VisibleImage
ref={image}
className="card-img card-img-fade-in"
loading="lazy"
src={errorImageSrc}
alt={""}
fill={true}
/>
) : (
<VisibleImage
ref={image}
className="card-img card-img-fade-in"
loading="lazy"
imageIsLoading={imageState === "loading"}
showDetailedViewOnClick={showDetailedViewOnClick}
src={imageSrc}
onLoadingComplete={onLoadingComplete}
onErrorCapture={onError}
onClick={handleShowDetailedView}
alt={imageAlt}
fill={true}
/>
)}
</>
))}
</>
);
Expand Down

0 comments on commit 45ed70f

Please sign in to comment.