Skip to content

Commit

Permalink
fix: Link preview should not enlarge small images (#5789)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Mello <diegolmello@gmail.com>
  • Loading branch information
JASIM0021 and diegolmello committed Jul 23, 2024
1 parent 0d1a911 commit 3fe5e6c
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions app/containers/message/Urls.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import React, { useContext, useEffect, useState } from 'react';
import { Image, StyleSheet, Text, unstable_batchedUpdates, View } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
import FastImage from 'react-native-fast-image';
import { dequal } from 'dequal';
Expand Down Expand Up @@ -94,11 +94,27 @@ type TImageLoadedState = 'loading' | 'done' | 'error';
const Url = React.memo(
({ url, index, theme }: { url: IUrl; index: number; theme: TSupportedThemes }) => {
const [imageLoadedState, setImageLoadedState] = useState<TImageLoadedState>('loading');
const [imageDimensions, setImageDimensions] = useState({ width: 0, height: 0 });
const { baseUrl, user } = useContext(MessageContext);

if (!url || url?.ignoreParse || imageLoadedState === 'error') {
return null;
}
let image = url.image || url.url;
image = image.includes('http') ? image : `${baseUrl}/${image}?rc_uid=${user.id}&rc_token=${user.token}`;

useEffect(() => {
if (image) {
Image.getSize(
image,
(width, height) => {
unstable_batchedUpdates(() => {
setImageDimensions({ width, height });
setImageLoadedState('done');
});
},
() => {
setImageLoadedState('error');
}
);
}
}, [image]);

const onPress = () => openLink(url.url, theme);

Expand All @@ -109,9 +125,8 @@ const Url = React.memo(

const hasContent = url.title || url.description;

let image = url.image || url.url;
if (image) {
image = image.includes('http') ? image : `${baseUrl}/${image}?rc_uid=${user.id}&rc_token=${user.token}`;
if (!url || url?.ignoreParse || imageLoadedState === 'error' || !imageDimensions.width || !imageDimensions.height) {
return null;
}

return (
Expand All @@ -128,14 +143,17 @@ const Url = React.memo(
},
imageLoadedState === 'loading' && styles.loading
]}
background={Touchable.Ripple(themes[theme].surfaceNeutral)}
>
background={Touchable.Ripple(themes[theme].surfaceNeutral)}>
<>
{image ? (
<FastImage
source={{ uri: image }}
style={[styles.image, !hasContent && styles.imageWithoutContent, imageLoadedState === 'loading' && styles.loading]}
resizeMode={FastImage.resizeMode.cover}
style={[
{ width: imageDimensions.width, height: imageDimensions.height },
!hasContent && styles.imageWithoutContent,
imageLoadedState === 'loading' && styles.loading
]}
resizeMode={FastImage.resizeMode.contain}
onError={() => setImageLoadedState('error')}
onLoad={() => setImageLoadedState('done')}
/>
Expand Down

0 comments on commit 3fe5e6c

Please sign in to comment.