Skip to content

Commit 3532447

Browse files
fix: image preview crashing on large pdfs (#6763)
* Fix image preview crashing on large pdfs * Update snapshots * Update app/containers/message/Urls.tsx Co-authored-by: Noach Magedman <[email protected]> --------- Co-authored-by: Noach Magedman <[email protected]>
1 parent 2d51ccf commit 3532447

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

app/containers/message/Urls.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { type ReactElement, useContext, useEffect, useLayoutEffect, useState } from 'react';
1+
import React, { type ReactElement, useCallback, useContext, useEffect, useLayoutEffect, useState } from 'react';
22
import { StyleSheet, Text, View, type ViewStyle } from 'react-native';
33
import Clipboard from '@react-native-clipboard/clipboard';
44
import { Image } from 'expo-image';
@@ -123,33 +123,33 @@ const Url = ({ url }: { url: IUrl }) => {
123123
const { colors, theme } = useTheme();
124124
const { baseUrl, user } = useContext(MessageContext);
125125
const API_Embed = useAppSelector(state => state.settings.API_Embed);
126-
const [imageUrl, setImageUrl] = useState(url.image);
126+
const [imageUrl, setImageUrl] = useState<string | null>(null);
127+
128+
const getImageUrl = useCallback(() => {
129+
const _imageUrl = url.image || url.url;
130+
131+
if (!_imageUrl) return null;
132+
if (_imageUrl.startsWith('http')) return _imageUrl;
133+
return `${baseUrl}/${_imageUrl}?rc_uid=${user.id}&rc_token=${user.token}`;
134+
}, [url.image, url.url, baseUrl, user.id, user.token]);
127135

128136
useEffect(() => {
129137
const verifyUrlIsImage = async () => {
130138
try {
131-
const imageUrl = getImageUrl();
132-
if (!imageUrl || !API_Embed) return;
139+
const _imageUrl = getImageUrl();
140+
if (!_imageUrl || !API_Embed) return;
133141

134-
const response = await axios.head(imageUrl);
142+
const response = await axios.head(_imageUrl);
135143
const contentType = response.headers['content-type'];
136144
if (contentType?.startsWith?.('image/')) {
137-
setImageUrl(imageUrl);
145+
setImageUrl(_imageUrl);
138146
}
139147
} catch {
140148
// do nothing
141149
}
142150
};
143151
verifyUrlIsImage();
144-
}, [url.image, url.url, API_Embed]);
145-
146-
const getImageUrl = () => {
147-
const _imageUrl = url.image || url.url;
148-
149-
if (!_imageUrl) return null;
150-
if (_imageUrl.includes('http')) return _imageUrl;
151-
return `${baseUrl}/${_imageUrl}?rc_uid=${user.id}&rc_token=${user.token}`;
152-
};
152+
}, [url.image, url.url, API_Embed, getImageUrl]);
153153

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

app/containers/message/__snapshots__/Message.test.tsx.snap

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103864,14 +103864,6 @@ exports[`Story Snapshots: URL should match snapshot 1`] = `
103864103864
}
103865103865
}
103866103866
>
103867-
<View
103868-
style={
103869-
{
103870-
"flex": 1,
103871-
"height": 150,
103872-
}
103873-
}
103874-
/>
103875103867
<View
103876103868
style={
103877103869
{
@@ -106432,14 +106424,6 @@ exports[`Story Snapshots: URLLargeFont should match snapshot 1`] = `
106432106424
}
106433106425
}
106434106426
>
106435-
<View
106436-
style={
106437-
{
106438-
"flex": 1,
106439-
"height": 150,
106440-
}
106441-
}
106442-
/>
106443106427
<View
106444106428
style={
106445106429
{

0 commit comments

Comments
 (0)