mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import React, { useRef, useState } from 'react';
|
|
import ConfirmationDialog from './ConfirmationDialog';
|
|
import { useDialogState } from './useDialogState';
|
|
|
|
type ConfirmationDialogProps = React.ComponentProps<typeof ConfirmationDialog>;
|
|
|
|
type TriggerProps = {
|
|
onClick: () => void;
|
|
};
|
|
|
|
type ConfirmationDialogTriggerProps = Omit<ConfirmationDialogProps, 'open' | 'onClose' | 'onConfirmed'> & {
|
|
/** A returned promise keeps the dialog pending until it settles; rejections are swallowed. */
|
|
onConfirmed: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, close: () => void) => void | Promise<void>;
|
|
trigger: (props: TriggerProps) => React.ReactNode;
|
|
};
|
|
|
|
export default function ConfirmationDialogTrigger({
|
|
onConfirmed,
|
|
trigger,
|
|
pending,
|
|
...props
|
|
}: ConfirmationDialogTriggerProps) {
|
|
const dialog = useDialogState();
|
|
const confirmingRef = useRef(false);
|
|
const [confirming, setConfirming] = useState(false);
|
|
|
|
const onConfirm = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
if (confirmingRef.current || pending) {
|
|
return;
|
|
}
|
|
|
|
const result = onConfirmed(event, dialog.hide);
|
|
if (!(result instanceof Promise)) {
|
|
return;
|
|
}
|
|
|
|
confirmingRef.current = true;
|
|
setConfirming(true);
|
|
result
|
|
.catch(() => undefined)
|
|
.finally(() => {
|
|
confirmingRef.current = false;
|
|
setConfirming(false);
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
{trigger({ onClick: dialog.show })}
|
|
<ConfirmationDialog
|
|
{...props}
|
|
pending={pending || confirming}
|
|
open={dialog.open}
|
|
onClose={dialog.hide}
|
|
onConfirmed={onConfirm}
|
|
/>
|
|
</>
|
|
);
|
|
}
|