verdnatura-chat/app/containers/message/Image.tsx

107 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

import React, { useContext } 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 { createImageProgress } from 'react-native-image-progress';
import * as Progress from 'react-native-progress';
import Touchable from './Touchable';
import Markdown from '../markdown';
import styles from './styles';
import { themes } from '../../lib/constants';
import MessageContext from './Context';
import { TGetCustomEmoji } from '../../definitions/IEmoji';
2022-03-21 20:44:06 +00:00
import { IAttachment } from '../../definitions';
import { TSupportedThemes, useTheme } from '../../theme';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
interface IMessageButton {
children: React.ReactElement;
2022-03-21 20:44:06 +00:00
disabled?: boolean;
onPress: () => void;
theme: TSupportedThemes;
}
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;
}
const ImageProgress = createImageProgress(FastImage);
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}
background={Touchable.Ripple(themes[theme].bannerBackground)}
>
{children}
</Touchable>
));
export const MessageImage = React.memo(({ imgUri, theme }: { imgUri: string; theme: TSupportedThemes }) => (
<ImageProgress
style={[styles.image, { borderColor: themes[theme].borderColor }]}
source={{ uri: encodeURI(imgUri) }}
resizeMode={FastImage.resizeMode.cover}
indicator={Progress.Pie}
indicatorProps={{
color: themes[theme].actionTintColor
}}
/>
));
const ImageContainer = React.memo(
({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply }: IMessageImage) => {
const { theme } = useTheme();
const { baseUrl, user } = useContext(MessageContext);
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
if (!img) {
return null;
}
2022-03-21 20:44:06 +00:00
const onPress = () => {
if (!showAttachment) {
return;
}
return showAttachment(file);
};
if (file.description) {
return (
2022-03-21 20:44:06 +00:00
<Button disabled={isReply} theme={theme} onPress={onPress}>
<View>
<Markdown
msg={file.description}
2022-03-21 20:44:06 +00:00
style={[isReply && style]}
username={user.username}
getCustomEmoji={getCustomEmoji}
theme={theme}
/>
<MessageImage imgUri={img} theme={theme} />
</View>
</Button>
);
}
return (
2022-03-21 20:44:06 +00:00
<Button disabled={isReply} theme={theme} onPress={onPress}>
<MessageImage imgUri={img} theme={theme} />
</Button>
);
},
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file)
);
ImageContainer.displayName = 'MessageImageContainer';
MessageImage.displayName = 'MessageImage';
export default ImageContainer;