// Copyright 2020-2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { ReactPortal } from 'react'; import { createPortal } from 'react-dom'; import { ConversationType } from '../../state/ducks/conversations'; import { About } from './About'; import { Avatar } from '../Avatar'; import { SharedGroupNames } from '../SharedGroupNames'; import { LocalizerType } from '../../types/Util'; export type PropsType = { areWeAdmin: boolean; contact?: ConversationType; readonly i18n: LocalizerType; isAdmin: boolean; isMember: boolean; onClose: () => void; openConversation: (conversationId: string) => void; removeMember: (conversationId: string) => void; showSafetyNumber: (conversationId: string) => void; toggleAdmin: (conversationId: string) => void; updateSharedGroups: () => void; }; export const ContactModal = ({ areWeAdmin, contact, i18n, isAdmin, isMember, onClose, openConversation, removeMember, showSafetyNumber, toggleAdmin, updateSharedGroups, }: PropsType): ReactPortal | null => { if (!contact) { throw new Error('Contact modal opened without a matching contact'); } const [root, setRoot] = React.useState(null); const overlayRef = React.useRef(null); const closeButtonRef = React.useRef(null); React.useEffect(() => { const div = document.createElement('div'); document.body.appendChild(div); setRoot(div); return () => { document.body.removeChild(div); setRoot(null); }; }, []); React.useEffect(() => { // Kick off the expensive hydration of the current sharedGroupNames updateSharedGroups(); }, [updateSharedGroups]); React.useEffect(() => { if (root !== null && closeButtonRef.current) { closeButtonRef.current.focus(); } }, [root]); React.useEffect(() => { const handler = (event: KeyboardEvent) => { if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); onClose(); } }; document.addEventListener('keyup', handler); return () => { document.removeEventListener('keyup', handler); }; }, [onClose]); const onClickOverlay = (e: React.MouseEvent) => { if (e.target === overlayRef.current) { e.preventDefault(); e.stopPropagation(); onClose(); } }; return root ? createPortal(
{ overlayRef.current = ref; }} role="presentation" className="module-contact-modal__overlay" onClick={onClickOverlay} >
{!contact.isMe && ( )} {!contact.isMe && areWeAdmin && isMember && ( <> )}
, root ) : null; };