import { toast, ToastOptions } from 'react-toastify'; import React from 'react'; import { Button } from './components/ui/button'; export interface ToastServiceOptions { silent?: boolean; shouldThrow?: boolean; } export default class ToastService { private silent: boolean = false; private shouldThrow: boolean = false; // Create a singleton instance private static instance: ToastService; public static getInstance(): ToastService { if (!ToastService.instance) { ToastService.instance = new ToastService(); } return ToastService.instance; } configure(options: ToastServiceOptions = {}): void { if (options.silent !== undefined) { this.silent = options.silent; } if (options.shouldThrow !== undefined) { this.shouldThrow = options.shouldThrow; } } error({ title, msg, traceback }: { title: string; msg: string; traceback: string }): void { if (!this.silent) { toastError({ title, msg, traceback }); } if (this.shouldThrow) { throw new Error(msg); } } loading({ title, msg }: { title: string; msg: string }): string | number | undefined { if (this.silent) { return undefined; } const toastId = toastLoading({ title, msg }); return toastId; } success({ title, msg }: { title: string; msg: string }): void { if (this.silent) { return; } toastSuccess({ title, msg }); } dismiss(toastId?: string | number): void { if (toastId) toast.dismiss(toastId); } /** * Handle errors with consistent logging and toast notifications * Consolidates the functionality of the original handleError function */ handleError(title: string, message: string, options: ToastServiceOptions = {}): void { this.configure(options); this.error({ title: title || 'Error', msg: message, traceback: message, }); } } // Export a singleton instance for use throughout the app export const toastService = ToastService.getInstance(); const commonToastOptions: ToastOptions = { position: 'top-right', closeButton: false, hideProgressBar: true, closeOnClick: true, pauseOnHover: true, draggable: true, }; type ToastSuccessProps = { title?: string; msg?: string; toastOptions?: ToastOptions }; export function toastSuccess({ title, msg, toastOptions = {} }: ToastSuccessProps) { return toast.success(