'use client'; import { useEffect, useState, useCallback } from 'react'; import { CheckCircleIcon, ExclamationCircleIcon, XMarkIcon } from '@heroicons/react/24/outline'; export interface Toast { id: string; type: 'success' | 'error'; message: string; } let toastListeners: ((toast: Toast) => void)[] = []; export function showToast(type: 'success' | 'error', message: string) { const toast: Toast = { id: Math.random().toString(36).slice(2), type, message, }; toastListeners.forEach((fn) => fn(toast)); } export default function ToastContainer() { const [toasts, setToasts] = useState([]); const addToast = useCallback((toast: Toast) => { setToasts((prev) => [...prev, toast]); setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== toast.id)); }, 4000); }, []); useEffect(() => { toastListeners.push(addToast); return () => { toastListeners = toastListeners.filter((fn) => fn !== addToast); }; }, [addToast]); if (toasts.length === 0) return null; return (
{toasts.map((toast) => (
{toast.type === 'success' ? ( ) : ( )}

{toast.message}

))}
); }