'use client'; import { createContext, useContext, useState, useCallback } from 'react'; import ToastNotification, { Toast } from './ToastNotification'; interface ToastContextType { showToast: (type: Toast['type'], title: string, message?: string) => void; success: (title: string, message?: string) => void; error: (title: string, message?: string) => void; info: (title: string, message?: string) => void; } const ToastContext = createContext(undefined); export function ToastProvider({ children }: { children: React.ReactNode }) { const [toasts, setToasts] = useState([]); const showToast = useCallback((type: Toast['type'], title: string, message?: string) => { const id = Date.now().toString(); setToasts(prev => [...prev, { id, type, title, message }]); }, []); const success = useCallback((title: string, message?: string) => { showToast('success', title, message); }, [showToast]); const error = useCallback((title: string, message?: string) => { showToast('error', title, message); }, [showToast]); const info = useCallback((title: string, message?: string) => { showToast('info', title, message); }, [showToast]); const removeToast = useCallback((id: string) => { setToasts(prev => prev.filter(toast => toast.id !== id)); }, []); return ( {children}
{toasts.map(toast => ( ))}
); } export function useToast() { const context = useContext(ToastContext); if (!context) { throw new Error('useToast must be used within ToastProvider'); } return context; }