2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2015 Signal Messenger, LLC
|
2021-01-04 18:47:14 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-01-13 00:24:59 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { render, unmountComponentAtNode } from 'react-dom';
|
|
|
|
import { ConfirmationDialog } from '../components/ConfirmationDialog';
|
2021-01-04 18:47:14 +00:00
|
|
|
|
|
|
|
type ConfirmationDialogViewProps = {
|
2021-12-03 23:04:34 +00:00
|
|
|
onTopOfEverything?: boolean;
|
2022-09-27 20:24:21 +00:00
|
|
|
dialogName: string;
|
2021-01-04 18:47:14 +00:00
|
|
|
cancelText?: string;
|
|
|
|
confirmStyle?: 'affirmative' | 'negative';
|
2023-03-24 21:16:48 +00:00
|
|
|
title: string;
|
|
|
|
description?: string;
|
2021-01-04 18:47:14 +00:00
|
|
|
okText: string;
|
2024-02-13 21:48:09 +00:00
|
|
|
noMouseClose?: boolean;
|
2021-02-26 21:06:37 +00:00
|
|
|
reject?: (error: Error) => void;
|
2021-01-04 18:47:14 +00:00
|
|
|
resolve: () => void;
|
|
|
|
};
|
|
|
|
|
2021-06-17 21:15:09 +00:00
|
|
|
let confirmationDialogViewNode: HTMLElement | undefined;
|
|
|
|
let confirmationDialogPreviousFocus: HTMLElement | undefined;
|
2021-01-04 18:47:14 +00:00
|
|
|
|
|
|
|
function removeConfirmationDialog() {
|
|
|
|
if (!confirmationDialogViewNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:48:09 +00:00
|
|
|
window.reduxActions?.globalModals.toggleConfirmationModal(false);
|
2023-03-24 21:16:48 +00:00
|
|
|
|
2023-01-13 00:24:59 +00:00
|
|
|
unmountComponentAtNode(confirmationDialogViewNode);
|
2021-01-04 18:47:14 +00:00
|
|
|
document.body.removeChild(confirmationDialogViewNode);
|
|
|
|
|
|
|
|
if (
|
|
|
|
confirmationDialogPreviousFocus &&
|
|
|
|
typeof confirmationDialogPreviousFocus.focus === 'function'
|
|
|
|
) {
|
|
|
|
confirmationDialogPreviousFocus.focus();
|
|
|
|
}
|
2021-06-17 21:15:09 +00:00
|
|
|
confirmationDialogViewNode = undefined;
|
2021-01-04 18:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 00:24:59 +00:00
|
|
|
export function showConfirmationDialog(
|
|
|
|
options: ConfirmationDialogViewProps
|
|
|
|
): void {
|
2021-01-04 18:47:14 +00:00
|
|
|
if (confirmationDialogViewNode) {
|
|
|
|
removeConfirmationDialog();
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:48:09 +00:00
|
|
|
window.reduxActions?.globalModals.toggleConfirmationModal(true);
|
2023-03-24 21:16:48 +00:00
|
|
|
|
2021-01-04 18:47:14 +00:00
|
|
|
confirmationDialogViewNode = document.createElement('div');
|
|
|
|
document.body.appendChild(confirmationDialogViewNode);
|
|
|
|
|
|
|
|
confirmationDialogPreviousFocus = document.activeElement as HTMLElement;
|
|
|
|
|
2023-01-13 00:24:59 +00:00
|
|
|
render(
|
|
|
|
<ConfirmationDialog
|
2022-09-27 20:24:21 +00:00
|
|
|
dialogName={options.dialogName}
|
2021-12-03 23:04:34 +00:00
|
|
|
onTopOfEverything={options.onTopOfEverything}
|
2021-01-04 18:47:14 +00:00
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
action: () => {
|
|
|
|
options.resolve();
|
|
|
|
},
|
|
|
|
style: options.confirmStyle,
|
2023-03-30 00:03:25 +00:00
|
|
|
text: options.okText || window.i18n('icu:ok'),
|
2021-01-04 18:47:14 +00:00
|
|
|
},
|
|
|
|
]}
|
2023-03-30 00:03:25 +00:00
|
|
|
cancelText={options.cancelText || window.i18n('icu:cancel')}
|
2021-01-04 18:47:14 +00:00
|
|
|
i18n={window.i18n}
|
2021-04-27 19:29:59 +00:00
|
|
|
onCancel={() => {
|
2021-01-04 18:47:14 +00:00
|
|
|
if (options.reject) {
|
2021-04-27 19:29:59 +00:00
|
|
|
options.reject(new Error('showConfirmationDialog: onCancel called'));
|
2021-01-04 18:47:14 +00:00
|
|
|
}
|
|
|
|
}}
|
2021-04-27 19:29:59 +00:00
|
|
|
onClose={() => {
|
|
|
|
removeConfirmationDialog();
|
|
|
|
}}
|
2023-03-24 21:16:48 +00:00
|
|
|
title={options.title}
|
2024-02-13 21:48:09 +00:00
|
|
|
noMouseClose={options.noMouseClose}
|
2023-03-24 21:16:48 +00:00
|
|
|
>
|
|
|
|
{options.description}
|
|
|
|
</ConfirmationDialog>,
|
2021-01-04 18:47:14 +00:00
|
|
|
confirmationDialogViewNode
|
|
|
|
);
|
|
|
|
}
|