Skip to content

Commit e92f72f

Browse files
committed
fix(mobile): stabilize image refresh frames
1 parent 868f811 commit e92f72f

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
type ColorValue,
4141
useWindowDimensions,
4242
View,
43+
type ViewStyle,
4344
} from "react-native";
4445
import { TouchableOpacity } from "react-native-gesture-handler";
4546
import ImageViewing from "react-native-image-viewing";
@@ -208,6 +209,8 @@ function ThreadMarkdownImageView(props: {
208209
const [availableWidth, setAvailableWidth] = useState(0);
209210
const [sourceSize, setSourceSize] = useState<{ width: number; height: number } | null>(null);
210211
const [failedUri, setFailedUri] = useState<string | null>(null);
212+
const activeUriRef = useRef(props.uri);
213+
activeUriRef.current = props.uri;
211214

212215
useEffect(() => {
213216
setSourceSize(null);
@@ -226,8 +229,9 @@ function ThreadMarkdownImageView(props: {
226229
availableWidth,
227230
});
228231
const failed = props.unavailable || (props.uri !== null && failedUri === props.uri);
229-
const placeholderWidth =
232+
const placeholderWidth: ViewStyle["width"] =
230233
availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";
234+
const frameStyle: ViewStyle = displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 };
231235

232236
return (
233237
<View
@@ -237,8 +241,7 @@ function ThreadMarkdownImageView(props: {
237241
{props.uri === null || failed ? (
238242
<View
239243
style={{
240-
width: placeholderWidth,
241-
aspectRatio: 16 / 9,
244+
...frameStyle,
242245
borderRadius: 10,
243246
backgroundColor: codeBackground,
244247
alignItems: "center",
@@ -261,7 +264,7 @@ function ThreadMarkdownImageView(props: {
261264
>
262265
<View
263266
style={{
264-
...(displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 }),
267+
...frameStyle,
265268
borderRadius: 10,
266269
backgroundColor: codeBackground,
267270
alignItems: "center",
@@ -274,6 +277,7 @@ function ThreadMarkdownImageView(props: {
274277
resizeMode="contain"
275278
accessible={false}
276279
onLoad={(event) => {
280+
if (activeUriRef.current !== props.uri) return;
277281
const { width, height } = event.nativeEvent.source;
278282
setSourceSize({ width, height });
279283
}}

0 commit comments

Comments
 (0)