Rocket.Chat.ReactNative/app/containers/message/Reactions.js

104 lines
3.0 KiB
JavaScript
Raw Normal View History

import React, { useContext } from 'react';
import { View, Text } from 'react-native';
import PropTypes from 'prop-types';
import Touchable from './Touchable';
import { CustomIcon } from '../../lib/Icons';
import styles from './styles';
import Emoji from './Emoji';
import { BUTTON_HIT_SLOP } from './utils';
2019-12-04 16:39:53 +00:00
import { themes } from '../../constants/colors';
import { withTheme } from '../../theme';
import MessageContext from './Context';
const AddReaction = React.memo(({ theme }) => {
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)}
hitSlop={BUTTON_HIT_SLOP}
>
<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} />
</View>
</Touchable>
);
});
const Reaction = React.memo(({
reaction, getCustomEmoji, theme
}) => {
const {
onReactionPress, onReactionLongPress, baseUrl, user
} = useContext(MessageContext);
const reacted = reaction.usernames.findIndex(item => item === user.username) !== -1;
return (
<Touchable
onPress={() => onReactionPress(reaction.emoji)}
onLongPress={onReactionLongPress}
key={reaction.emoji}
testID={`message-reaction-${ reaction.emoji }`}
2019-12-04 16:39:53 +00:00
style={[styles.reactionButton, { backgroundColor: reacted ? themes[theme].bannerBackground : themes[theme].backgroundColor }]}
background={Touchable.Ripple(themes[theme].bannerBackground)}
hitSlop={BUTTON_HIT_SLOP}
>
2019-12-04 16:39:53 +00:00
<View style={[styles.reactionContainer, { borderColor: reacted ? themes[theme].tintColor : themes[theme].borderColor }]}>
<Emoji
content={reaction.emoji}
standardEmojiStyle={styles.reactionEmoji}
customEmojiStyle={styles.reactionCustomEmoji}
baseUrl={baseUrl}
getCustomEmoji={getCustomEmoji}
/>
2019-12-04 16:39:53 +00:00
<Text style={[styles.reactionCount, { color: themes[theme].tintColor }]}>{ reaction.usernames.length }</Text>
</View>
</Touchable>
);
});
const Reactions = React.memo(({
reactions, getCustomEmoji, theme
}) => {
if (!Array.isArray(reactions) || reactions.length === 0) {
return null;
}
return (
<View style={styles.reactionsContainer}>
{reactions.map(reaction => (
<Reaction
key={reaction.emoji}
reaction={reaction}
getCustomEmoji={getCustomEmoji}
2019-12-04 16:39:53 +00:00
theme={theme}
/>
))}
<AddReaction theme={theme} />
</View>
);
});
Reaction.propTypes = {
reaction: PropTypes.object,
2019-12-04 16:39:53 +00:00
getCustomEmoji: PropTypes.func,
theme: PropTypes.string
};
Reaction.displayName = 'MessageReaction';
Reactions.propTypes = {
reactions: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
2019-12-04 16:39:53 +00:00
getCustomEmoji: PropTypes.func,
theme: PropTypes.string
};
Reactions.displayName = 'MessageReactions';
AddReaction.propTypes = {
2019-12-04 16:39:53 +00:00
theme: PropTypes.string
};
AddReaction.displayName = 'MessageAddReaction';
2019-12-04 16:39:53 +00:00
export default withTheme(Reactions);