2023-03-17 18:37:34 +00:00
|
|
|
import React, { useContext, useState } from 'react';
|
2022-03-29 16:09:44 +00:00
|
|
|
import { StyleSheet, Text, View } from 'react-native';
|
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
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 Touchable from './Touchable';
|
2022-06-06 14:17:51 +00:00
|
|
|
import openLink from '../../lib/methods/helpers/openLink';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, useTheme, withTheme } from '../../theme';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { LISTENER } from '../Toast';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
2021-09-13 20:41:05 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import MessageContext from './Context';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { IUrl } from '../../definitions';
|
2023-08-16 12:02:53 +00:00
|
|
|
import { DEFAULT_MESSAGE_HEIGHT } from './utils';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
marginTop: 6
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
borderRadius: 4,
|
|
|
|
borderWidth: 1
|
|
|
|
},
|
|
|
|
textContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
padding: 15,
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
alignItems: 'flex-start'
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
marginTop: {
|
|
|
|
marginTop: 4
|
|
|
|
},
|
|
|
|
image: {
|
|
|
|
width: '100%',
|
2023-08-16 12:02:53 +00:00
|
|
|
height: DEFAULT_MESSAGE_HEIGHT,
|
2021-09-13 20:41:05 +00:00
|
|
|
borderTopLeftRadius: 4,
|
|
|
|
borderTopRightRadius: 4
|
2023-03-17 18:37:34 +00:00
|
|
|
},
|
|
|
|
imageWithoutContent: {
|
|
|
|
borderRadius: 4
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
height: 0,
|
2023-09-26 12:54:21 +00:00
|
|
|
borderWidth: 0,
|
|
|
|
marginTop: 0
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const UrlContent = React.memo(
|
2023-03-17 18:37:34 +00:00
|
|
|
({ title, description }: { title: string; description: string }) => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
return (
|
|
|
|
<View style={styles.textContainer}>
|
|
|
|
{title ? (
|
|
|
|
<Text style={[styles.title, { color: colors.tintColor }]} numberOfLines={2}>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
) : null}
|
|
|
|
{description ? (
|
|
|
|
<Text style={[styles.description, { color: colors.auxiliaryText }]} numberOfLines={2}>
|
|
|
|
{description}
|
|
|
|
</Text>
|
|
|
|
) : null}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
2021-09-13 20:41:05 +00:00
|
|
|
(prevProps, nextProps) => {
|
|
|
|
if (prevProps.title !== nextProps.title) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (prevProps.description !== nextProps.description) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-03-17 18:37:34 +00:00
|
|
|
type TImageLoadedState = 'loading' | 'done' | 'error';
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const Url = React.memo(
|
2022-04-12 16:27:05 +00:00
|
|
|
({ url, index, theme }: { url: IUrl; index: number; theme: TSupportedThemes }) => {
|
2023-03-17 18:37:34 +00:00
|
|
|
const [imageLoadedState, setImageLoadedState] = useState<TImageLoadedState>('loading');
|
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
|
|
|
|
|
|
|
if (!url || url?.ignoreParse || imageLoadedState === 'error') {
|
2021-09-13 20:41:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onPress = () => openLink(url.url, theme);
|
|
|
|
|
|
|
|
const onLongPress = () => {
|
|
|
|
Clipboard.setString(url.url);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
|
|
|
|
};
|
|
|
|
|
2023-03-17 18:37:34 +00:00
|
|
|
const hasContent = url.title || url.description;
|
|
|
|
|
|
|
|
let image = url.image || url.url;
|
|
|
|
if (image) {
|
|
|
|
image = image.includes('http') ? image : `${baseUrl}/${image}?rc_uid=${user.id}&rc_token=${user.token}`;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
onPress={onPress}
|
|
|
|
onLongPress={onLongPress}
|
|
|
|
style={[
|
|
|
|
styles.button,
|
|
|
|
index > 0 && styles.marginTop,
|
|
|
|
styles.container,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].chatComponentBackground,
|
|
|
|
borderColor: themes[theme].borderColor
|
2023-03-17 18:37:34 +00:00
|
|
|
},
|
|
|
|
imageLoadedState === 'loading' && styles.loading
|
2021-09-13 20:41:05 +00:00
|
|
|
]}
|
2022-08-08 21:02:08 +00:00
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
|
|
|
>
|
2021-09-13 20:41:05 +00:00
|
|
|
<>
|
2023-03-17 18:37:34 +00:00
|
|
|
{image ? (
|
|
|
|
<FastImage
|
|
|
|
source={{ uri: image }}
|
|
|
|
style={[styles.image, !hasContent && styles.imageWithoutContent, imageLoadedState === 'loading' && styles.loading]}
|
|
|
|
resizeMode={FastImage.resizeMode.cover}
|
|
|
|
onError={() => setImageLoadedState('error')}
|
|
|
|
onLoad={() => setImageLoadedState('done')}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{hasContent ? <UrlContent title={url.title} description={url.description} /> : null}
|
2021-09-13 20:41:05 +00:00
|
|
|
</>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(oldProps, newProps) => dequal(oldProps.url, newProps.url) && oldProps.theme === newProps.theme
|
|
|
|
);
|
|
|
|
|
|
|
|
const Urls = React.memo(
|
2022-04-01 21:52:38 +00:00
|
|
|
// TODO - didn't work - (React.ReactElement | null)[] | React.ReactElement | null
|
|
|
|
({ urls }: { urls?: IUrl[] }): any => {
|
|
|
|
const { theme } = useTheme();
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!urls || urls.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
return urls.map((url: IUrl, index: number) => <Url url={url} key={url.url} index={index} theme={theme} />);
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
2022-04-01 21:52:38 +00:00
|
|
|
(oldProps, newProps) => dequal(oldProps.urls, newProps.urls)
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
UrlContent.displayName = 'MessageUrlContent';
|
|
|
|
Url.displayName = 'MessageUrl';
|
|
|
|
Urls.displayName = 'MessageUrls';
|
|
|
|
|
|
|
|
export default withTheme(Urls);
|