Rocket.Chat.ReactNative/app/containers/message/Image.tsx

203 lines
5.7 KiB
TypeScript
Raw Normal View History

import React, { useContext, useLayoutEffect, useRef, useState } from 'react';
2022-03-21 20:44:06 +00:00
import { StyleProp, TextStyle, View } from 'react-native';
import FastImage from 'react-native-fast-image';
import { dequal } from 'dequal';
import { BlurView } from '@react-native-community/blur';
import Touchable from './Touchable';
import Markdown from '../markdown';
import styles from './styles';
import MessageContext from './Context';
import { TGetCustomEmoji } from '../../definitions/IEmoji';
import { IAttachment, IUserMessage } from '../../definitions';
import { useTheme } from '../../theme';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
import { cancelDownload, downloadMediaFile, isDownloadActive, getMediaCache } from '../../lib/methods/handleMediaDownload';
import { fetchAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
import RCActivityIndicator from '../ActivityIndicator';
import { CustomIcon } from '../CustomIcon';
interface IMessageButton {
children: React.ReactElement;
2022-03-21 20:44:06 +00:00
disabled?: boolean;
onPress: () => void;
}
interface IMessageImage {
2022-03-21 20:44:06 +00:00
file: IAttachment;
imageUrl?: string;
showAttachment?: (file: IAttachment) => void;
2022-03-21 20:44:06 +00:00
style?: StyleProp<TextStyle>[];
isReply?: boolean;
getCustomEmoji?: TGetCustomEmoji;
author?: IUserMessage;
}
const Button = React.memo(({ children, onPress, disabled }: IMessageButton) => {
const { colors } = useTheme();
return (
<Touchable
disabled={disabled}
onPress={onPress}
style={styles.imageContainer}
background={Touchable.Ripple(colors.bannerBackground)}
>
{children}
</Touchable>
);
});
const BlurComponent = ({ loading = false }: { loading: boolean }) => {
const { theme, colors } = useTheme();
return (
<>
<BlurView
style={[styles.image, styles.imageBlur]}
blurType={theme === 'light' ? 'light' : 'dark'}
blurAmount={10}
reducedTransparencyFallbackColor='white'
/>
<View style={[styles.image, styles.imageIndicator]}>
{loading ? <RCActivityIndicator /> : <CustomIcon color={colors.buttonText} name='arrow-down-circle' size={54} />}
</View>
</>
);
};
export const MessageImage = React.memo(({ imgUri, cached, loading }: { imgUri: string; cached: boolean; loading: boolean }) => {
const { colors } = useTheme();
return (
<>
<FastImage
style={[styles.image, { borderColor: colors.borderColor }]}
source={{ uri: encodeURI(imgUri) }}
resizeMode={FastImage.resizeMode.cover}
/>
{!cached ? <BlurComponent loading={loading} /> : null}
</>
);
});
const ImageContainer = React.memo(
({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply, author }: IMessageImage) => {
const [imageCached, setImageCached] = useState(file);
const [cached, setCached] = useState(false);
const [loading, setLoading] = useState(true);
const { theme } = useTheme();
const { baseUrl, user } = useContext(MessageContext);
const filePath = useRef('');
const getUrl = (link?: string) => imageUrl || formatAttachmentUrl(link, user.id, user.token, baseUrl);
const img = getUrl(file.image_url);
// The param file.title_link is the one that point to image with best quality, however we still need to test the imageUrl
// And we cannot be certain whether the file.title_link actually exists.
const imgUrlToCache = getUrl(imageCached.title_link || imageCached.image_url);
useLayoutEffect(() => {
const handleCache = async () => {
if (img) {
const cachedImageResult = await getMediaCache({
type: 'image',
mimeType: imageCached.image_type,
urlToCache: imgUrlToCache
});
filePath.current = cachedImageResult.filePath;
if (cachedImageResult.file?.exists) {
setImageCached(prev => ({
2023-05-19 05:42:21 +00:00
...prev,
title_link: cachedImageResult.file?.uri
2023-05-19 05:42:21 +00:00
}));
setLoading(false);
setCached(true);
return;
}
if (isReply) {
setLoading(false);
return;
}
if (isDownloadActive(imgUrlToCache)) {
return;
2023-05-19 05:42:21 +00:00
}
await handleAutoDownload();
}
};
handleCache();
}, []);
if (!img) {
return null;
}
const handleAutoDownload = async () => {
const isCurrentUserAuthor = author?._id === user.id;
2023-06-07 21:15:37 +00:00
const isAutoDownloadEnabled = fetchAutoDownloadEnabled('imagesPreferenceDownload');
if (isAutoDownloadEnabled || isCurrentUserAuthor) {
await handleDownload();
}
};
const handleDownload = async () => {
2023-05-19 14:35:36 +00:00
try {
const imageUri = await downloadMediaFile({
downloadUrl: imgUrlToCache,
2023-05-19 14:35:36 +00:00
path: filePath.current
});
setImageCached(prev => ({
2023-05-19 14:35:36 +00:00
...prev,
title_link: imageUri
}));
setCached(true);
2023-05-19 14:35:36 +00:00
setLoading(false);
} catch (e) {
setLoading(false);
setCached(false);
}
};
2022-03-21 20:44:06 +00:00
const onPress = () => {
if (loading && isDownloadActive(imgUrlToCache)) {
cancelDownload(imgUrlToCache);
2023-05-19 05:42:21 +00:00
setLoading(false);
setCached(false);
return;
}
if (!cached && !loading) {
handleDownload();
return;
}
2022-03-21 20:44:06 +00:00
if (!showAttachment) {
return;
}
showAttachment(imageCached);
2022-03-21 20:44:06 +00:00
};
if (imageCached.description) {
return (
<Button disabled={isReply} onPress={onPress}>
<View>
<Markdown
msg={imageCached.description}
2022-03-21 20:44:06 +00:00
style={[isReply && style]}
username={user.username}
getCustomEmoji={getCustomEmoji}
theme={theme}
/>
<MessageImage imgUri={img} cached={cached} loading={loading} />
</View>
</Button>
);
}
return (
<Button disabled={isReply} onPress={onPress}>
<MessageImage imgUri={img} cached={cached} loading={loading} />
</Button>
);
},
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file)
);
ImageContainer.displayName = 'MessageImageContainer';
MessageImage.displayName = 'MessageImage';
export default ImageContainer;