From 3a9b8640c8b1f63cdda8b0bb759d94b85bf4f462 Mon Sep 17 00:00:00 2001 From: Silberengel Date: Wed, 3 Dec 2025 13:27:24 +0100 Subject: [PATCH] apply horizontal/vertical layout to og cards, analog to the one for fallback cards --- src/components/WebPreview/index.tsx | 82 +++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/src/components/WebPreview/index.tsx b/src/components/WebPreview/index.tsx index deb9537..ecf75b9 100644 --- a/src/components/WebPreview/index.tsx +++ b/src/components/WebPreview/index.tsx @@ -349,6 +349,8 @@ export default function WebPreview({ url, className }: { url: string; className? // Detect image aspect ratio to determine layout - MUST be called unconditionally const [imageAspectRatio, setImageAspectRatio] = useState(null) const [isImageLoading, setIsImageLoading] = useState(true) + const [ogImageAspectRatio, setOgImageAspectRatio] = useState(null) + const [isOgImageLoading, setIsOgImageLoading] = useState(true) useEffect(() => { if (!displayImageForDetection) { @@ -371,6 +373,28 @@ export default function WebPreview({ url, className }: { url: string; className? img.src = displayImageForDetection }, [displayImageForDetection]) + // Detect OG image aspect ratio for OG cards + useEffect(() => { + if (!image) { + setOgImageAspectRatio(null) + setIsOgImageLoading(false) + return + } + + setIsOgImageLoading(true) + const img = new window.Image() + img.onload = () => { + const aspectRatio = img.width / img.height + setOgImageAspectRatio(aspectRatio) + setIsOgImageLoading(false) + } + img.onerror = () => { + setOgImageAspectRatio(null) + setIsOgImageLoading(false) + } + img.src = image + }, [image]) + // Early return after ALL hooks are called if (!autoLoadMedia) { return null @@ -710,7 +734,12 @@ export default function WebPreview({ url, className }: { url: string; className? ) } + // Determine OG image orientation + const isOgPortrait = ogImageAspectRatio !== null && ogImageAspectRatio < 1 + const isOgLandscape = ogImageAspectRatio !== null && ogImageAspectRatio > 1 + if (isSmallScreen && image) { + // Small screen: always use horizontal layout with image on left return (
@@ -730,7 +759,51 @@ export default function WebPreview({ url, className }: { url: string; className?
{title &&
{title}
} {!title && description &&
{description}
} -
+
+ e.stopPropagation()} + className="text-xs text-muted-foreground truncate block hover:underline" + > + {url} + +
+ + ) + } + + // Render OG card with portrait/landscape layout + if (isOgLandscape && image) { + return ( +
+
+ +
+
+ + {title &&
{title}
} + {description && ( +
+ {description} +
+ )} +
- {image && ( -
+ {image && (isOgPortrait || isOgImageLoading) && ( +
)} -
+