signal-desktop/ts/components/SafetyNumberChangeDialog.tsx

150 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-05-07 22:21:10 +00:00
// Copyright 2020-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import * as React from 'react';
import { noop } from 'lodash';
2020-07-24 01:35:32 +00:00
import { Avatar } from './Avatar';
import { ConfirmationDialog } from './ConfirmationDialog';
2020-07-24 01:35:32 +00:00
import { InContactsIcon } from './InContactsIcon';
import { Modal } from './Modal';
2020-07-24 01:35:32 +00:00
import type { ConversationType } from '../state/ducks/conversations';
import type { PreferredBadgeSelectorType } from '../state/selectors/badges';
import type { LocalizerType, ThemeType } from '../types/Util';
import { isInSystemContacts } from '../util/isInSystemContacts';
export enum SafetyNumberChangeSource {
Calling = 'Calling',
MessageSend = 'MessageSend',
}
export type SafetyNumberProps = {
contactID: string;
2022-03-02 18:24:28 +00:00
onClose: () => void;
};
export type Props = {
readonly confirmText?: string;
readonly contacts: Array<ConversationType>;
readonly getPreferredBadge: PreferredBadgeSelectorType;
readonly i18n: LocalizerType;
readonly onCancel: () => void;
readonly onConfirm: () => void;
readonly renderSafetyNumber: (props: SafetyNumberProps) => JSX.Element;
readonly theme: ThemeType;
};
export const SafetyNumberChangeDialog = ({
confirmText,
contacts,
getPreferredBadge,
i18n,
onCancel,
onConfirm,
renderSafetyNumber,
theme,
}: Props): JSX.Element => {
const [selectedContact, setSelectedContact] = React.useState<
ConversationType | undefined
>(undefined);
const cancelButtonRef = React.createRef<HTMLButtonElement>();
React.useEffect(() => {
if (cancelButtonRef && cancelButtonRef.current) {
cancelButtonRef.current.focus();
}
2020-09-12 00:46:52 +00:00
}, [cancelButtonRef, contacts]);
const onClose = selectedContact
? () => {
setSelectedContact(undefined);
}
: onCancel;
if (selectedContact) {
return (
2022-03-02 18:24:28 +00:00
<Modal hasXButton i18n={i18n} onClose={onClose}>
{renderSafetyNumber({ contactID: selectedContact.id, onClose })}
</Modal>
);
}
return (
<ConfirmationDialog
actions={[
{
action: onConfirm,
text: confirmText || i18n('sendMessageToContact'),
style: 'affirmative',
},
]}
i18n={i18n}
noMouseClose
onCancel={onClose}
onClose={noop}
title={i18n('safetyNumberChanges')}
>
<div className="module-SafetyNumberChangeDialog__message">
{i18n('changedVerificationWarning')}
</div>
<ul className="module-SafetyNumberChangeDialog__contacts">
2020-07-24 01:35:32 +00:00
{contacts.map((contact: ConversationType) => {
const shouldShowNumber = Boolean(contact.name || contact.profileName);
return (
<li
className="module-SafetyNumberChangeDialog__contact"
key={contact.id}
>
2020-07-24 01:35:32 +00:00
<Avatar
acceptedMessageRequest={contact.acceptedMessageRequest}
2020-07-24 01:35:32 +00:00
avatarPath={contact.avatarPath}
badge={getPreferredBadge(contact.badges)}
2020-07-24 01:35:32 +00:00
color={contact.color}
conversationType="direct"
i18n={i18n}
2021-05-07 22:21:10 +00:00
isMe={contact.isMe}
2020-07-24 01:35:32 +00:00
name={contact.name}
phoneNumber={contact.phoneNumber}
profileName={contact.profileName}
theme={theme}
2020-07-24 01:35:32 +00:00
title={contact.title}
sharedGroupNames={contact.sharedGroupNames}
2020-07-24 01:35:32 +00:00
size={52}
unblurredAvatarPath={contact.unblurredAvatarPath}
2020-07-24 01:35:32 +00:00
/>
<div className="module-SafetyNumberChangeDialog__contact--wrapper">
<div className="module-SafetyNumberChangeDialog__contact--name">
2020-07-24 01:35:32 +00:00
{contact.title}
{isInSystemContacts(contact) ? (
2020-07-24 01:35:32 +00:00
<span>
{' '}
<InContactsIcon i18n={i18n} />
</span>
) : null}
</div>
{shouldShowNumber ? (
<div className="module-SafetyNumberChangeDialog__contact--number">
{contact.phoneNumber}
</div>
2020-07-24 01:35:32 +00:00
) : null}
</div>
<button
className="module-SafetyNumberChangeDialog__contact--view"
2020-07-24 01:35:32 +00:00
onClick={() => {
setSelectedContact(contact);
2020-07-24 01:35:32 +00:00
}}
tabIndex={0}
2020-09-12 00:46:52 +00:00
type="button"
2020-07-24 01:35:32 +00:00
>
{i18n('view')}
</button>
</li>
);
})}
</ul>
</ConfirmationDialog>
);
};