2021-09-13 20:41:05 +00:00
|
|
|
import React, { useContext } 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';
|
|
|
|
|
|
|
|
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';
|
2022-03-21 20:44:06 +00:00
|
|
|
import { IAttachment } 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';
|
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;
|
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>
|
|
|
|
));
|
|
|
|
|
2022-04-12 16:27:05 +00:00
|
|
|
export const MessageImage = React.memo(({ imgUri, theme }: { imgUri: string; theme: TSupportedThemes }) => (
|
2021-09-13 20:41:05 +00:00
|
|
|
<ImageProgress
|
|
|
|
style={[styles.image, { borderColor: themes[theme].borderColor }]}
|
2022-04-01 21:52:38 +00:00
|
|
|
source={{ uri: encodeURI(imgUri) }}
|
2021-09-13 20:41:05 +00:00
|
|
|
resizeMode={FastImage.resizeMode.cover}
|
|
|
|
indicator={Progress.Pie}
|
|
|
|
indicatorProps={{
|
|
|
|
color: themes[theme].actionTintColor
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
|
|
|
const ImageContainer = React.memo(
|
2022-03-29 20:06:50 +00:00
|
|
|
({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply }: IMessageImage) => {
|
|
|
|
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);
|
2022-04-01 21:52:38 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!img) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:44:06 +00:00
|
|
|
const onPress = () => {
|
|
|
|
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
|
|
|
baseUrl={baseUrl}
|
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
2022-07-04 18:10:14 +00:00
|
|
|
theme={theme}
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
2022-04-01 21:52:38 +00:00
|
|
|
<MessageImage imgUri={img} theme={theme} />
|
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}>
|
2022-04-01 21:52:38 +00:00
|
|
|
<MessageImage imgUri={img} theme={theme} />
|
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;
|