mirror of
https://github.com/averel10/crypto_clash.git
synced 2026-03-12 19:08:11 +01:00
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
export type ToastType = 'success' | 'error' | 'info' | 'warning';
|
|
|
|
export interface Toast {
|
|
id: string;
|
|
message: string;
|
|
type: ToastType;
|
|
duration?: number;
|
|
}
|
|
|
|
let toastListener: ((toast: Toast) => void) | null = null;
|
|
let toastId = 0;
|
|
|
|
export function setToastListener(listener: (toast: Toast) => void) {
|
|
toastListener = listener;
|
|
}
|
|
|
|
export function showToast(message: string, type: ToastType = 'info', duration = 4000) {
|
|
const id = String(toastId++);
|
|
const toast: Toast = {
|
|
id,
|
|
message,
|
|
type,
|
|
duration,
|
|
};
|
|
|
|
if (toastListener) {
|
|
toastListener(toast);
|
|
}
|
|
|
|
return id;
|
|
}
|
|
|
|
export function showSuccessToast(message: string, duration?: number) {
|
|
return showToast(message, 'success', duration);
|
|
}
|
|
|
|
export function showErrorToast(message: string, duration?: number) {
|
|
return showToast(message, 'error', duration);
|
|
}
|
|
|
|
export function showInfoToast(message: string, duration?: number) {
|
|
return showToast(message, 'info', duration);
|
|
}
|
|
|
|
export function showWarningToast(message: string, duration?: number) {
|
|
return showToast(message, 'warning', duration);
|
|
}
|