diff --git a/native/chat/reaction-message-utils.js b/native/chat/reaction-message-utils.js index 59087807d..14dd0d0f3 100644 --- a/native/chat/reaction-message-utils.js +++ b/native/chat/reaction-message-utils.js @@ -1,182 +1,188 @@ // @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'; import type { ViewStyle } from '../types/styles.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: ViewStyle, + +popoverLocation: 'above' | 'below', +}; function useReactionSelectionPopoverPosition({ initialCoordinates, verticalBounds, margin, -}: ReactionSelectionPopoverPositionArgs): ViewStyle { +}: ReactionSelectionPopoverPositionArgs): ReactionSelectionPopoverPosition { const reactionSelectionPopoverHeight = 56; const calculatedMargin = margin ?? 16; const windowWidth = useSelector(state => state.dimensions.width); - const reactionSelectionPopoverLocation: 'above' | 'below' = (() => { + 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'; })(); - return React.useMemo(() => { + 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 (reactionSelectionPopoverLocation === 'above') { + if (popoverLocation === 'above') { style.bottom = height + calculatedMargin / 2; } else { style.top = height + calculatedMargin / 2; } return style; - }, [ - calculatedMargin, - initialCoordinates, - reactionSelectionPopoverLocation, - windowWidth, - ]); + }, [calculatedMargin, initialCoordinates, popoverLocation, windowWidth]); + return React.useMemo( + () => ({ + popoverLocation, + containerStyle, + }), + [popoverLocation, containerStyle], + ); } export { useSendReaction, useReactionSelectionPopoverPosition }; diff --git a/native/chat/reaction-selection-popover.react.js b/native/chat/reaction-selection-popover.react.js index 98f5452a3..454145b4f 100644 --- a/native/chat/reaction-selection-popover.react.js +++ b/native/chat/reaction-selection-popover.react.js @@ -1,130 +1,124 @@ // @flow import * as React from 'react'; import { View, TouchableOpacity, Text } from 'react-native'; import { useReactionSelectionPopoverPosition } from './reaction-message-utils.js'; import SWMansionIcon from '../components/swmansion-icon.react.js'; import type { AppNavigationProp } from '../navigation/app-navigator.react.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'; type Props> = { +navigation: AppNavigationProp, +route: TooltipRoute, +openEmojiPicker: () => mixed, +sendReaction: (reaction: string) => mixed, }; function ReactionSelectionPopover>( props: Props, ): React.Node { const { navigation, route, openEmojiPicker, sendReaction } = props; const { verticalBounds, initialCoordinates, margin } = route.params; - const reactionSelectionPopoverContainerStyle = + const { containerStyle: popoverContainerStyle } = useReactionSelectionPopoverPosition({ initialCoordinates, verticalBounds, margin, }); const styles = useStyles(unboundStyles); const containerStyle = React.useMemo( - () => [ - styles.reactionSelectionPopoverContainer, - reactionSelectionPopoverContainerStyle, - ], - [ - reactionSelectionPopoverContainerStyle, - styles.reactionSelectionPopoverContainer, - ], + () => [styles.reactionSelectionPopoverContainer, popoverContainerStyle], + [popoverContainerStyle, styles.reactionSelectionPopoverContainer], ); 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;