2020-06-26 20:22:56 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, Image, StyleSheet, View } from 'react-native';
|
|
|
|
import { RectButton, TouchableNativeFeedback, TouchableOpacity } from 'react-native-gesture-handler';
|
2020-06-26 20:22:56 +00:00
|
|
|
|
|
|
|
import { BUTTON_HIT_SLOP } from '../../containers/message/utils';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../../containers/CustomIcon';
|
2020-06-26 20:22:56 +00:00
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
|
|
|
import { THUMBS_HEIGHT } from './constants';
|
|
|
|
import { allowPreview } from './utils';
|
2021-11-16 16:19:50 +00:00
|
|
|
import { IAttachment } from './interfaces';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes } from '../../theme';
|
2020-06-26 20:22:56 +00:00
|
|
|
|
|
|
|
const THUMB_SIZE = 64;
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
list: {
|
|
|
|
height: THUMBS_HEIGHT,
|
|
|
|
paddingHorizontal: 8
|
|
|
|
},
|
|
|
|
videoThumbIcon: {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
bottom: 0
|
|
|
|
},
|
|
|
|
dangerIcon: {
|
|
|
|
position: 'absolute',
|
|
|
|
right: 16,
|
|
|
|
bottom: 0
|
|
|
|
},
|
|
|
|
removeButton: {
|
|
|
|
position: 'absolute',
|
|
|
|
right: 6,
|
|
|
|
width: 28,
|
|
|
|
height: 28,
|
|
|
|
borderWidth: 2,
|
|
|
|
borderRadius: 14,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
removeView: {
|
|
|
|
width: 28,
|
|
|
|
height: 28,
|
|
|
|
borderWidth: 2,
|
|
|
|
borderRadius: 14,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
item: {
|
|
|
|
paddingTop: 8
|
|
|
|
},
|
|
|
|
thumb: {
|
|
|
|
width: THUMB_SIZE,
|
|
|
|
height: THUMB_SIZE,
|
|
|
|
borderRadius: 2,
|
|
|
|
marginRight: 16,
|
|
|
|
overflow: 'hidden',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
borderWidth: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-16 16:19:50 +00:00
|
|
|
interface IThumbContent {
|
|
|
|
item: IAttachment;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-11-16 16:19:50 +00:00
|
|
|
isShareExtension: boolean;
|
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
|
2021-11-16 16:19:50 +00:00
|
|
|
interface IThumb extends IThumbContent {
|
|
|
|
onPress(item: IAttachment): void;
|
|
|
|
onRemove(item: IAttachment): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IThumbs extends Omit<IThumb, 'item'> {
|
|
|
|
attachments: IAttachment[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const ThumbContent = React.memo(({ item, theme, isShareExtension }: IThumbContent) => {
|
2020-06-26 20:22:56 +00:00
|
|
|
const type = item?.mime;
|
|
|
|
|
|
|
|
if (type?.match(/image/)) {
|
|
|
|
// Disallow preview of images too big in order to prevent memory issues on iOS share extension
|
|
|
|
if (allowPreview(isShareExtension, item?.size)) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return <Image source={{ uri: item.path }} style={[styles.thumb, { borderColor: themes[theme].borderColor }]} />;
|
2020-06-26 20:22:56 +00:00
|
|
|
}
|
2021-11-16 16:19:50 +00:00
|
|
|
return (
|
|
|
|
<View style={[styles.thumb, { borderColor: themes[theme].borderColor }]}>
|
|
|
|
<CustomIcon name='image' size={30} color={themes[theme].tintColor} />
|
|
|
|
</View>
|
|
|
|
);
|
2020-06-26 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type?.match(/video/)) {
|
2021-02-01 17:18:55 +00:00
|
|
|
if (isIOS) {
|
|
|
|
return (
|
|
|
|
<View style={[styles.thumb, { borderColor: themes[theme].borderColor }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon name='camera' size={30} color={themes[theme].tintColor} />
|
2021-02-01 17:18:55 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2021-11-16 16:19:50 +00:00
|
|
|
const { uri } = item;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Image source={{ uri }} style={styles.thumb} />
|
|
|
|
<CustomIcon name='camera-filled' size={20} color={themes[theme].buttonText} style={styles.videoThumbIcon} />
|
|
|
|
</>
|
|
|
|
);
|
2020-06-26 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple files upload of files different than image/video is not implemented, so there's no thumb
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2021-11-16 16:19:50 +00:00
|
|
|
const ThumbButton: typeof React.Component = isIOS ? TouchableOpacity : TouchableNativeFeedback;
|
|
|
|
|
|
|
|
const Thumb = ({ item, theme, isShareExtension, onPress, onRemove }: IThumb) => (
|
2020-06-26 20:22:56 +00:00
|
|
|
<ThumbButton style={styles.item} onPress={() => onPress(item)} activeOpacity={0.7}>
|
|
|
|
<>
|
2021-09-13 20:41:05 +00:00
|
|
|
<ThumbContent item={item} theme={theme} isShareExtension={isShareExtension} />
|
2020-06-26 20:22:56 +00:00
|
|
|
<RectButton
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
|
|
|
style={[styles.removeButton, { backgroundColor: themes[theme].bodyText, borderColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
activeOpacity={1}
|
|
|
|
rippleColor={themes[theme].bannerBackground}
|
2021-09-13 20:41:05 +00:00
|
|
|
onPress={() => onRemove(item)}>
|
2020-06-26 20:22:56 +00:00
|
|
|
<View style={[styles.removeView, { borderColor: themes[theme].auxiliaryBackground }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon name='close' color={themes[theme].backgroundColor} size={14} />
|
2020-06-26 20:22:56 +00:00
|
|
|
</View>
|
|
|
|
</RectButton>
|
|
|
|
{!item?.canUpload ? (
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon name='warning' size={20} color={themes[theme].dangerColor} style={styles.dangerIcon} />
|
2020-06-26 20:22:56 +00:00
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
</ThumbButton>
|
|
|
|
);
|
|
|
|
|
2021-11-16 16:19:50 +00:00
|
|
|
const Thumbs = React.memo(({ attachments, theme, isShareExtension, onPress, onRemove }: IThumbs) => {
|
2020-06-26 20:22:56 +00:00
|
|
|
if (attachments?.length > 1) {
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
horizontal
|
|
|
|
data={attachments}
|
|
|
|
keyExtractor={item => item.path}
|
|
|
|
renderItem={({ item }) => (
|
|
|
|
<Thumb
|
|
|
|
item={item}
|
|
|
|
theme={theme}
|
|
|
|
isShareExtension={isShareExtension}
|
|
|
|
onPress={() => onPress(item)}
|
|
|
|
onRemove={() => onRemove(item)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
style={[styles.list, { backgroundColor: themes[theme].messageboxBackground }]}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
return null;
|
2020-06-26 20:22:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default Thumbs;
|