chore: add image loading error handling (#532)

This commit is contained in:
BiggerRain
2024-04-16 14:02:02 +08:00
committed by GitHub
parent afd8feaa33
commit 405b6a693c
4 changed files with 9 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@@ -1,5 +1,5 @@
import { Link } from "react-router-dom";
import { cn } from "@renderer/lib/utils";
import { cn, imgErrorToDefalut } from "@renderer/lib/utils";
export const StoryCard = (props: { story: StoryType; className?: string }) => {
const { story, className } = props;
@@ -16,6 +16,7 @@ export const StoryCard = (props: { story: StoryType; className?: string }) => {
<img
crossOrigin="anonymous"
src={story.metadata.image}
onError={imgErrorToDefalut}
className="w-full h-full object-cover hover:scale-105"
/>
</div>

View File

@@ -137,3 +137,10 @@ export function renderPitchContour(options: {
},
});
}
export function imgErrorToDefalut(e: React.SyntheticEvent<HTMLImageElement, Event>) {
const target = e.target as HTMLImageElement;
target.onerror = null;
target.src = "assets/default-img.jpg";
}