Skip to content

Commit

Permalink
Fallback to thumbnails if the base image fails to load
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscheddar committed Oct 25, 2023
1 parent e6a288c commit ae7b9c9
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/features/shared/Media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ interface MediaProps {
const Media = ({ post, detail = false, blur = true, onError }: MediaProps) => {
const postUrl = transformUrl(post.post.url || "");
const embedVideoUrl = transformUrl(post.post.embed_video_url || "");
const thumbnailUrl = transformUrl(post.post.thumbnail_url || "");
const [url, setUrl] = useState(embedVideoUrl || postUrl);
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const isImage = isUrlImage(url as string);
const isVideo = isUrlVideo(url as string);
const isVideoEmbed = isUrlVideoEmbed(url as string);

Expand All @@ -51,15 +53,24 @@ const Media = ({ post, detail = false, blur = true, onError }: MediaProps) => {
if (isVideo && url === embedVideoUrl) {
return setUrl(postUrl);
}

// Cycle the image url before throwing an error
if (isImage && url === postUrl) {
return setUrl(thumbnailUrl);
}

onError();
};

if (!url) {
return;
}

// Change the url as we need
const postWithUrl = { ...post, post: { ...post.post, url: url } };

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

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

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

export default Media;

0 comments on commit ae7b9c9

Please sign in to comment.