Skip to content

Commit

Permalink
Add error handling to GalleryImg and Media images
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscheddar committed Oct 25, 2023
1 parent ae7b9c9 commit 5647e77
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/features/gallery/GalleryImg.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { FocusEvent, KeyboardEvent, useContext, useRef } from "react";
import React, {
FocusEvent,
KeyboardEvent,
ReactEventHandler,
useContext,
useRef,
} from "react";
import "photoswipe/dist/photoswipe.css";
import { PostView } from "lemmy-js-client";
import { GalleryContext } from "./GalleryProvider";
Expand All @@ -10,6 +16,7 @@ export interface GalleryImgProps {
className?: string;
post?: PostView;
animationType?: PreparedPhotoSwipeOptions["showHideAnimationType"];
onError?: ReactEventHandler<HTMLImageElement> | undefined;
}

/**
Expand All @@ -28,6 +35,7 @@ export function GalleryImg({
className,
post,
animationType,
onError,
}: GalleryImgProps) {
const loaded = useRef(false);
const imgRef = useRef<HTMLImageElement>(null);
Expand All @@ -53,6 +61,7 @@ export function GalleryImg({

loaded.current = true;
}}
onError={onError}
/>
);
}
18 changes: 16 additions & 2 deletions src/features/shared/Media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ const Media = ({ post, detail = false, blur = true, onError }: MediaProps) => {
const postWithUrl = { ...post, post: { ...post.post, url: url } };

if (postUrl && isUrlImage(postUrl)) {
return <Image blur={blur} post={postWithUrl} animationType="zoom" />;
return (
<Image
blur={blur}
post={postWithUrl}
animationType="zoom"
onError={handleMediaError}
/>
);
}

if (isVideo) {
Expand Down Expand Up @@ -100,7 +107,14 @@ const Media = ({ post, detail = false, blur = true, onError }: MediaProps) => {
}

if (markdownLoneImage)
return <Image blur={blur} post={postWithUrl} animationType="zoom" />;
return (
<Image
blur={blur}
post={postWithUrl}
animationType="zoom"
onError={handleMediaError}
/>
);
};

export default Media;

0 comments on commit 5647e77

Please sign in to comment.