/* * Wire * Copyright (C) 2022 Wire Swiss GmbH * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * */ import {useState, RefObject, FC, useRef} from 'react'; import {isSpaceOrEnterKey} from 'Util/KeyboardUtil'; import {t} from 'Util/LocalizerUtil'; import {EmojiChar} from './EmojiChar'; import {reactionImgSize} from './EmojiChar.styles'; import {EmojiPickerContainer} from './EmojiPicker'; import {MessageActionsId} from '../MessageActions'; import {useMessageActionsState} from '../MessageActions.state'; import {messageActionsMenuButton, getActionsMenuCSS, getIconCSS} from '../MessageActions.styles'; const thumbsUpEmoji = '👍'; const likeEmoji = '❤️'; const INITIAL_CLIENT_X_POS = 0; const INITIAL_CLIENT_Y_POS = 0; export interface MessageReactionsProps { messageFocusedTabIndex: number; currentMsgActionName: string; toggleActiveMenu: (event: React.MouseEvent | React.KeyboardEvent) => void; handleKeyDown: (event: React.KeyboardEvent) => void; handleCurrentMsgAction: (actionName: string) => void; resetActionMenuStates: () => void; wrapperRef: RefObject; handleReactionClick: (emoji: string) => void; } const MessageReactions: FC = ({ messageFocusedTabIndex, currentMsgActionName, handleCurrentMsgAction, toggleActiveMenu, handleKeyDown, resetActionMenuStates, wrapperRef, handleReactionClick, }) => { const isThumbUpAction = currentMsgActionName === MessageActionsId.THUMBSUP; const isLikeAction = currentMsgActionName === MessageActionsId.HEART; const [showEmojis, setShowEmojis] = useState(false); const {handleMenuOpen} = useMessageActionsState(); const [clientX, setPOSX] = useState(INITIAL_CLIENT_X_POS); const [clientY, setPOSY] = useState(INITIAL_CLIENT_Y_POS); const emojiButtonRef = useRef(null); const handleEmojiSelectionWithKeyboard = () => { if (showEmojis) { handleMenuOpen(false); setShowEmojis(false); } if (emojiButtonRef.current) { emojiButtonRef.current.focus(); } }; const handleOutsideClick = () => { resetActionMenuStates(); setShowEmojis(false); }; const handleReactionCurrentState = (actionName = '') => { const isActive = !!actionName; handleCurrentMsgAction(actionName); handleMenuOpen(isActive); setShowEmojis(isActive); }; const handleEmojiBtnClick = (event: React.MouseEvent) => { event.stopPropagation(); const selectedMsgActionName = event.currentTarget.dataset.uieName; if (currentMsgActionName === selectedMsgActionName) { // reset on double click handleReactionCurrentState(''); } else if (selectedMsgActionName) { handleReactionCurrentState(selectedMsgActionName); showReactions(event.currentTarget.getBoundingClientRect()); } }; const handleEmojiKeyDown = (event: React.KeyboardEvent) => { event.stopPropagation(); const selectedMsgActionName = event.currentTarget.dataset.uieName; handleKeyDown(event); if (isSpaceOrEnterKey(event.key)) { if (currentMsgActionName === selectedMsgActionName) { // reset on double click handleReactionCurrentState(''); } else if (selectedMsgActionName) { handleReactionCurrentState(selectedMsgActionName); showReactions(event.currentTarget.getBoundingClientRect()); } } }; const showReactions = (rect: DOMRect) => { setPOSX(rect.x); setPOSY(rect.y); }; const handleMsgActionClick = (event: React.MouseEvent) => { event.stopPropagation(); const actionType = event.currentTarget.dataset.uieName; switch (actionType) { case MessageActionsId.EMOJI: handleEmojiBtnClick(event); break; case MessageActionsId.THUMBSUP: toggleActiveMenu(event); handleReactionClick(thumbsUpEmoji); break; case MessageActionsId.HEART: toggleActiveMenu(event); handleReactionClick(likeEmoji); break; } }; const handleMsgActionKeyDown = (event: React.KeyboardEvent) => { event.stopPropagation(); const actionType = event.currentTarget.dataset.uieName; switch (actionType) { case MessageActionsId.EMOJI: handleEmojiKeyDown(event); break; case MessageActionsId.THUMBSUP: handleKeyDown(event); break; case MessageActionsId.HEART: handleKeyDown(event); break; } }; const emojiSize = 15; return ( <> {showEmojis ? ( ) : null} ); }; export {MessageReactions};