import { createContext, useCallback, useContext, useRef, useState, type ReactNode } from 'react'; type ToastType = '' | 'ok' | 'err'; interface ToastContextValue { toast: (msg: string, type?: ToastType) => void; } const ToastContext = createContext(null); export function ToastProvider({ children }: { children: ReactNode }) { const [msg, setMsg] = useState(''); const [type, setType] = useState(''); const [show, setShow] = useState(false); const timer = useRef(undefined); const toast = useCallback((text: string, t: ToastType = '') => { setMsg(text); setType(t); setShow(true); window.clearTimeout(timer.current); timer.current = window.setTimeout(() => setShow(false), 2600); }, []); return ( {children}
{msg}
); } export function useToast(): ToastContextValue { const ctx = useContext(ToastContext); if (!ctx) throw new Error('useToast must be used within ToastProvider'); return ctx; }