2020-06-26 20:22:56 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
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';
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
|
|
|
import { THUMBS_HEIGHT } from './constants';
|
|
|
|
import { allowPreview } from './utils';
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const ThumbButton = isIOS ? TouchableOpacity : TouchableNativeFeedback;
|
|
|
|
|
|
|
|
const ThumbContent = React.memo(({ item, theme, isShareExtension }) => {
|
|
|
|
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
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<View style={[styles.thumb, { borderColor: themes[theme].borderColor }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon name='image' size={30} color={themes[theme].tintColor} />
|
2020-06-26 20:22:56 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const { uri } = item;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Image source={{ uri }} style={styles.thumb} />
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon name='camera-filled' size={20} color={themes[theme].buttonText} style={styles.videoThumbIcon} />
|
2021-02-01 17:18:55 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
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-09-13 20:41:05 +00:00
|
|
|
const Thumb = ({ item, theme, isShareExtension, onPress, onRemove }) => (
|
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-09-13 20:41:05 +00:00
|
|
|
const Thumbs = React.memo(({ attachments, theme, isShareExtension, onPress, onRemove }) => {
|
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
|
|
|
});
|
|
|
|
Thumbs.propTypes = {
|
|
|
|
attachments: PropTypes.array,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
isShareExtension: PropTypes.bool,
|
|
|
|
onPress: PropTypes.func,
|
|
|
|
onRemove: PropTypes.func
|
|
|
|
};
|
|
|
|
Thumb.propTypes = {
|
|
|
|
item: PropTypes.object,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
isShareExtension: PropTypes.bool,
|
|
|
|
onPress: PropTypes.func,
|
|
|
|
onRemove: PropTypes.func
|
|
|
|
};
|
|
|
|
ThumbContent.propTypes = {
|
|
|
|
item: PropTypes.object,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
isShareExtension: PropTypes.bool
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Thumbs;
|