import React, { useState, useCallback } from 'react' import { Toast, type ToastVariant } from './Toast' interface ToastMessage { id: string message: string variant: ToastVariant duration?: number } interface ToastContextValue { showToast: (message: string, variant?: ToastVariant, duration?: number) => void } const ToastContext = React.createContext(undefined) function generateToastId(): string { return `toast-${Date.now()}-${Math.random().toString(36).substr(2, 9)}` } export function useToast(): ToastContextValue { const context = React.useContext(ToastContext) if (!context) { throw new Error('useToast must be used within ToastProvider') } return context } export function ToastProvider({ children }: { children: React.ReactNode }): React.ReactElement { const [toasts, setToasts] = useState([]) const showToast = useCallback((message: string, variant: ToastVariant = 'info', duration = 5000): void => { const id = generateToastId() setToasts((prev) => [...prev, { id, message, variant, duration }]) }, []) const removeToast = useCallback((id: string): void => { setToasts((prev) => prev.filter((toast) => toast.id !== id)) }, []) const contextValue: ToastContextValue = { showToast, } return ( {children}
{toasts.map((toast) => (
removeToast(toast.id)} aria-label={toast.message} > {toast.message}
))}
) }