reset mobile context caches on user identity changes
This commit is contained in:
parent
aa662a9b74
commit
34e88bdde5
@ -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 (
|
||||
<FitnessGoalsContext.Provider
|
||||
value={{
|
||||
|
||||
@ -74,6 +74,18 @@ export function HydrationProvider({ children }: { children: React.ReactNode }) {
|
||||
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(
|
||||
async (amount: number) => {
|
||||
if (!user?.id) return;
|
||||
|
||||
@ -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<Notification[]>([]);
|
||||
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;
|
||||
|
||||
@ -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<MealEntry, "id" | "createdAt" | "dailyNutritionId">) => {
|
||||
if (!user?.id) return;
|
||||
|
||||
@ -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 (
|
||||
<RecommendationsContext.Provider
|
||||
value={{
|
||||
|
||||
@ -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 { getUserStatistics } from "../api/statistics";
|
||||
import type { UserStatisticsResponse } from "../api/types";
|
||||
@ -75,11 +81,21 @@ export function StatisticsProvider({
|
||||
|
||||
const clearCache = useCallback(() => {
|
||||
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;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user