'use client' import { createContext, useContext, useState, useCallback, ReactNode } from 'react' import Toast, { ToastType } from './Toast' interface ToastMessage { id: string message: string type: ToastType } interface ToastContextType { showToast: (message: string, type: ToastType) => void } const ToastContext = createContext(undefined) export function useToast() { const context = useContext(ToastContext) if (!context) { throw new Error('useToast must be used within ToastProvider') } return context } export function ToastProvider({ children }: { children: ReactNode }) { 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 }]) }, []) const removeToast = useCallback((id: string) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)) }, []) return ( {children}
{toasts.map((toast) => ( removeToast(toast.id)} /> ))}
) }