import React, { createContext, useContext, useState, useCallback, ReactNode } from 'react'; import { createPortal } from 'react-dom'; import { XIcon, CheckIcon, AlertCircleIcon, InfoIcon } from '../components/Icons'; export type ToastType = 'success' | 'error' | 'info' | 'warning'; interface Toast { id: string; message: string; type: ToastType; } interface ToastContextType { showToast: (message: string, type?: ToastType, duration?: number) => void; } const ToastContext = createContext(undefined); export const useToast = () => { const context = useContext(ToastContext); if (!context) { throw new Error('useToast must be used within a ToastProvider'); } return context; }; interface ToastProviderProps { children: ReactNode; } export const ToastProvider: React.FC = ({ children }) => { const [toasts, setToasts] = useState([]); const removeToast = useCallback((id: string) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }, []); const showToast = useCallback((message: string, type: ToastType = 'info', duration = 3500) => { const id = Date.now().toString() + Math.random().toString(); setToasts((prev) => [...prev, { id, message, type }]); if (duration > 0) { setTimeout(() => { removeToast(id); }, duration); } }, [removeToast]); return ( {children} {createPortal(
{toasts.map((toast) => (
{toast.type === 'success' && } {toast.type === 'error' && } {toast.type === 'warning' && } {toast.type === 'info' && }

{toast.message}

))}
, document.body )}
); };