diff --git a/native/chat/reaction-message-utils.js b/native/chat/reaction-message-utils.js index 058560518..ae1e8f031 100644 --- a/native/chat/reaction-message-utils.js +++ b/native/chat/reaction-message-utils.js @@ -1,194 +1,203 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import Alert from 'react-native/Libraries/Alert/Alert.js'; import { sendReactionMessage, sendReactionMessageActionTypes, } from 'lib/actions/message-actions.js'; import type { ReactionInfo } from 'lib/selectors/chat-selectors.js'; import { messageTypes } from 'lib/types/message-types.js'; import type { RawReactionMessageInfo } from 'lib/types/messages/reaction.js'; import { useDispatchActionPromise, useServerCall, } from 'lib/utils/action-utils.js'; import { cloneError } from 'lib/utils/errors.js'; import { useSelector } from '../redux/redux-utils.js'; import type { LayoutCoordinates, VerticalBounds, } from '../types/layout-types.js'; function useSendReaction( messageID: ?string, localID: string, threadID: string, reactions: ReactionInfo, ): (reaction: string) => mixed { const viewerID = useSelector( state => state.currentUserInfo && state.currentUserInfo.id, ); const callSendReactionMessage = useServerCall(sendReactionMessage); const dispatchActionPromise = useDispatchActionPromise(); return React.useCallback( reaction => { if (!messageID) { return; } invariant(viewerID, 'viewerID should be set'); const viewerReacted = reactions[reaction] ? reactions[reaction].viewerReacted : false; const action = viewerReacted ? 'remove_reaction' : 'add_reaction'; const reactionMessagePromise = (async () => { try { const result = await callSendReactionMessage({ threadID, localID, targetMessageID: messageID, reaction, action, }); return { localID, serverID: result.id, threadID, time: result.time, interface: result.interface, }; } catch (e) { Alert.alert( 'Couldn’t send the reaction', 'Please try again later', [{ text: 'OK' }], { cancelable: true, }, ); const copy = cloneError(e); copy.localID = localID; copy.threadID = threadID; throw copy; } })(); const startingPayload: RawReactionMessageInfo = { type: messageTypes.REACTION, threadID, localID, creatorID: viewerID, time: Date.now(), targetMessageID: messageID, reaction, action, }; dispatchActionPromise( sendReactionMessageActionTypes, reactionMessagePromise, undefined, startingPayload, ); }, [ messageID, viewerID, reactions, threadID, localID, dispatchActionPromise, callSendReactionMessage, ], ); } type ReactionSelectionPopoverPositionArgs = { +initialCoordinates: LayoutCoordinates, +verticalBounds: VerticalBounds, +margin: ?number, }; type ReactionSelectionPopoverPosition = { +containerStyle: { +position: 'absolute', +left?: number, +right?: number, +bottom?: number, +top?: number, ... }, +popoverLocation: 'above' | 'below', }; function useReactionSelectionPopoverPosition({ initialCoordinates, verticalBounds, margin, }: ReactionSelectionPopoverPositionArgs): ReactionSelectionPopoverPosition { - const reactionSelectionPopoverHeight = 56; - - const calculatedMargin = margin ?? 16; + const calculatedMargin = getCalculatedMargin(margin); const windowWidth = useSelector(state => state.dimensions.width); const popoverLocation: 'above' | 'below' = (() => { const { y, height } = initialCoordinates; const contentTop = y; const contentBottom = y + height; const boundsTop = verticalBounds.y; const boundsBottom = verticalBounds.y + verticalBounds.height; const fullHeight = reactionSelectionPopoverHeight + calculatedMargin; if ( contentBottom + fullHeight > boundsBottom && contentTop - fullHeight > boundsTop ) { return 'above'; } return 'below'; })(); const containerStyle = React.useMemo(() => { const { x, width, height } = initialCoordinates; const style = {}; style.position = 'absolute'; const extraLeftSpace = x; const extraRightSpace = windowWidth - width - x; if (extraLeftSpace < extraRightSpace) { style.left = 0; } else { style.right = 0; } if (popoverLocation === 'above') { style.bottom = height + calculatedMargin / 2; } else { style.top = height + calculatedMargin / 2; } return style; }, [calculatedMargin, initialCoordinates, popoverLocation, windowWidth]); return React.useMemo( () => ({ popoverLocation, containerStyle, }), [popoverLocation, containerStyle], ); } -export { useSendReaction, useReactionSelectionPopoverPosition }; +function getCalculatedMargin(margin: ?number): number { + return margin ?? 16; +} + +const reactionSelectionPopoverHeight = 56; + +export { + useSendReaction, + useReactionSelectionPopoverPosition, + getCalculatedMargin, + reactionSelectionPopoverHeight, +}; diff --git a/native/chat/reaction-selection-popover.react.js b/native/chat/reaction-selection-popover.react.js index 019a5295d..a70230ce3 100644 --- a/native/chat/reaction-selection-popover.react.js +++ b/native/chat/reaction-selection-popover.react.js @@ -1,167 +1,194 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import { View, TouchableOpacity, Text } from 'react-native'; import Animated from 'react-native-reanimated'; -import { useReactionSelectionPopoverPosition } from './reaction-message-utils.js'; +import { + useReactionSelectionPopoverPosition, + getCalculatedMargin, + reactionSelectionPopoverHeight, +} from './reaction-message-utils.js'; import SWMansionIcon from '../components/swmansion-icon.react.js'; import type { AppNavigationProp } from '../navigation/app-navigator.react.js'; import { OverlayContext } from '../navigation/overlay-context.js'; import type { TooltipModalParamList } from '../navigation/route-names.js'; import { useStyles } from '../themes/colors.js'; import { useTooltipActions } from '../tooltip/tooltip-hooks.js'; import type { TooltipRoute } from '../tooltip/tooltip.react.js'; import { AnimatedView } from '../types/styles.js'; type Props> = { +navigation: AppNavigationProp, +route: TooltipRoute, +openEmojiPicker: () => mixed, +sendReaction: (reaction: string) => mixed, }; /* eslint-disable import/no-named-as-default-member */ const { Extrapolate, interpolateNode } = Animated; /* eslint-enable import/no-named-as-default-member */ function ReactionSelectionPopover>( props: Props, ): React.Node { const { navigation, route, openEmojiPicker, sendReaction } = props; const { verticalBounds, initialCoordinates, margin } = route.params; - const { containerStyle: popoverContainerStyle } = + const { containerStyle: popoverContainerStyle, popoverLocation } = useReactionSelectionPopoverPosition({ initialCoordinates, verticalBounds, margin, }); const overlayContext = React.useContext(OverlayContext); invariant( overlayContext, 'ReactionSelectionPopover should have OverlayContext', ); const { position } = overlayContext; - const animationStyle = React.useMemo( - () => ({ - opacity: interpolateNode(position, { - inputRange: [0, 0.1], - outputRange: [0, 1], - extrapolate: Extrapolate.CLAMP, - }), - transform: [ - { - scale: interpolateNode(position, { - inputRange: [0.2, 0.8], - outputRange: [0, 1], - extrapolate: Extrapolate.CLAMP, - }), - }, - ], - }), - [position], - ); + const calculatedMargin = getCalculatedMargin(margin); + const animationStyle = React.useMemo(() => { + const style = {}; + style.opacity = interpolateNode(position, { + inputRange: [0, 0.1], + outputRange: [0, 1], + extrapolate: Extrapolate.CLAMP, + }); + style.transform = [ + { + scale: interpolateNode(position, { + inputRange: [0.2, 0.8], + outputRange: [0, 1], + extrapolate: Extrapolate.CLAMP, + }), + }, + ]; + if (popoverLocation === 'above') { + style.transform.push({ + translateY: interpolateNode(position, { + inputRange: [0, 1], + outputRange: [ + calculatedMargin + reactionSelectionPopoverHeight / 2, + 0, + ], + extrapolate: Extrapolate.CLAMP, + }), + }); + } else { + style.transform.push({ + translateY: interpolateNode(position, { + inputRange: [0, 1], + outputRange: [ + -calculatedMargin - reactionSelectionPopoverHeight / 2, + 0, + ], + extrapolate: Extrapolate.CLAMP, + }), + }); + } + return style; + }, [position, calculatedMargin, popoverLocation]); const styles = useStyles(unboundStyles); const containerStyle = React.useMemo( () => ({ ...styles.reactionSelectionPopoverContainer, ...popoverContainerStyle, ...animationStyle, }), [ popoverContainerStyle, styles.reactionSelectionPopoverContainer, animationStyle, ], ); const tooltipRouteKey = route.key; const { hideTooltip, dismissTooltip } = useTooltipActions( navigation, tooltipRouteKey, ); const onPressDefaultEmoji = React.useCallback( (emoji: string) => { sendReaction(emoji); dismissTooltip(); }, [sendReaction, dismissTooltip], ); const onPressEmojiKeyboardButton = React.useCallback(() => { openEmojiPicker(); hideTooltip(); }, [openEmojiPicker, hideTooltip]); const defaultEmojis = React.useMemo(() => { const defaultEmojisData = ['❤️', '😆', '😮', '😠', '👍']; return defaultEmojisData.map(emoji => ( onPressDefaultEmoji(emoji)}> {emoji} )); }, [ onPressDefaultEmoji, styles.reactionSelectionItemContainer, styles.reactionSelectionItemEmoji, ]); return ( {defaultEmojis} ); } const unboundStyles = { reactionSelectionPopoverContainer: { flexDirection: 'row', alignItems: 'center', backgroundColor: 'tooltipBackground', padding: 8, borderRadius: 8, flex: 1, }, reactionSelectionItemContainer: { backgroundColor: 'reactionSelectionPopoverItemBackground', justifyContent: 'center', alignItems: 'center', padding: 8, borderRadius: 20, width: 40, height: 40, marginRight: 12, }, reactionSelectionItemEmoji: { fontSize: 18, }, emojiKeyboardButtonContainer: { backgroundColor: 'reactionSelectionPopoverItemBackground', justifyContent: 'center', alignItems: 'center', padding: 8, borderRadius: 20, width: 40, height: 40, }, icon: { color: 'modalForegroundLabel', }, }; export default ReactionSelectionPopover;