From 5dfb3eb35141c461947facac4e4d356b66a03294 Mon Sep 17 00:00:00 2001 From: thomasgross Date: Tue, 12 Nov 2024 23:45:18 +0100 Subject: [PATCH] fix: improve loading state handling and image rendering in WorkCard image --- components/shared/workCard/WorkCard.tsx | 14 ++-- components/shared/workCard/WorkCardImage.tsx | 86 ++++++++++---------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/components/shared/workCard/WorkCard.tsx b/components/shared/workCard/WorkCard.tsx index b0f19044..ab494d05 100644 --- a/components/shared/workCard/WorkCard.tsx +++ b/components/shared/workCard/WorkCard.tsx @@ -18,7 +18,7 @@ type WorkCardProps = { } const WorkCard = ({ work }: WorkCardProps) => { - const { data: dataCovers } = useGetCoverCollection({ + const { data: dataCovers, isLoading: isLoadingCovers } = useGetCoverCollection({ type: "pid", identifiers: [getAllWorkPids(work).join(", ")], sizes: [ @@ -67,11 +67,13 @@ const WorkCard = ({ work }: WorkCardProps) => { )}
- + {!isLoadingCovers && ( + + )}
diff --git a/components/shared/workCard/WorkCardImage.tsx b/components/shared/workCard/WorkCardImage.tsx index 833c2ddb..be663e7b 100644 --- a/components/shared/workCard/WorkCardImage.tsx +++ b/components/shared/workCard/WorkCardImage.tsx @@ -36,7 +36,7 @@ export const WorkCardImage: FC = ({ src, lowResSrc, alt }) => { return (
- {!imageError && ( + {!imageError && src ? ( = ({ src, lowResSrc, alt }) => { tiltReverse={true} className={"relative m-auto"} style={{ paddingTop, width: `min(100%,${imageWidthByContainerHeight}px)` }}> - {alt} { - // get the intrinsic dimensions of the image - const { naturalWidth, naturalHeight } = target as HTMLImageElement - setImageHeight(naturalHeight) - setImageWidth(naturalWidth) - }} - /> - {alt} { - setImageLoaded(true) - }} - onError={() => { - setImageError(true) - }} - /> + {lowResSrc && ( + {alt} { + // get the intrinsic dimensions of the image + const { naturalWidth, naturalHeight } = target as HTMLImageElement + setImageHeight(naturalHeight) + setImageWidth(naturalWidth) + }} + /> + )} + {src && ( + {alt} { + setImageLoaded(true) + }} + onError={() => { + setImageError(true) + }} + /> + )} - )} - - {imageError && ( + ) : (