'use client'; import React, { createContext, useContext, useState, useCallback } from 'react'; import { Toast, ToastType } from '../components/Toast'; interface ToastContextType { addToast: (message: string, type: ToastType, duration?: number) => void; removeToast: (id: string) => void; } const ToastContext = createContext(undefined); export const ToastProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [toasts, setToasts] = useState>([]); const addToast = useCallback((message: string, type: ToastType, duration?: number) => { const id = Math.random().toString(36).substr(2, 9); setToasts((prev) => [...prev, { id, message, type, duration }]); }, []); const removeToast = useCallback((id: string) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }, []); return ( {children}
{toasts.map((toast) => ( ))}
); }; export const useToast = () => { const context = useContext(ToastContext); if (context === undefined) { throw new Error('useToast must be used within a ToastProvider'); } return context; };