2021-04-30 19:40:25 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-11-11 17:36:05 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, { ReactPortal } from 'react';
|
|
|
|
import { createPortal } from 'react-dom';
|
|
|
|
|
|
|
|
import { ConversationType } from '../../state/ducks/conversations';
|
2021-01-26 01:01:19 +00:00
|
|
|
import { About } from './About';
|
2020-11-11 17:36:05 +00:00
|
|
|
import { Avatar } from '../Avatar';
|
2021-04-16 15:51:26 +00:00
|
|
|
import { SharedGroupNames } from '../SharedGroupNames';
|
2020-11-11 17:36:05 +00:00
|
|
|
import { LocalizerType } from '../../types/Util';
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
areWeAdmin: boolean;
|
|
|
|
contact?: ConversationType;
|
|
|
|
readonly i18n: LocalizerType;
|
2021-01-29 21:19:24 +00:00
|
|
|
isAdmin: boolean;
|
2020-11-11 17:36:05 +00:00
|
|
|
isMember: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
openConversation: (conversationId: string) => void;
|
|
|
|
removeMember: (conversationId: string) => void;
|
|
|
|
showSafetyNumber: (conversationId: string) => void;
|
2021-01-29 21:19:24 +00:00
|
|
|
toggleAdmin: (conversationId: string) => void;
|
2021-05-21 17:27:28 +00:00
|
|
|
updateSharedGroups: () => void;
|
2020-11-11 17:36:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ContactModal = ({
|
|
|
|
areWeAdmin,
|
|
|
|
contact,
|
|
|
|
i18n,
|
2021-01-29 21:19:24 +00:00
|
|
|
isAdmin,
|
2020-11-11 17:36:05 +00:00
|
|
|
isMember,
|
|
|
|
onClose,
|
|
|
|
openConversation,
|
|
|
|
removeMember,
|
|
|
|
showSafetyNumber,
|
2021-01-29 21:19:24 +00:00
|
|
|
toggleAdmin,
|
2021-05-21 17:27:28 +00:00
|
|
|
updateSharedGroups,
|
2020-11-11 17:36:05 +00:00
|
|
|
}: PropsType): ReactPortal | null => {
|
|
|
|
if (!contact) {
|
|
|
|
throw new Error('Contact modal opened without a matching contact');
|
|
|
|
}
|
|
|
|
|
|
|
|
const [root, setRoot] = React.useState<HTMLElement | null>(null);
|
|
|
|
const overlayRef = React.useRef<HTMLElement | null>(null);
|
|
|
|
const closeButtonRef = React.useRef<HTMLElement | null>(null);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
document.body.appendChild(div);
|
|
|
|
setRoot(div);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.body.removeChild(div);
|
|
|
|
setRoot(null);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2021-05-21 17:27:28 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
// Kick off the expensive hydration of the current sharedGroupNames
|
|
|
|
updateSharedGroups();
|
|
|
|
}, [updateSharedGroups]);
|
|
|
|
|
2020-11-11 17:36:05 +00:00
|
|
|
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<HTMLElement>) => {
|
|
|
|
if (e.target === overlayRef.current) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return root
|
|
|
|
? createPortal(
|
|
|
|
<div
|
|
|
|
ref={ref => {
|
|
|
|
overlayRef.current = ref;
|
|
|
|
}}
|
|
|
|
role="presentation"
|
|
|
|
className="module-contact-modal__overlay"
|
|
|
|
onClick={onClickOverlay}
|
|
|
|
>
|
|
|
|
<div className="module-contact-modal">
|
|
|
|
<button
|
|
|
|
ref={r => {
|
|
|
|
closeButtonRef.current = r;
|
|
|
|
}}
|
|
|
|
type="button"
|
|
|
|
className="module-contact-modal__close-button"
|
|
|
|
onClick={onClose}
|
|
|
|
aria-label={i18n('close')}
|
|
|
|
/>
|
|
|
|
<Avatar
|
2021-04-30 19:40:25 +00:00
|
|
|
acceptedMessageRequest={contact.acceptedMessageRequest}
|
2020-11-11 17:36:05 +00:00
|
|
|
avatarPath={contact.avatarPath}
|
|
|
|
color={contact.color}
|
|
|
|
conversationType="direct"
|
|
|
|
i18n={i18n}
|
2021-05-07 22:21:10 +00:00
|
|
|
isMe={contact.isMe}
|
2020-11-11 17:36:05 +00:00
|
|
|
name={contact.name}
|
|
|
|
profileName={contact.profileName}
|
2021-04-30 19:40:25 +00:00
|
|
|
sharedGroupNames={contact.sharedGroupNames}
|
2020-11-11 17:36:05 +00:00
|
|
|
size={96}
|
|
|
|
title={contact.title}
|
2021-04-30 19:40:25 +00:00
|
|
|
unblurredAvatarPath={contact.unblurredAvatarPath}
|
2020-11-11 17:36:05 +00:00
|
|
|
/>
|
|
|
|
<div className="module-contact-modal__name">{contact.title}</div>
|
2021-01-26 01:01:19 +00:00
|
|
|
<div className="module-about__container">
|
|
|
|
<About text={contact.about} />
|
|
|
|
</div>
|
2020-11-11 17:36:05 +00:00
|
|
|
{contact.phoneNumber && (
|
2021-04-16 15:51:26 +00:00
|
|
|
<div className="module-contact-modal__info">
|
2020-11-11 17:36:05 +00:00
|
|
|
{contact.phoneNumber}
|
|
|
|
</div>
|
|
|
|
)}
|
2021-04-16 15:51:26 +00:00
|
|
|
<div className="module-contact-modal__info">
|
|
|
|
<SharedGroupNames
|
|
|
|
i18n={i18n}
|
|
|
|
sharedGroupNames={contact.sharedGroupNames || []}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-11-11 17:36:05 +00:00
|
|
|
<div className="module-contact-modal__button-container">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="module-contact-modal__button module-contact-modal__send-message"
|
|
|
|
onClick={() => openConversation(contact.id)}
|
|
|
|
>
|
|
|
|
<div className="module-contact-modal__bubble-icon">
|
|
|
|
<div className="module-contact-modal__send-message__bubble-icon" />
|
|
|
|
</div>
|
|
|
|
<span>{i18n('ContactModal--message')}</span>
|
|
|
|
</button>
|
|
|
|
{!contact.isMe && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="module-contact-modal__button module-contact-modal__safety-number"
|
|
|
|
onClick={() => showSafetyNumber(contact.id)}
|
|
|
|
>
|
|
|
|
<div className="module-contact-modal__bubble-icon">
|
|
|
|
<div className="module-contact-modal__safety-number__bubble-icon" />
|
|
|
|
</div>
|
|
|
|
<span>{i18n('showSafetyNumber')}</span>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{!contact.isMe && areWeAdmin && isMember && (
|
2021-01-29 21:19:24 +00:00
|
|
|
<>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="module-contact-modal__button module-contact-modal__make-admin"
|
|
|
|
onClick={() => toggleAdmin(contact.id)}
|
|
|
|
>
|
|
|
|
<div className="module-contact-modal__bubble-icon">
|
|
|
|
<div className="module-contact-modal__make-admin__bubble-icon" />
|
|
|
|
</div>
|
|
|
|
{isAdmin ? (
|
|
|
|
<span>{i18n('ContactModal--rm-admin')}</span>
|
|
|
|
) : (
|
|
|
|
<span>{i18n('ContactModal--make-admin')}</span>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="module-contact-modal__button module-contact-modal__remove-from-group"
|
|
|
|
onClick={() => removeMember(contact.id)}
|
|
|
|
>
|
|
|
|
<div className="module-contact-modal__bubble-icon">
|
|
|
|
<div className="module-contact-modal__remove-from-group__bubble-icon" />
|
|
|
|
</div>
|
|
|
|
<span>{i18n('ContactModal--remove-from-group')}</span>
|
|
|
|
</button>
|
|
|
|
</>
|
2020-11-11 17:36:05 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>,
|
|
|
|
root
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
};
|