diff --git a/apps/mobile/src/contexts/FitnessGoalsContext.tsx b/apps/mobile/src/contexts/FitnessGoalsContext.tsx index d4150c1..cf861a9 100644 --- a/apps/mobile/src/contexts/FitnessGoalsContext.tsx +++ b/apps/mobile/src/contexts/FitnessGoalsContext.tsx @@ -3,6 +3,7 @@ import React, { useContext, useState, useCallback, + useEffect, useRef, } from "react"; import { useUser, useAuth } from "@clerk/clerk-expo"; @@ -168,12 +169,22 @@ export function FitnessGoalsProvider({ const clearCache = useCallback(() => { setGoals([]); + setLoading(false); setLastFetchTime(0); setError(null); fetchInProgress.current = false; log.debug("Fitness goals cache cleared"); }, []); + useEffect(() => { + clearCache(); + if (user?.id) { + log.debug("Fitness goals cache reset for user", { userId: user.id }); + } else { + log.debug("Fitness goals cache reset on sign-out"); + } + }, [user?.id, clearCache]); + return ( { + setHydration(null); + setError(null); + setLoading(false); + setWaterGoal(2000); + if (user?.id) { + log.debug("Hydration state reset for user", { userId: user.id }); + } else { + log.debug("Hydration state reset on sign-out"); + } + }, [user?.id]); + const addWater = useCallback( async (amount: number) => { if (!user?.id) return; diff --git a/apps/mobile/src/contexts/NotificationsContext.tsx b/apps/mobile/src/contexts/NotificationsContext.tsx index f2159e2..9e227ba 100644 --- a/apps/mobile/src/contexts/NotificationsContext.tsx +++ b/apps/mobile/src/contexts/NotificationsContext.tsx @@ -6,7 +6,7 @@ import React, { useCallback, useRef, } from "react"; -import { useAuth } from "@clerk/clerk-expo"; +import { useAuth, useUser } from "@clerk/clerk-expo"; import { fetchNotifications, fetchUnreadCount, @@ -37,6 +37,7 @@ export function NotificationsProvider({ children: React.ReactNode; }) { const { getToken, isSignedIn } = useAuth(); + const { user } = useUser(); const [notifications, setNotifications] = useState([]); const [unreadCount, setUnreadCount] = useState(0); const [loading, setLoading] = useState(false); @@ -160,6 +161,19 @@ export function NotificationsProvider({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [isSignedIn]); // Only run when sign-in state changes + useEffect(() => { + setNotifications([]); + setUnreadCount(0); + setLoading(false); + fetchInProgressRef.current = false; + lastFetchTimeRef.current = 0; + if (user?.id) { + log.debug("Notifications state reset for user", { userId: user.id }); + } else { + log.debug("Notifications state reset on sign-out"); + } + }, [user?.id]); + // Periodic refresh every 30 seconds useEffect(() => { if (!isSignedIn) return; diff --git a/apps/mobile/src/contexts/NutritionContext.tsx b/apps/mobile/src/contexts/NutritionContext.tsx index d97b82c..f2bb6fe 100644 --- a/apps/mobile/src/contexts/NutritionContext.tsx +++ b/apps/mobile/src/contexts/NutritionContext.tsx @@ -88,6 +88,19 @@ export function NutritionProvider({ children }: { children: React.ReactNode }) { fetchTodayNutrition(); }, [fetchTodayNutrition]); + useEffect(() => { + setNutrition(null); + setMeals([]); + setError(null); + setLoading(false); + setCalorieGoal(2000); + if (user?.id) { + log.debug("Nutrition state reset for user", { userId: user.id }); + } else { + log.debug("Nutrition state reset on sign-out"); + } + }, [user?.id]); + const addMeal = useCallback( async (data: Omit) => { if (!user?.id) return; diff --git a/apps/mobile/src/contexts/RecommendationsContext.tsx b/apps/mobile/src/contexts/RecommendationsContext.tsx index a2c81d5..211afde 100644 --- a/apps/mobile/src/contexts/RecommendationsContext.tsx +++ b/apps/mobile/src/contexts/RecommendationsContext.tsx @@ -3,6 +3,7 @@ import React, { useContext, useState, useCallback, + useEffect, useRef, } from "react"; import { useUser, useAuth } from "@clerk/clerk-expo"; @@ -108,12 +109,22 @@ export function RecommendationsProvider({ const clearCache = useCallback(() => { setRecommendations([]); + setLoading(false); setLastFetchTime(0); setError(null); fetchInProgress.current = false; log.debug("Recommendations cache cleared"); }, []); + useEffect(() => { + clearCache(); + if (user?.id) { + log.debug("Recommendations cache reset for user", { userId: user.id }); + } else { + log.debug("Recommendations cache reset on sign-out"); + } + }, [user?.id, clearCache]); + return ( { setStatistics(null); + setLoading(false); setLastFetchTime(0); setError(null); log.debug("Statistics cache cleared"); }, []); + useEffect(() => { + clearCache(); + if (user?.id) { + log.debug("Statistics cache reset for user", { userId: user.id }); + } else { + log.debug("Statistics cache reset on sign-out"); + } + }, [user?.id, clearCache]); + const forceRefresh = useCallback(async () => { if (!user?.id) return;