reset mobile context caches on user identity changes

This commit is contained in:
echo 2026-03-29 15:38:51 +02:00
parent aa662a9b74
commit 34e88bdde5
6 changed files with 79 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import React, {
useContext, useContext,
useState, useState,
useCallback, useCallback,
useEffect,
useRef, useRef,
} from "react"; } from "react";
import { useUser, useAuth } from "@clerk/clerk-expo"; import { useUser, useAuth } from "@clerk/clerk-expo";
@ -168,12 +169,22 @@ export function FitnessGoalsProvider({
const clearCache = useCallback(() => { const clearCache = useCallback(() => {
setGoals([]); setGoals([]);
setLoading(false);
setLastFetchTime(0); setLastFetchTime(0);
setError(null); setError(null);
fetchInProgress.current = false; fetchInProgress.current = false;
log.debug("Fitness goals cache cleared"); 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 ( return (
<FitnessGoalsContext.Provider <FitnessGoalsContext.Provider
value={{ value={{

View File

@ -74,6 +74,18 @@ export function HydrationProvider({ children }: { children: React.ReactNode }) {
fetchTodayHydration(); fetchTodayHydration();
}, [fetchTodayHydration]); }, [fetchTodayHydration]);
useEffect(() => {
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( const addWater = useCallback(
async (amount: number) => { async (amount: number) => {
if (!user?.id) return; if (!user?.id) return;

View File

@ -6,7 +6,7 @@ import React, {
useCallback, useCallback,
useRef, useRef,
} from "react"; } from "react";
import { useAuth } from "@clerk/clerk-expo"; import { useAuth, useUser } from "@clerk/clerk-expo";
import { import {
fetchNotifications, fetchNotifications,
fetchUnreadCount, fetchUnreadCount,
@ -37,6 +37,7 @@ export function NotificationsProvider({
children: React.ReactNode; children: React.ReactNode;
}) { }) {
const { getToken, isSignedIn } = useAuth(); const { getToken, isSignedIn } = useAuth();
const { user } = useUser();
const [notifications, setNotifications] = useState<Notification[]>([]); const [notifications, setNotifications] = useState<Notification[]>([]);
const [unreadCount, setUnreadCount] = useState(0); const [unreadCount, setUnreadCount] = useState(0);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@ -160,6 +161,19 @@ export function NotificationsProvider({
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [isSignedIn]); // Only run when sign-in state changes }, [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 // Periodic refresh every 30 seconds
useEffect(() => { useEffect(() => {
if (!isSignedIn) return; if (!isSignedIn) return;

View File

@ -88,6 +88,19 @@ export function NutritionProvider({ children }: { children: React.ReactNode }) {
fetchTodayNutrition(); fetchTodayNutrition();
}, [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( const addMeal = useCallback(
async (data: Omit<MealEntry, "id" | "createdAt" | "dailyNutritionId">) => { async (data: Omit<MealEntry, "id" | "createdAt" | "dailyNutritionId">) => {
if (!user?.id) return; if (!user?.id) return;

View File

@ -3,6 +3,7 @@ import React, {
useContext, useContext,
useState, useState,
useCallback, useCallback,
useEffect,
useRef, useRef,
} from "react"; } from "react";
import { useUser, useAuth } from "@clerk/clerk-expo"; import { useUser, useAuth } from "@clerk/clerk-expo";
@ -108,12 +109,22 @@ export function RecommendationsProvider({
const clearCache = useCallback(() => { const clearCache = useCallback(() => {
setRecommendations([]); setRecommendations([]);
setLoading(false);
setLastFetchTime(0); setLastFetchTime(0);
setError(null); setError(null);
fetchInProgress.current = false; fetchInProgress.current = false;
log.debug("Recommendations cache cleared"); 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 ( return (
<RecommendationsContext.Provider <RecommendationsContext.Provider
value={{ value={{

View File

@ -1,4 +1,10 @@
import React, { createContext, useContext, useState, useCallback } from "react"; import React, {
createContext,
useContext,
useState,
useCallback,
useEffect,
} from "react";
import { useUser, useAuth } from "@clerk/clerk-expo"; import { useUser, useAuth } from "@clerk/clerk-expo";
import { getUserStatistics } from "../api/statistics"; import { getUserStatistics } from "../api/statistics";
import type { UserStatisticsResponse } from "../api/types"; import type { UserStatisticsResponse } from "../api/types";
@ -75,11 +81,21 @@ export function StatisticsProvider({
const clearCache = useCallback(() => { const clearCache = useCallback(() => {
setStatistics(null); setStatistics(null);
setLoading(false);
setLastFetchTime(0); setLastFetchTime(0);
setError(null); setError(null);
log.debug("Statistics cache cleared"); 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 () => { const forceRefresh = useCallback(async () => {
if (!user?.id) return; if (!user?.id) return;