2020-04-30 20:05:59 +00:00
|
|
|
import React, { useContext } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { Text, View } from 'react-native';
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2020-04-30 20:05:59 +00:00
|
|
|
import Touchable from './Touchable';
|
2019-05-20 20:43:50 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import styles from './styles';
|
|
|
|
import Emoji from './Emoji';
|
|
|
|
import { BUTTON_HIT_SLOP } from './utils';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../../definitions/IEmoji';
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
interface IReaction {
|
|
|
|
_id: string;
|
|
|
|
emoji: string;
|
|
|
|
usernames: string[];
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IMessageReaction {
|
2022-04-01 21:52:38 +00:00
|
|
|
reaction: IReaction;
|
2022-02-17 15:27:01 +00:00
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
2021-09-13 20:41:05 +00:00
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IMessageReactions {
|
2022-04-01 21:52:38 +00:00
|
|
|
reactions?: IReaction[];
|
2022-02-17 15:27:01 +00:00
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
const AddReaction = React.memo(({ theme }: { theme: string }) => {
|
2020-04-30 20:05:59 +00:00
|
|
|
const { reactionInit } = useContext(MessageContext);
|
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
onPress={reactionInit}
|
|
|
|
key='message-add-reaction'
|
|
|
|
testID='message-add-reaction'
|
|
|
|
style={[styles.reactionButton, { backgroundColor: themes[theme].backgroundColor }]}
|
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
2021-09-13 20:41:05 +00:00
|
|
|
hitSlop={BUTTON_HIT_SLOP}>
|
2020-04-30 20:05:59 +00:00
|
|
|
<View style={[styles.reactionContainer, { borderColor: themes[theme].borderColor }]}>
|
2020-07-27 19:53:33 +00:00
|
|
|
<CustomIcon name='reaction-add' size={21} color={themes[theme].tintColor} />
|
2020-04-30 20:05:59 +00:00
|
|
|
</View>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
});
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const Reaction = React.memo(({ reaction, getCustomEmoji, theme }: IMessageReaction) => {
|
|
|
|
const { onReactionPress, onReactionLongPress, baseUrl, user } = useContext(MessageContext);
|
2022-04-01 21:52:38 +00:00
|
|
|
const reacted = reaction.usernames.findIndex((item: string) => item === user.username) !== -1;
|
2019-05-20 20:43:50 +00:00
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
onPress={() => onReactionPress(reaction.emoji)}
|
|
|
|
onLongPress={onReactionLongPress}
|
|
|
|
key={reaction.emoji}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`message-reaction-${reaction.emoji}`}
|
|
|
|
style={[
|
|
|
|
styles.reactionButton,
|
|
|
|
{ backgroundColor: reacted ? themes[theme].bannerBackground : themes[theme].backgroundColor }
|
|
|
|
]}
|
2019-12-04 16:39:53 +00:00
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
2021-09-13 20:41:05 +00:00
|
|
|
hitSlop={BUTTON_HIT_SLOP}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<View style={[styles.reactionContainer, { borderColor: reacted ? themes[theme].tintColor : themes[theme].borderColor }]}>
|
2019-05-20 20:43:50 +00:00
|
|
|
<Emoji
|
|
|
|
content={reaction.emoji}
|
|
|
|
standardEmojiStyle={styles.reactionEmoji}
|
|
|
|
customEmojiStyle={styles.reactionCustomEmoji}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.reactionCount, { color: themes[theme].tintColor }]}>{reaction.usernames.length}</Text>
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
|
|
|
</Touchable>
|
|
|
|
);
|
2019-05-23 17:51:42 +00:00
|
|
|
});
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
const Reactions = React.memo(({ reactions, getCustomEmoji }: IMessageReactions) => {
|
|
|
|
const { theme } = useTheme();
|
|
|
|
|
2020-02-17 16:06:30 +00:00
|
|
|
if (!Array.isArray(reactions) || reactions.length === 0) {
|
2019-05-20 20:43:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.reactionsContainer}>
|
2022-04-01 21:52:38 +00:00
|
|
|
{reactions.map(reaction => (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Reaction key={reaction.emoji} reaction={reaction} getCustomEmoji={getCustomEmoji} theme={theme} />
|
2019-05-20 20:43:50 +00:00
|
|
|
))}
|
2020-04-30 20:05:59 +00:00
|
|
|
<AddReaction theme={theme} />
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Reaction.displayName = 'MessageReaction';
|
|
|
|
Reactions.displayName = 'MessageReactions';
|
|
|
|
AddReaction.displayName = 'MessageAddReaction';
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
export default Reactions;
|