signal-desktop/ts/components/SafetyNumberViewer.tsx

123 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2023-11-01 22:55:30 +00:00
import React from 'react';
import { Button, ButtonVariant } from './Button';
import { QrCode } from './QrCode';
import type { ConversationType } from '../state/ducks/conversations';
2020-07-24 01:35:32 +00:00
import { Intl } from './Intl';
import { Emojify } from './conversation/Emojify';
import type { LocalizerType } from '../types/Util';
import type { SafetyNumberType } from '../types/safetyNumber';
2023-11-01 22:55:30 +00:00
import { SAFETY_NUMBER_URL } from '../types/support';
export type PropsType = {
2022-03-02 18:24:28 +00:00
contact: ConversationType;
generateSafetyNumber: (contact: ConversationType) => void;
i18n: LocalizerType;
2022-03-02 18:24:28 +00:00
onClose: () => void;
2023-11-01 22:55:30 +00:00
safetyNumber?: SafetyNumberType;
toggleVerified: (contact: ConversationType) => void;
verificationDisabled: boolean;
};
2022-11-18 00:45:19 +00:00
export function SafetyNumberViewer({
contact,
generateSafetyNumber,
i18n,
onClose,
2023-11-01 22:55:30 +00:00
safetyNumber,
toggleVerified,
verificationDisabled,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element | null {
2020-09-12 00:46:52 +00:00
React.useEffect(() => {
if (!contact) {
return;
}
generateSafetyNumber(contact);
}, [contact, generateSafetyNumber]);
// Keyboard navigation
2023-11-01 22:55:30 +00:00
if (!contact) {
return null;
}
2023-11-01 22:55:30 +00:00
if (!safetyNumber) {
return (
<div className="module-SafetyNumberViewer">
2023-03-30 00:03:25 +00:00
<div>{i18n('icu:cannotGenerateSafetyNumber')}</div>
2022-03-02 18:24:28 +00:00
<div className="module-SafetyNumberViewer__buttons">
<Button
className="module-SafetyNumberViewer__button"
onClick={() => onClose?.()}
variant={ButtonVariant.Primary}
>
2023-03-30 00:03:25 +00:00
{i18n('icu:ok')}
2022-03-02 18:24:28 +00:00
</Button>
</div>
</div>
);
}
const boldName = (
<span className="module-SafetyNumberViewer__bold-name">
<Emojify text={contact.title} />
</span>
2020-07-24 01:35:32 +00:00
);
2020-09-12 00:46:52 +00:00
const { isVerified } = contact;
const verifyButtonText = isVerified
? i18n('icu:SafetyNumberViewer__clearVerification')
: i18n('icu:SafetyNumberViewer__markAsVerified');
2023-11-01 22:55:30 +00:00
const numberBlocks = safetyNumber.numberBlocks.join(' ');
const safetyNumberCard = (
<div className="module-SafetyNumberViewer__card-container">
2023-11-01 22:55:30 +00:00
<div className="module-SafetyNumberViewer__card">
<QrCode
className="module-SafetyNumberViewer__card__qr"
2023-11-01 22:55:30 +00:00
data={safetyNumber.qrData}
alt={i18n('icu:Install__scan-this-code')}
/>
<div className="module-SafetyNumberViewer__card__number">
{numberBlocks}
</div>
</div>
</div>
);
return (
<div className="module-SafetyNumberViewer">
{safetyNumberCard}
<div className="module-SafetyNumberViewer__help">
<Intl
i18n={i18n}
2023-11-01 22:55:30 +00:00
id="icu:SafetyNumberViewer__hint"
components={{ name: boldName }}
/>
<br />
2023-11-01 22:55:30 +00:00
<a href={SAFETY_NUMBER_URL} rel="noreferrer" target="_blank">
<Intl i18n={i18n} id="icu:SafetyNumberViewer__learn_more" />
</a>
</div>
<div className="module-SafetyNumberViewer__button">
<Button
disabled={verificationDisabled}
onClick={() => {
toggleVerified(contact);
}}
variant={ButtonVariant.Secondary}
>
{verifyButtonText}
</Button>
</div>
</div>
);
2022-11-18 00:45:19 +00:00
}