signal-desktop/ts/util/showConfirmationDialog.tsx

88 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2015 Signal Messenger, LLC
// 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';
type ConfirmationDialogViewProps = {
onTopOfEverything?: boolean;
2022-09-27 20:24:21 +00:00
dialogName: string;
cancelText?: string;
confirmStyle?: 'affirmative' | 'negative';
title: string;
description?: string;
okText: string;
2024-02-13 21:48:09 +00:00
noMouseClose?: boolean;
reject?: (error: Error) => void;
resolve: () => void;
};
2021-06-17 21:15:09 +00:00
let confirmationDialogViewNode: HTMLElement | undefined;
let confirmationDialogPreviousFocus: HTMLElement | undefined;
function removeConfirmationDialog() {
if (!confirmationDialogViewNode) {
return;
}
2024-02-13 21:48:09 +00:00
window.reduxActions?.globalModals.toggleConfirmationModal(false);
2023-01-13 00:24:59 +00:00
unmountComponentAtNode(confirmationDialogViewNode);
document.body.removeChild(confirmationDialogViewNode);
if (
confirmationDialogPreviousFocus &&
typeof confirmationDialogPreviousFocus.focus === 'function'
) {
confirmationDialogPreviousFocus.focus();
}
2021-06-17 21:15:09 +00:00
confirmationDialogViewNode = undefined;
}
2023-01-13 00:24:59 +00:00
export function showConfirmationDialog(
options: ConfirmationDialogViewProps
): void {
if (confirmationDialogViewNode) {
removeConfirmationDialog();
}
2024-02-13 21:48:09 +00:00
window.reduxActions?.globalModals.toggleConfirmationModal(true);
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}
onTopOfEverything={options.onTopOfEverything}
actions={[
{
action: () => {
options.resolve();
},
style: options.confirmStyle,
2023-03-30 00:03:25 +00:00
text: options.okText || window.i18n('icu:ok'),
},
]}
2023-03-30 00:03:25 +00:00
cancelText={options.cancelText || window.i18n('icu:cancel')}
i18n={window.i18n}
onCancel={() => {
if (options.reject) {
options.reject(new Error('showConfirmationDialog: onCancel called'));
}
}}
onClose={() => {
removeConfirmationDialog();
}}
title={options.title}
2024-02-13 21:48:09 +00:00
noMouseClose={options.noMouseClose}
>
{options.description}
</ConfirmationDialog>,
confirmationDialogViewNode
);
}