From 9b076032cc534dd85960f83fca072c72cf64b9a0 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 1 Nov 2024 01:36:01 +0000 Subject: [PATCH] Use explicit values when useImageAspectRatio doesn't know It's not very good that you can't distingiush when we haven't loaded vs when we're certain. This shifts the burden of dealing with missing values to the caller. --- src/view/com/util/images/AutoSizedImage.tsx | 23 ++++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/view/com/util/images/AutoSizedImage.tsx b/src/view/com/util/images/AutoSizedImage.tsx index fe101fae6b..4453304e4f 100644 --- a/src/view/com/util/images/AutoSizedImage.tsx +++ b/src/view/com/util/images/AutoSizedImage.tsx @@ -21,14 +21,19 @@ export function useImageAspectRatio({ src: string dimensions: Dimensions | undefined }) { - const [raw, setAspectRatio] = React.useState( - dimensions ? calc(dimensions) : 1, + const [raw, setAspectRatio] = React.useState( + dimensions ? calc(dimensions) : undefined, ) - const ratio = 1 / 2 // max of 1:2 ratio in feeds - const constrained = Math.max(raw, ratio) - const max = Math.max(raw, 0.25) // max of 1:4 in thread - const isCropped = raw < constrained + let constrained: number | undefined + let max: number | undefined + let isCropped: boolean | undefined + if (raw !== undefined) { + const ratio = 1 / 2 // max of 1:2 ratio in feeds + constrained = Math.max(raw, ratio) + max = Math.max(raw, 0.25) // max of 1:4 in thread + isCropped = raw < constrained + } React.useEffect(() => { let aborted = false @@ -215,14 +220,16 @@ export function AutoSizedImage({ a.rounded_md, a.overflow_hidden, t.atoms.bg_contrast_25, - {aspectRatio: max}, + {aspectRatio: max ?? 1}, ]}> {contents} ) } else { return ( - +