import React, { createContext, useContext, useState, useCallback } from 'react'; type ToastType = 'success' | 'error' | 'info'; interface Toast { id: string; message: string; type: ToastType; } interface NotificationContextType { showToast: (message: string, type: ToastType) => void; } const NotificationContext = createContext(undefined); export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [toasts, setToasts] = useState([]); const showToast = useCallback((message: string, type: ToastType) => { const id = Math.random().toString(36).substring(7); setToasts((prev) => [...prev, { id, message, type }]); setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 5000); }, []); return ( {children}
{toasts.map((toast) => (
{toast.message}
))}
); }; export const useNotification = () => { const context = useContext(NotificationContext); if (!context) throw new Error('useNotification must be used within NotificationProvider'); return context; };