From b7d733d0b0fe156f968b543079f220a13d00b431 Mon Sep 17 00:00:00 2001 From: ash1shkumar Date: Mon, 15 Jun 2026 20:25:52 +0530 Subject: [PATCH] feat: improve context propagation consistency --- frontend/app/context/ToastContext.tsx | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/frontend/app/context/ToastContext.tsx b/frontend/app/context/ToastContext.tsx index 75a0a75..73ff1a9 100644 --- a/frontend/app/context/ToastContext.tsx +++ b/frontend/app/context/ToastContext.tsx @@ -33,6 +33,34 @@ const ToastContext = const MAX_TOASTS = 4; const TOAST_DURATION = 4000; +type ToastContextSnapshot = { + activeToastCount: number; + latestToastId: number; + generatedAt: number; +}; + +function buildToastContextSnapshot( + toasts: Toast[], + latestToastId: number +): ToastContextSnapshot { + return { + activeToastCount: toasts.length, + latestToastId, + generatedAt: Date.now(), + }; +} + +function validateToastContext( + snapshot: ToastContextSnapshot +) { + return { + isValid: + snapshot.activeToastCount >= 0, + checkedAt: Date.now(), + }; +} + + export function useToast() { return useContext(ToastContext); } @@ -47,6 +75,13 @@ export function ToastProvider({ const toastIdRef = useRef(0); + const [contextSnapshot, setContextSnapshot] = + useState({ + activeToastCount: 0, + latestToastId: 0, + generatedAt: Date.now(), + }); + const toastTimersRef = useRef< Map @@ -223,6 +258,23 @@ export function ToastProvider({ ] ); + useEffect(() => { + const snapshot = + buildToastContextSnapshot( + toasts, + toastIdRef.current + ); + + const validation = + validateToastContext( + snapshot + ); + + if (validation.isValid) { + setContextSnapshot(snapshot); + } + }, [toasts]); + useEffect(() => { return () => { toastTimersRef.current.forEach( @@ -238,6 +290,11 @@ export function ToastProvider({ +
+ {contextSnapshot.activeToastCount} + {contextSnapshot.latestToastId} +
+ {children}