2023-05-15 18:10:43 +00:00
|
|
|
import React, { useContext, useLayoutEffect, useRef, useState } from 'react';
|
2022-03-21 20:44:06 +00:00
|
|
|
import { StyleProp, TextStyle, View } from 'react-native';
|
2022-05-31 16:08:18 +00:00
|
|
|
import FastImage from 'react-native-fast-image';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { dequal } from 'dequal';
|
|
|
|
import { createImageProgress } from 'react-native-image-progress';
|
|
|
|
import * as Progress from 'react-native-progress';
|
2023-05-15 18:10:43 +00:00
|
|
|
import { BlurView } from '@react-native-community/blur';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
import Touchable from './Touchable';
|
|
|
|
import Markdown from '../markdown';
|
|
|
|
import styles from './styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import MessageContext from './Context';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../../definitions/IEmoji';
|
2023-05-11 23:20:48 +00:00
|
|
|
import { IAttachment, IUserMessage } from '../../definitions';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, useTheme } from '../../theme';
|
2022-04-07 13:13:19 +00:00
|
|
|
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
|
2023-05-15 18:10:43 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
|
|
|
import RCActivityIndicator from '../ActivityIndicator';
|
|
|
|
import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload';
|
2023-05-11 23:20:48 +00:00
|
|
|
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
interface IMessageButton {
|
|
|
|
children: React.ReactElement;
|
2022-03-21 20:44:06 +00:00
|
|
|
disabled?: boolean;
|
2022-04-01 21:52:38 +00:00
|
|
|
onPress: () => void;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-04-01 21:52:38 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
interface IMessageImage {
|
2022-03-21 20:44:06 +00:00
|
|
|
file: IAttachment;
|
2021-09-13 20:41:05 +00:00
|
|
|
imageUrl?: string;
|
2022-04-01 21:52:38 +00:00
|
|
|
showAttachment?: (file: IAttachment) => void;
|
2022-03-21 20:44:06 +00:00
|
|
|
style?: StyleProp<TextStyle>[];
|
|
|
|
isReply?: boolean;
|
2022-03-29 20:06:50 +00:00
|
|
|
getCustomEmoji?: TGetCustomEmoji;
|
2023-05-11 23:20:48 +00:00
|
|
|
author?: IUserMessage;
|
2023-05-15 18:10:43 +00:00
|
|
|
messageId: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ImageProgress = createImageProgress(FastImage);
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
const Button = React.memo(({ children, onPress, disabled, theme }: IMessageButton) => (
|
2022-03-21 20:44:06 +00:00
|
|
|
<Touchable
|
|
|
|
disabled={disabled}
|
|
|
|
onPress={onPress}
|
|
|
|
style={styles.imageContainer}
|
2022-08-08 21:02:08 +00:00
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
|
|
|
>
|
2021-09-13 20:41:05 +00:00
|
|
|
{children}
|
|
|
|
</Touchable>
|
|
|
|
));
|
|
|
|
|
2023-05-15 18:10:43 +00:00
|
|
|
export const MessageImage = React.memo(
|
|
|
|
({ imgUri, toDownload, loading }: { imgUri: string; toDownload: boolean; loading: boolean }) => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ImageProgress
|
|
|
|
style={[styles.image, { borderColor: colors.borderColor }]}
|
|
|
|
source={{ uri: encodeURI(imgUri) }}
|
|
|
|
resizeMode={FastImage.resizeMode.cover}
|
|
|
|
indicator={Progress.Pie}
|
|
|
|
indicatorProps={{
|
|
|
|
color: colors.actionTintColor
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{toDownload ? <BlurComponent loading={loading} /> : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const BlurComponent = ({ loading = false }: { loading: boolean }) => {
|
|
|
|
const { theme, colors } = useTheme();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<BlurView
|
|
|
|
style={[styles.image, { position: 'absolute', borderWidth: 0, top: 0, left: 0, bottom: 0, right: 0 }]}
|
|
|
|
blurType={theme === 'light' ? 'light' : 'dark'}
|
|
|
|
blurAmount={10}
|
|
|
|
reducedTransparencyFallbackColor='white'
|
|
|
|
/>
|
|
|
|
<View style={[styles.image, { position: 'absolute', justifyContent: 'center', alignItems: 'center', borderWidth: 0 }]}>
|
|
|
|
{loading ? <RCActivityIndicator /> : <CustomIcon color={colors.buttonText} name='arrow-down-circle' size={54} />}
|
|
|
|
</View>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const ImageContainer = React.memo(
|
2023-05-15 18:10:43 +00:00
|
|
|
({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply, author, messageId }: IMessageImage) => {
|
|
|
|
const [toDownload, setToDownload] = useState(true);
|
|
|
|
const [loading, setLoading] = useState(false);
|
2022-03-29 20:06:50 +00:00
|
|
|
const { theme } = useTheme();
|
2021-09-13 20:41:05 +00:00
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
|
|
|
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
|
2023-05-15 18:10:43 +00:00
|
|
|
const filePath = useRef('');
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
const handleAutoDownload = async () => {
|
|
|
|
if (img) {
|
|
|
|
const searchImageBestQuality = await searchMediaFileAsync({
|
|
|
|
type: MediaTypes.image,
|
|
|
|
mimeType: file.image_type,
|
|
|
|
messageId
|
|
|
|
});
|
|
|
|
filePath.current = searchImageBestQuality.filePath;
|
|
|
|
if (searchImageBestQuality.file?.exists) {
|
|
|
|
file.title_link = searchImageBestQuality.file.uri;
|
|
|
|
return setToDownload(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user, author });
|
|
|
|
if (autoDownload) {
|
|
|
|
await handleDownload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
handleAutoDownload();
|
|
|
|
}, []);
|
2022-04-01 21:52:38 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!img) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:10:43 +00:00
|
|
|
const handleDownload = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
const imgUrl = imageUrl || formatAttachmentUrl(file.title_link || file.image_url, user.id, user.token, baseUrl);
|
|
|
|
const imageUri = await downloadMediaFile({ url: imgUrl, filePath: filePath.current });
|
|
|
|
file.title_link = imageUri;
|
|
|
|
setToDownload(false);
|
|
|
|
setLoading(false);
|
|
|
|
};
|
2023-05-11 23:20:48 +00:00
|
|
|
|
2022-03-21 20:44:06 +00:00
|
|
|
const onPress = () => {
|
2023-05-15 18:10:43 +00:00
|
|
|
if (loading) {
|
|
|
|
// Cancelar o download
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toDownload && !loading) {
|
|
|
|
return handleDownload();
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:44:06 +00:00
|
|
|
if (!showAttachment) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return showAttachment(file);
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
if (file.description) {
|
|
|
|
return (
|
2022-03-21 20:44:06 +00:00
|
|
|
<Button disabled={isReply} theme={theme} onPress={onPress}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<View>
|
|
|
|
<Markdown
|
|
|
|
msg={file.description}
|
2022-03-21 20:44:06 +00:00
|
|
|
style={[isReply && style]}
|
2021-09-13 20:41:05 +00:00
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
2022-07-04 18:10:14 +00:00
|
|
|
theme={theme}
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
2023-05-15 18:10:43 +00:00
|
|
|
<MessageImage imgUri={img} toDownload={toDownload} loading={loading} />
|
2021-09-13 20:41:05 +00:00
|
|
|
</View>
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-21 20:44:06 +00:00
|
|
|
<Button disabled={isReply} theme={theme} onPress={onPress}>
|
2023-05-15 18:10:43 +00:00
|
|
|
<>
|
|
|
|
<MessageImage imgUri={img} toDownload={toDownload} loading={loading} />
|
|
|
|
</>
|
2021-09-13 20:41:05 +00:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
2022-03-29 20:06:50 +00:00
|
|
|
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file)
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
ImageContainer.displayName = 'MessageImageContainer';
|
|
|
|
MessageImage.displayName = 'MessageImage';
|
|
|
|
|
|
|
|
export default ImageContainer;
|