import React, { createContext, useContext, useState, ReactNode } from "react"; type Toast = { id: string; message: string; type: "success" | "error" | "info" | "warning"; position?: ToastPosition; // Optional position for each toast }; type ToastPosition = | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right"; type ToastContextType = { addToast: ( message: string, type: Toast["type"], position?: ToastPosition ) => void; removeToast: (id: string) => void; }; const ToastContext = createContext(undefined); const ToastProvider: React.FC<{ children: ReactNode }> = ({ children }) => { const [toasts, setToasts] = useState([]); const addToast = ( message: string, type: Toast["type"], position: ToastPosition = "bottom-center" // Default position ) => { const id = Math.random().toString(36).substr(2, 9); // Generate a unique ID setToasts((prev) => [...prev, { id, message, type, position }]); // Auto-remove toast after 3 seconds setTimeout(() => removeToast(id), 3000); }; const removeToast = (id: string) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }; return ( {children}
{toasts.map((toast) => (
removeToast(toast.id)} // Allow manual dismissal > {toast.message}
))}
); }; export const useToast = (): ToastContextType => { const context = useContext(ToastContext); if (!context) { throw new Error("useToast must be used within a ToastProvider"); } return context; }; export default ToastProvider;