tweb/src/components/confirmationPopup.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-12-11 17:37:08 +01:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import PopupElement, {addCancelButton} from './popups';
import PopupPeer, {PopupPeerCheckboxOptions, PopupPeerOptions} from './popups/peer';
2021-12-11 17:37:08 +01:00
// type PopupConfirmationOptions = Pick<PopupPeerOptions, 'titleLangKey'>;
2022-07-18 15:00:41 +02:00
export type PopupConfirmationOptions = PopupPeerOptions & {
2021-12-11 17:37:08 +01:00
button: PopupPeerOptions['buttons'][0],
checkbox?: PopupPeerOptions['checkboxes'][0]
};
export default function confirmationPopup<T extends PopupConfirmationOptions>(
options: T
): Promise<T['checkboxes'] extends PopupPeerCheckboxOptions[] ? Array<boolean> : (T['checkbox'] extends PopupPeerCheckboxOptions ? boolean : void)> {
return new Promise<any>((resolve, reject) => {
2021-12-11 17:37:08 +01:00
const {button, checkbox} = options;
button.callback = (set) => {
if(checkbox || !set) {
resolve(set ? !!set.size : undefined);
} else {
resolve(options.checkboxes.map((checkbox) => set.has(checkbox.text)));
}
2021-12-11 17:37:08 +01:00
};
2022-07-18 15:00:41 +02:00
const buttons = addCancelButton(options.buttons || [button]);
const cancelButton = buttons.find((button) => button.isCancel);
2021-12-11 17:37:08 +01:00
cancelButton.callback = () => {
reject();
};
options.buttons = buttons;
2022-07-18 15:00:41 +02:00
options.checkboxes ??= checkbox && [checkbox];
2021-12-11 17:37:08 +01:00
PopupElement.createPopup(PopupPeer, 'popup-confirmation', options).show();
2021-12-11 17:37:08 +01:00
});
}