import { createContext, useCallback, useContext, useRef, useState, type ReactNode } from 'react'; import { AnimatePresence, motion } from 'motion/react'; type Tone = 'info' | 'success' | 'error'; interface ToastItem { id: number; text: string; tone: Tone } const ToastContext = createContext<(text: string, tone?: Tone) => void>(() => {}); /** پیامِ کوتاهِ بالای صفحه که خودش بعد از چند ثانیه می‌رود. */ export function ToastProvider({ children }: { children: ReactNode }) { const [items, setItems] = useState([]); const nextId = useRef(1); const show = useCallback((text: string, tone: Tone = 'info') => { const id = nextId.current++; setItems((list) => [...list.slice(-2), { id, text, tone }]); setTimeout(() => setItems((list) => list.filter((t) => t.id !== id)), 3500); }, []); return ( {children}
{items.map((t) => ( {t.text} ))}
); } export const useToast = () => useContext(ToastContext);