2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-26 00:08:58 +00:00
|
|
|
import React from 'react';
|
2021-10-06 20:27:14 +00:00
|
|
|
import { Button, ButtonVariant } from './Button';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
2020-07-24 01:35:32 +00:00
|
|
|
import { Intl } from './Intl';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2020-10-07 00:49:22 +00:00
|
|
|
export type PropsType = {
|
2022-03-02 18:24:28 +00:00
|
|
|
contact: ConversationType;
|
2020-06-26 00:08:58 +00:00
|
|
|
generateSafetyNumber: (contact: ConversationType) => void;
|
|
|
|
i18n: LocalizerType;
|
2022-03-02 18:24:28 +00:00
|
|
|
onClose: () => void;
|
2020-06-26 00:08:58 +00:00
|
|
|
safetyNumber: string;
|
|
|
|
toggleVerified: (contact: ConversationType) => void;
|
|
|
|
verificationDisabled: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SafetyNumberViewer = ({
|
|
|
|
contact,
|
|
|
|
generateSafetyNumber,
|
|
|
|
i18n,
|
|
|
|
onClose,
|
|
|
|
safetyNumber,
|
|
|
|
toggleVerified,
|
|
|
|
verificationDisabled,
|
2020-10-07 00:49:22 +00:00
|
|
|
}: PropsType): JSX.Element | null => {
|
2020-09-12 00:46:52 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!contact) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateSafetyNumber(contact);
|
|
|
|
}, [contact, generateSafetyNumber, safetyNumber]);
|
|
|
|
|
2020-06-26 00:08:58 +00:00
|
|
|
if (!contact) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-07 00:49:22 +00:00
|
|
|
if (!contact.phoneNumber) {
|
|
|
|
return (
|
2021-04-27 19:29:59 +00:00
|
|
|
<div className="module-SafetyNumberViewer">
|
2022-03-02 18:24:28 +00:00
|
|
|
<div>{i18n('cannotGenerateSafetyNumber')}</div>
|
|
|
|
<div className="module-SafetyNumberViewer__buttons">
|
|
|
|
<Button
|
|
|
|
className="module-SafetyNumberViewer__button"
|
|
|
|
onClick={() => onClose?.()}
|
|
|
|
variant={ButtonVariant.Primary}
|
|
|
|
>
|
|
|
|
{i18n('ok')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2020-10-07 00:49:22 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-24 01:35:32 +00:00
|
|
|
const showNumber = Boolean(contact.name || contact.profileName);
|
2020-10-07 00:49:22 +00:00
|
|
|
const numberFragment =
|
|
|
|
showNumber && contact.phoneNumber ? ` · ${contact.phoneNumber}` : '';
|
2020-07-24 01:35:32 +00:00
|
|
|
const name = `${contact.title}${numberFragment}`;
|
2020-07-29 23:20:05 +00:00
|
|
|
const boldName = (
|
2021-04-27 19:29:59 +00:00
|
|
|
<span className="module-SafetyNumberViewer__bold-name">{name}</span>
|
2020-07-24 01:35:32 +00:00
|
|
|
);
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
const { isVerified } = contact;
|
2020-07-24 01:35:32 +00:00
|
|
|
const verifiedStatusKey = isVerified ? 'isVerified' : 'isNotVerified';
|
2020-06-26 00:08:58 +00:00
|
|
|
const verifyButtonText = isVerified ? i18n('unverify') : i18n('verify');
|
|
|
|
|
|
|
|
return (
|
2021-04-27 19:29:59 +00:00
|
|
|
<div className="module-SafetyNumberViewer">
|
|
|
|
<div className="module-SafetyNumberViewer__number">
|
2020-06-26 00:08:58 +00:00
|
|
|
{safetyNumber || getPlaceholder()}
|
|
|
|
</div>
|
2020-07-29 23:20:05 +00:00
|
|
|
<Intl i18n={i18n} id="verifyHelp" components={[boldName]} />
|
2021-04-27 19:29:59 +00:00
|
|
|
<div className="module-SafetyNumberViewer__verification-status">
|
2020-06-26 00:08:58 +00:00
|
|
|
{isVerified ? (
|
2021-04-27 19:29:59 +00:00
|
|
|
<span className="module-SafetyNumberViewer__icon--verified" />
|
2020-06-26 00:08:58 +00:00
|
|
|
) : (
|
2021-04-27 19:29:59 +00:00
|
|
|
<span className="module-SafetyNumberViewer__icon--shield" />
|
2020-06-26 00:08:58 +00:00
|
|
|
)}
|
2020-07-29 23:20:05 +00:00
|
|
|
<Intl i18n={i18n} id={verifiedStatusKey} components={[boldName]} />
|
2020-06-26 00:08:58 +00:00
|
|
|
</div>
|
2021-10-06 20:27:14 +00:00
|
|
|
<div className="module-SafetyNumberViewer__button">
|
|
|
|
<Button
|
2020-06-26 00:08:58 +00:00
|
|
|
disabled={verificationDisabled}
|
|
|
|
onClick={() => {
|
|
|
|
toggleVerified(contact);
|
|
|
|
}}
|
2021-10-06 20:27:14 +00:00
|
|
|
variant={ButtonVariant.Secondary}
|
2020-06-26 00:08:58 +00:00
|
|
|
>
|
|
|
|
{verifyButtonText}
|
2021-10-06 20:27:14 +00:00
|
|
|
</Button>
|
2020-06-26 00:08:58 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2021-02-24 19:07:05 +00:00
|
|
|
|
|
|
|
function getPlaceholder(): string {
|
|
|
|
return Array.from(Array(12))
|
|
|
|
.map(() => 'XXXXX')
|
|
|
|
.join(' ');
|
|
|
|
}
|