signal-desktop/ts/components/SafetyNumberModal.tsx

86 lines
2.5 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useState, useCallback } from 'react';
import { SafetyNumberMode } from '../types/safetyNumber';
import { isSafetyNumberNotAvailable } from '../util/isSafetyNumberNotAvailable';
import { Modal } from './Modal';
import type { PropsType as SafetyNumberViewerPropsType } from './SafetyNumberViewer';
import { SafetyNumberViewer } from './SafetyNumberViewer';
import { SafetyNumberOnboarding } from './SafetyNumberOnboarding';
import { SafetyNumberNotReady } from './SafetyNumberNotReady';
type PropsType = {
toggleSafetyNumberModal: () => unknown;
hasCompletedSafetyNumberOnboarding: boolean;
markHasCompletedSafetyNumberOnboarding: () => unknown;
2022-03-02 18:24:28 +00:00
} & Omit<SafetyNumberViewerPropsType, 'onClose'>;
2022-11-18 00:45:19 +00:00
export function SafetyNumberModal({
i18n,
toggleSafetyNumberModal,
hasCompletedSafetyNumberOnboarding,
markHasCompletedSafetyNumberOnboarding,
...safetyNumberViewerProps
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element | null {
const { contact, safetyNumberMode } = safetyNumberViewerProps;
const [isOnboarding, setIsOnboarding] = useState(
2023-07-18 23:40:14 +00:00
safetyNumberMode !== SafetyNumberMode.JustE164 &&
!hasCompletedSafetyNumberOnboarding
);
const showOnboarding = useCallback(() => {
setIsOnboarding(true);
}, [setIsOnboarding]);
const hideOnboarding = useCallback(() => {
setIsOnboarding(false);
markHasCompletedSafetyNumberOnboarding();
}, [setIsOnboarding, markHasCompletedSafetyNumberOnboarding]);
2023-07-18 23:40:14 +00:00
const missingRequiredE164 =
safetyNumberMode !== SafetyNumberMode.DefaultACIAndMaybeE164 &&
!contact.e164;
let title: string | undefined;
let content: JSX.Element;
let hasXButton = true;
2023-07-18 23:40:14 +00:00
if (missingRequiredE164 || isSafetyNumberNotAvailable(contact)) {
content = (
<SafetyNumberNotReady
i18n={i18n}
onClose={() => toggleSafetyNumberModal()}
/>
);
hasXButton = false;
} else if (isOnboarding) {
content = <SafetyNumberOnboarding i18n={i18n} onClose={hideOnboarding} />;
} else {
title = i18n('icu:SafetyNumberModal__title');
content = (
<SafetyNumberViewer
i18n={i18n}
onClose={toggleSafetyNumberModal}
showOnboarding={showOnboarding}
{...safetyNumberViewerProps}
/>
);
}
return (
<Modal
2022-09-27 20:24:21 +00:00
modalName="SafetyNumberModal"
hasXButton={hasXButton}
i18n={i18n}
moduleClassName="module-SafetyNumberViewer__modal"
2022-03-02 18:24:28 +00:00
onClose={toggleSafetyNumberModal}
title={title}
>
{content}
</Modal>
);
2022-11-18 00:45:19 +00:00
}