import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from 'react'; import { registerPartnerToastListener, type PartnerToastVariant } from '../lib/toast'; type PartnerToastContextValue = { showToast: (message: string, variant?: PartnerToastVariant) => void; }; const PartnerToastContext = createContext(null); export function PartnerToastProvider({ children }: { children: ReactNode }) { const [toast, setToast] = useState(''); const [variant, setVariant] = useState('success'); const timerRef = useRef(null); const showToast = useCallback((message: string, nextVariant: PartnerToastVariant = 'success') => { if (timerRef.current) window.clearTimeout(timerRef.current); setVariant(nextVariant); setToast(message); timerRef.current = window.setTimeout(() => { setToast(''); timerRef.current = null; }, nextVariant === 'error' ? 3000 : 2000); }, []); useEffect(() => { registerPartnerToastListener(showToast); return () => { registerPartnerToastListener(null); if (timerRef.current) window.clearTimeout(timerRef.current); }; }, [showToast]); return ( {children} {toast ? (
{toast}
) : null}
); } export function usePartnerToast(): PartnerToastContextValue { const ctx = useContext(PartnerToastContext); if (!ctx) throw new Error('usePartnerToast 必须在 PartnerToastProvider 内使用'); return ctx; }