2019-05-20 20:43:50 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, StyleSheet, Text, View } from 'react-native';
|
2019-05-20 20:43:50 +00:00
|
|
|
import Modal from 'react-native-modal';
|
|
|
|
import Touchable from 'react-native-platform-touchable';
|
|
|
|
|
|
|
|
import Emoji from './message/Emoji';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { CustomIcon } from '../lib/Icons';
|
|
|
|
import sharedStyles from '../views/Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2022-04-01 15:32:17 +00:00
|
|
|
import { useTheme, withTheme } from '../theme';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../definitions/IEmoji';
|
2022-03-02 14:18:01 +00:00
|
|
|
import { TMessageModel, ILoggedUser } from '../definitions';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from './SafeAreaView';
|
2019-05-20 20:43:50 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2020-06-15 14:00:46 +00:00
|
|
|
safeArea: {
|
|
|
|
backgroundColor: 'transparent'
|
|
|
|
},
|
2019-05-20 20:43:50 +00:00
|
|
|
titleContainer: {
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingVertical: 10
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
fontSize: 16,
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textSemibold,
|
|
|
|
...sharedStyles.textAlignCenter
|
2019-05-20 20:43:50 +00:00
|
|
|
},
|
|
|
|
reactCount: {
|
|
|
|
fontSize: 13,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
peopleReacted: {
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
peopleItemContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
emojiContainer: {
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
itemContainer: {
|
|
|
|
height: 50,
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
|
|
|
listContainer: {
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
closeButton: {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
2019-12-04 16:39:53 +00:00
|
|
|
top: 10
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const standardEmojiStyle = { fontSize: 20 };
|
|
|
|
const customEmojiStyle = { width: 20, height: 20 };
|
|
|
|
|
2022-04-01 15:32:17 +00:00
|
|
|
interface ISharedFields {
|
|
|
|
user?: Pick<ILoggedUser, 'username'>;
|
|
|
|
baseUrl: string;
|
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IItem extends ISharedFields {
|
2021-09-13 20:41:05 +00:00
|
|
|
item: {
|
2022-04-01 15:32:17 +00:00
|
|
|
usernames: string[];
|
2021-09-13 20:41:05 +00:00
|
|
|
emoji: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:32:17 +00:00
|
|
|
interface IModalContent extends ISharedFields {
|
2022-03-02 14:18:01 +00:00
|
|
|
message?: TMessageModel;
|
2022-03-29 20:06:50 +00:00
|
|
|
onClose: () => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:32:17 +00:00
|
|
|
interface IReactionsModal extends ISharedFields {
|
|
|
|
message?: TMessageModel;
|
2021-09-13 20:41:05 +00:00
|
|
|
isVisible: boolean;
|
|
|
|
onClose(): void;
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:32:17 +00:00
|
|
|
const Item = React.memo(({ item, user, baseUrl, getCustomEmoji }: IItem) => {
|
|
|
|
const { theme } = useTheme();
|
2019-05-20 20:43:50 +00:00
|
|
|
const count = item.usernames.length;
|
2021-09-13 20:41:05 +00:00
|
|
|
let usernames = item.usernames
|
|
|
|
.slice(0, 3)
|
2022-04-01 15:32:17 +00:00
|
|
|
.map((username: string) => (username === user?.username ? I18n.t('you') : username))
|
2021-09-13 20:41:05 +00:00
|
|
|
.join(', ');
|
2019-05-20 20:43:50 +00:00
|
|
|
if (count > 3) {
|
2021-09-13 20:41:05 +00:00
|
|
|
usernames = `${usernames} ${I18n.t('and_more')} ${count - 3}`;
|
2019-05-20 20:43:50 +00:00
|
|
|
} else {
|
2021-09-13 20:41:05 +00:00
|
|
|
usernames = usernames.replace(/,(?=[^,]*$)/, ` ${I18n.t('and')}`);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.itemContainer}>
|
|
|
|
<View style={styles.emojiContainer}>
|
|
|
|
<Emoji
|
|
|
|
content={item.emoji}
|
|
|
|
standardEmojiStyle={standardEmojiStyle}
|
|
|
|
customEmojiStyle={customEmojiStyle}
|
2022-04-01 15:32:17 +00:00
|
|
|
baseUrl={baseUrl}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
2019-05-20 20:43:50 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={styles.peopleItemContainer}>
|
2022-04-01 15:32:17 +00:00
|
|
|
<Text style={[styles.reactCount, { color: themes[theme].buttonText }]}>
|
2019-05-20 20:43:50 +00:00
|
|
|
{count === 1 ? I18n.t('1_person_reacted') : I18n.t('N_people_reacted', { n: count })}
|
|
|
|
</Text>
|
2022-04-01 15:32:17 +00:00
|
|
|
<Text style={[styles.peopleReacted, { color: themes[theme].buttonText }]}>{usernames}</Text>
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const ModalContent = React.memo(({ message, onClose, ...props }: IModalContent) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
if (message && message.reactions) {
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView style={styles.safeArea}>
|
2019-05-20 20:43:50 +00:00
|
|
|
<Touchable onPress={onClose}>
|
|
|
|
<View style={styles.titleContainer}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon style={[styles.closeButton, { color: themes[props.theme].buttonText }]} name='close' size={20} />
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.title, { color: themes[props.theme].buttonText }]}>{I18n.t('Reactions')}</Text>
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
|
|
|
</Touchable>
|
|
|
|
<FlatList
|
|
|
|
style={styles.listContainer}
|
|
|
|
data={message.reactions}
|
|
|
|
renderItem={({ item }) => <Item item={item} {...props} />}
|
|
|
|
keyExtractor={item => item.emoji}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const ReactionsModal = React.memo(
|
2022-04-01 15:32:17 +00:00
|
|
|
({ isVisible, onClose, ...props }: IReactionsModal) => {
|
|
|
|
const { theme } = useTheme();
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
isVisible={isVisible}
|
|
|
|
onBackdropPress={onClose}
|
|
|
|
onBackButtonPress={onClose}
|
|
|
|
backdropOpacity={0.8}
|
|
|
|
onSwipeComplete={onClose}
|
|
|
|
swipeDirection={['up', 'left', 'right', 'down']}>
|
|
|
|
<ModalContent onClose={onClose} theme={theme} {...props} />
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(prevProps, nextProps) => prevProps.isVisible === nextProps.isVisible
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
2019-05-20 20:43:50 +00:00
|
|
|
|
|
|
|
ReactionsModal.displayName = 'ReactionsModal';
|
|
|
|
ModalContent.displayName = 'ReactionsModalContent';
|
|
|
|
Item.displayName = 'ReactionsModalItem';
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default withTheme(ReactionsModal);
|