'use client'; import { createContext, useContext, useState, useCallback, type ReactNode } from 'react'; type ToastType = 'success' | 'error' | 'info' | 'warning'; interface Toast { id: number; type: ToastType; message: string; } interface ToastContextValue { showToast: (message: string, type?: ToastType) => void; } const ToastContext = createContext(null); const toastColors: Record = { success: 'bg-success text-white', error: 'bg-error text-white', info: 'bg-primary text-white', warning: 'bg-secondary text-white', }; export function ToastProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]); const showToast = useCallback((message: string, type: ToastType = 'info') => { const id = Date.now() + Math.random(); setToasts((prev) => [...prev, { id, type, message }]); setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 5000); }, []); const removeToast = useCallback((id: number) => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, []); return ( {children}
{toasts.map((toast) => (
{toast.message}
))}
); } export function useToast(): ToastContextValue { const ctx = useContext(ToastContext); if (!ctx) { throw new Error('useToast must be used within ToastProvider'); } return ctx; }