2020-04-30 20:05:59 +00:00
|
|
|
import React, { useContext } from 'react';
|
2019-05-20 20:43:50 +00:00
|
|
|
import { View } from 'react-native';
|
2020-07-17 17:39:06 +00:00
|
|
|
import FastImage from '@rocket.chat/react-native-fast-image';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2019-12-18 21:13:11 +00:00
|
|
|
import { createImageProgress } from 'react-native-image-progress';
|
|
|
|
import * as Progress from 'react-native-progress';
|
2018-08-01 19:35:06 +00:00
|
|
|
|
2020-04-30 20:05:59 +00:00
|
|
|
import Touchable from './Touchable';
|
2019-08-27 12:25:38 +00:00
|
|
|
import Markdown from '../markdown';
|
2018-08-01 19:35:06 +00:00
|
|
|
import styles from './styles';
|
2019-05-20 20:43:50 +00:00
|
|
|
import { formatAttachmentUrl } from '../../lib/utils';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2021-07-21 21:01:59 +00:00
|
|
|
type TMessageButton = {
|
|
|
|
children: JSX.Element;
|
|
|
|
onPress: Function;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type TMessageImage = {
|
|
|
|
img: string;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IMessageImage {
|
2021-07-26 22:33:27 +00:00
|
|
|
file: { image_url: string; description?: string; };
|
2021-07-22 20:09:06 +00:00
|
|
|
imageUrl?: string;
|
2021-07-21 21:01:59 +00:00
|
|
|
showAttachment: Function;
|
|
|
|
theme: string;
|
2021-07-26 22:33:27 +00:00
|
|
|
getCustomEmoji?: Function;
|
2021-07-21 21:01:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:13:11 +00:00
|
|
|
const ImageProgress = createImageProgress(FastImage);
|
|
|
|
|
2021-07-21 21:01:59 +00:00
|
|
|
const Button = React.memo(({children, onPress, theme}: TMessageButton) => (
|
2019-05-20 20:43:50 +00:00
|
|
|
<Touchable
|
|
|
|
onPress={onPress}
|
2020-06-15 14:00:46 +00:00
|
|
|
style={styles.imageContainer}
|
2019-12-04 16:39:53 +00:00
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
2019-05-20 20:43:50 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Touchable>
|
|
|
|
));
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2021-07-21 21:01:59 +00:00
|
|
|
export const MessageImage = React.memo(({ img, theme }: TMessageImage) => (
|
2019-12-18 21:13:11 +00:00
|
|
|
<ImageProgress
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.image, { borderColor: themes[theme].borderColor }]}
|
2019-05-20 20:43:50 +00:00
|
|
|
source={{ uri: encodeURI(img) }}
|
|
|
|
resizeMode={FastImage.resizeMode.cover}
|
2019-12-18 21:13:11 +00:00
|
|
|
indicator={Progress.Pie}
|
|
|
|
indicatorProps={{
|
|
|
|
color: themes[theme].actionTintColor
|
|
|
|
}}
|
2019-05-20 20:43:50 +00:00
|
|
|
/>
|
|
|
|
));
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2021-07-21 21:01:59 +00:00
|
|
|
const ImageContainer = React.memo(({file, imageUrl, showAttachment, getCustomEmoji, theme}: IMessageImage) => {
|
2020-04-30 20:05:59 +00:00
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
|
2019-05-20 20:43:50 +00:00
|
|
|
if (!img) {
|
|
|
|
return null;
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2019-12-18 21:13:11 +00:00
|
|
|
const onPress = () => showAttachment(file);
|
2018-09-19 14:18:32 +00:00
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
if (file.description) {
|
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<Button theme={theme} onPress={onPress}>
|
2019-05-20 20:43:50 +00:00
|
|
|
<View>
|
2020-02-11 14:01:35 +00:00
|
|
|
<MessageImage img={img} theme={theme} />
|
2021-07-21 21:01:59 +00:00
|
|
|
//TODO - fix the required fields for the Markdown
|
|
|
|
{/*@ts-ignore*/}
|
2020-02-27 18:34:20 +00:00
|
|
|
<Markdown msg={file.description} baseUrl={baseUrl} username={user.username} getCustomEmoji={getCustomEmoji} theme={theme} />
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
|
|
|
</Button>
|
|
|
|
);
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<Button theme={theme} onPress={onPress}>
|
2020-02-11 14:01:35 +00:00
|
|
|
<MessageImage img={img} theme={theme} />
|
2019-05-20 20:43:50 +00:00
|
|
|
</Button>
|
|
|
|
);
|
2021-02-26 16:01:45 +00:00
|
|
|
}, (prevProps, nextProps) => dequal(prevProps.file, nextProps.file) && prevProps.theme === nextProps.theme);
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
ImageContainer.displayName = 'MessageImageContainer';
|
2021-07-22 18:59:48 +00:00
|
|
|
MessageImage.displayName = 'MessageImage';
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
export default ImageContainer;
|