2024-03-11 17:02:15 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2024-08-02 15:51:49 +02:00
|
|
|
import { Alert } from 'react-native';
|
2024-11-16 23:09:50 +02:00
|
|
|
import { DialogControl } from '../components/DialogManager';
|
2024-08-02 15:51:49 +02:00
|
|
|
import { RefObject } from 'react';
|
2024-11-16 23:09:50 +02:00
|
|
|
import { MessageBoxType, ShowMessageBoxOptions } from '@joplin/lib/shim';
|
|
|
|
import { PromptButton } from '../components/DialogManager/types';
|
2024-03-11 17:02:15 +02:00
|
|
|
|
|
|
|
|
2024-11-16 23:09:50 +02:00
|
|
|
const makeShowMessageBox = (dialogControl: null|RefObject<DialogControl>) => (message: string, options: ShowMessageBoxOptions = null) => {
|
2024-03-11 17:02:15 +02:00
|
|
|
return new Promise<number>(resolve => {
|
2024-11-16 23:09:50 +02:00
|
|
|
const okButton: PromptButton = {
|
|
|
|
text: _('OK'),
|
|
|
|
onPress: () => resolve(0),
|
|
|
|
};
|
|
|
|
const cancelButton: PromptButton = {
|
|
|
|
text: _('Cancel'),
|
|
|
|
onPress: () => resolve(1),
|
|
|
|
style: 'cancel',
|
|
|
|
};
|
|
|
|
const defaultConfirmButtons = [okButton, cancelButton];
|
|
|
|
const defaultAlertButtons = [okButton];
|
2024-03-11 17:02:15 +02:00
|
|
|
|
2024-11-16 23:09:50 +02:00
|
|
|
const dialogType = options.type ?? MessageBoxType.Confirm;
|
|
|
|
let buttons = dialogType === MessageBoxType.Confirm ? defaultConfirmButtons : defaultAlertButtons;
|
2024-03-11 17:02:15 +02:00
|
|
|
if (options?.buttons) {
|
|
|
|
buttons = options.buttons.map((text, index) => {
|
|
|
|
return {
|
|
|
|
text,
|
|
|
|
onPress: () => resolve(index),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
// Web doesn't support Alert.alert -- prefer using the global dialogControl if available.
|
|
|
|
(dialogControl?.current?.prompt ?? Alert.alert)(
|
2024-03-11 17:02:15 +02:00
|
|
|
options?.title ?? '',
|
|
|
|
message,
|
|
|
|
buttons,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
2024-08-02 15:51:49 +02:00
|
|
|
export default makeShowMessageBox;
|