mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-24 23:38:57 +01:00
98 lines
2.5 KiB
TypeScript
98 lines
2.5 KiB
TypeScript
import { Alert as RNAlert, Platform, ToastAndroid } from 'react-native';
|
|
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
|
|
import loc from '../loc';
|
|
|
|
export enum AlertType {
|
|
Alert,
|
|
Toast,
|
|
}
|
|
|
|
interface AlertButton {
|
|
text: string;
|
|
onPress?: () => void;
|
|
style?: 'default' | 'cancel' | 'destructive';
|
|
}
|
|
|
|
interface AlertOptions {
|
|
cancelable?: boolean;
|
|
}
|
|
|
|
const presentAlert = (() => {
|
|
let lastAlertParams: {
|
|
title?: string;
|
|
message: string;
|
|
type?: AlertType;
|
|
hapticFeedback?: HapticFeedbackTypes;
|
|
buttons?: AlertButton[];
|
|
options?: AlertOptions;
|
|
} | null = null;
|
|
|
|
const clearCache = () => {
|
|
lastAlertParams = null;
|
|
};
|
|
|
|
return ({
|
|
title,
|
|
message,
|
|
type = AlertType.Alert,
|
|
hapticFeedback,
|
|
buttons = [],
|
|
options = { cancelable: false },
|
|
forceClearCache = false,
|
|
}: {
|
|
title?: string;
|
|
message: string;
|
|
type?: AlertType;
|
|
hapticFeedback?: HapticFeedbackTypes;
|
|
buttons?: AlertButton[];
|
|
options?: AlertOptions;
|
|
forceClearCache?: boolean;
|
|
}) => {
|
|
if (
|
|
!forceClearCache &&
|
|
lastAlertParams &&
|
|
lastAlertParams.title === title &&
|
|
lastAlertParams.message === message &&
|
|
lastAlertParams.type === type &&
|
|
lastAlertParams.hapticFeedback === hapticFeedback &&
|
|
JSON.stringify(lastAlertParams.buttons) === JSON.stringify(buttons) &&
|
|
JSON.stringify(lastAlertParams.options) === JSON.stringify(options)
|
|
) {
|
|
return; // Skip showing the alert if the content is the same as the last one
|
|
}
|
|
|
|
lastAlertParams = { title, message, type, hapticFeedback, buttons, options };
|
|
|
|
if (hapticFeedback) {
|
|
triggerHapticFeedback(hapticFeedback);
|
|
}
|
|
|
|
// Ensure that there's at least one button (required for both iOS and Android)
|
|
const wrappedButtons =
|
|
buttons.length > 0
|
|
? buttons
|
|
: [
|
|
{
|
|
text: loc._.ok,
|
|
onPress: () => {},
|
|
},
|
|
];
|
|
|
|
// Correctly adjust button order based on the platform
|
|
const adjustedButtons = Platform.OS === 'android' ? wrappedButtons.slice().reverse() : wrappedButtons;
|
|
|
|
switch (type) {
|
|
case AlertType.Toast:
|
|
if (Platform.OS === 'android') {
|
|
ToastAndroid.show(message, ToastAndroid.LONG);
|
|
clearCache();
|
|
}
|
|
break;
|
|
default:
|
|
RNAlert.alert(title ?? message, title && message ? message : undefined, adjustedButtons, options);
|
|
break;
|
|
}
|
|
};
|
|
})();
|
|
|
|
export default presentAlert;
|