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