import { createContext, useContext, useState, useCallback } from 'react' import Toast from './Toast' const ToastContext = createContext() export function useToast() { const context = useContext(ToastContext) if (!context) { throw new Error('useToast must be used within ToastProvider') } return context } export function ToastProvider({ children }) { const [toasts, setToasts] = useState([]) const addToast = useCallback((message, type = 'info', duration = 4000) => { const id = Date.now() + Math.random() setToasts(prev => [...prev, { id, message, type, duration }]) }, []) const removeToast = useCallback((id) => { setToasts(prev => prev.map(t => t.id === id ? { ...t, exiting: true } : t)) setTimeout(() => { setToasts(prev => prev.filter(t => t.id !== id)) }, 300) }, []) const toast = { success: (message, duration) => addToast(message, 'success', duration), error: (message, duration) => addToast(message, 'error', duration), info: (message, duration) => addToast(message, 'info', duration), warning: (message, duration) => addToast(message, 'warning', duration), } return ( {children} {/* Toast container - fixed position */}
{toasts.map(t => ( removeToast(t.id)} /> ))}
) }