import { useEffect, useState } from 'react' /** * Toast notification system. * Usage: import { useToast } from './Toast' * const toast = useToast() * toast.success('Done!') / toast.error('Oops') */ let _dispatch = null export function useToast() { return { success: (msg) => _dispatch?.({ type: 'success', msg }), error: (msg) => _dispatch?.({ type: 'error', msg }), info: (msg) => _dispatch?.({ type: 'info', msg }), } } export default function ToastContainer() { const [toasts, setToasts] = useState([]) useEffect(() => { _dispatch = ({ type, msg }) => { const id = Date.now() setToasts((t) => [...t, { id, type, msg }]) setTimeout(() => setToasts((t) => t.filter((x) => x.id !== id)), 3500) } return () => { _dispatch = null } }, []) if (!toasts.length) return null return (
{toasts.map((t) => (
{t.type === 'success' && } {t.type === 'error' && } {t.type === 'info' && } {t.msg}
))}
) } const CheckIcon = () => ( ) const XIcon = () => ( ) const InfoIcon = () => ( )