import { motion } from 'framer-motion'; import { useRef, useCallback, useEffect } from 'react'; import PropTypes from 'prop-types'; import { channel as $channelContent } from '../../../content'; import { clsm } from '../../../utils'; import { createAnimationProps } from '../../../helpers/animationPropsHelper'; import { HAIRLINE_DIVIDER_CLASSES } from '../../../components/ProfileMenu/ProfileMenuTheme'; import { useModal } from '../../../contexts/Modal'; import { useResponsiveDevice } from '../../../contexts/ResponsiveDevice'; import { useUser } from '../../../contexts/User'; import Button from '../../../components/Button'; import ChatLine, { CHAT_LINE_VARIANT } from './Messages/ChatLine'; import useClickAway from '../../../hooks/useClickAway'; import usePreviousFocus from '../../../hooks/usePreviousFocus'; import withPortal from '../../../components/withPortal'; const $content = $channelContent.chat.popup; const $modalContent = $channelContent.chat.modal.ban_user_modal; const ChatPopup = ({ banUser, deleteMessage, isOpen, selectedMessage: { avatarSrc, color, displayName, message, channelArn }, setIsChatPopupOpen }) => { const { isMobileView } = useResponsiveDevice(); const { userData } = useUser(); const { username } = userData || {}; const { openModal } = useModal(); const popupRef = useRef(); const isOwnMessage = username === displayName; const showChatPopup = useCallback( () => setIsChatPopupOpen(true), [setIsChatPopupOpen] ); const hideChatPopup = useCallback( () => setIsChatPopupOpen(false), [setIsChatPopupOpen] ); const { refocus: handleClose } = usePreviousFocus({ isActive: isOpen, onRefocus: hideChatPopup }); const handleBanUser = () => { handleClose(); openModal({ content: { confirmText: $modalContent.confirm_ban_user, isDestructive: true, message: `${$modalContent.ban_user_message} ${displayName}?` }, onConfirm: async () => { await banUser(channelArn); handleClose(); }, onCancel: showChatPopup }); }; const handleCancelAndRefocus = (event) => handleClose(event, true); useClickAway([popupRef], () => setIsChatPopupOpen(false)); useEffect(() => { /** * When the chat popup is open and not currently visible on the screen, automatically scroll the chat popup into view. * Scroll treshhold value determines when the auto-scroll will trigger. We are triggering the scroll when at least a quarter of the popup is visible. */ if (!isOpen) return; const { top: popupTop, bottom: popupBottom, height: popupHeight } = popupRef.current.getBoundingClientRect(); const windowHeight = window.innerHeight; const scrollTreshhold = popupTop + popupHeight / 4; const isPopupInView = scrollTreshhold < windowHeight && popupBottom >= 0; if (!isPopupInView) popupRef.current.scrollIntoView({ behavior: 'smooth' }); }, [isOpen]); return (