Compare commits
No commits in common. "4e322503cc2cd926d6b01e294c8cb6b6e4c19628" and "ad3eba48b009fccc3bc67a019c5f8b10c8a3f4c0" have entirely different histories.
4e322503cc
...
ad3eba48b0
Binary file not shown.
@ -4,7 +4,6 @@ import React, {
|
|||||||
useState,
|
useState,
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
useRef,
|
|
||||||
} from "react";
|
} 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";
|
||||||
@ -37,9 +36,6 @@ export function StatisticsProvider({
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [error, setError] = useState<Error | null>(null);
|
const [error, setError] = useState<Error | null>(null);
|
||||||
const [lastFetchTime, setLastFetchTime] = useState<number>(0);
|
const [lastFetchTime, setLastFetchTime] = useState<number>(0);
|
||||||
const statisticsRef = useRef<UserStatisticsResponse | null>(null);
|
|
||||||
const lastFetchTimeRef = useRef<number>(0);
|
|
||||||
const fetchInProgressRef = useRef(false);
|
|
||||||
|
|
||||||
// Cache statistics for 30 seconds to avoid duplicate calls
|
// Cache statistics for 30 seconds to avoid duplicate calls
|
||||||
const CACHE_DURATION = 30000; // 30 seconds
|
const CACHE_DURATION = 30000; // 30 seconds
|
||||||
@ -47,49 +43,48 @@ export function StatisticsProvider({
|
|||||||
const refetchStatistics = useCallback(async () => {
|
const refetchStatistics = useCallback(async () => {
|
||||||
if (!user?.id) return;
|
if (!user?.id) return;
|
||||||
|
|
||||||
if (fetchInProgressRef.current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have recent cached data
|
// Check if we have recent cached data
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (
|
if (statistics && now - lastFetchTime < CACHE_DURATION) {
|
||||||
statisticsRef.current &&
|
log.debug("Using cached statistics", {
|
||||||
now - lastFetchTimeRef.current < CACHE_DURATION
|
age: now - lastFetchTime,
|
||||||
) {
|
cacheRemaining: CACHE_DURATION - (now - lastFetchTime),
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fetchInProgressRef.current = true;
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
log.debug("Fetching fresh statistics", { userId: user.id });
|
||||||
|
|
||||||
const token = await getToken();
|
const token = await getToken();
|
||||||
const stats = await getUserStatistics(user.id, token);
|
const stats = await getUserStatistics(user.id, token);
|
||||||
|
|
||||||
setStatistics(stats);
|
setStatistics(stats);
|
||||||
statisticsRef.current = stats;
|
|
||||||
setLastFetchTime(now);
|
setLastFetchTime(now);
|
||||||
lastFetchTimeRef.current = now;
|
log.debug("Statistics fetched and cached", {
|
||||||
|
userId: user.id,
|
||||||
|
hasWeeklyTrend: !!stats.weeklyTrend,
|
||||||
|
weeklyTrendLength: stats.weeklyTrend?.length || 0,
|
||||||
|
weeklyTrendSample: stats.weeklyTrend?.[0],
|
||||||
|
stats,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const error = err instanceof Error ? err : new Error(String(err));
|
const error = err instanceof Error ? err : new Error(String(err));
|
||||||
log.error("Failed to fetch statistics", error);
|
log.error("Failed to fetch statistics", error);
|
||||||
setError(error);
|
setError(error);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
fetchInProgressRef.current = false;
|
|
||||||
}
|
}
|
||||||
}, [user?.id, getToken]);
|
}, [user?.id, getToken, statistics, lastFetchTime]);
|
||||||
|
|
||||||
const clearCache = useCallback(() => {
|
const clearCache = useCallback(() => {
|
||||||
setStatistics(null);
|
setStatistics(null);
|
||||||
statisticsRef.current = null;
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setLastFetchTime(0);
|
setLastFetchTime(0);
|
||||||
lastFetchTimeRef.current = 0;
|
|
||||||
setError(null);
|
setError(null);
|
||||||
fetchInProgressRef.current = false;
|
log.debug("Statistics cache cleared");
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -105,25 +100,28 @@ export function StatisticsProvider({
|
|||||||
if (!user?.id) return;
|
if (!user?.id) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fetchInProgressRef.current = true;
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
log.debug("Force fetching statistics", { userId: user.id });
|
||||||
|
|
||||||
const token = await getToken();
|
const token = await getToken();
|
||||||
const stats = await getUserStatistics(user.id, token);
|
const stats = await getUserStatistics(user.id, token);
|
||||||
|
|
||||||
setStatistics(stats);
|
setStatistics(stats);
|
||||||
statisticsRef.current = stats;
|
setLastFetchTime(Date.now());
|
||||||
const now = Date.now();
|
log.debug("Statistics force fetched and cached", {
|
||||||
setLastFetchTime(now);
|
userId: user.id,
|
||||||
lastFetchTimeRef.current = now;
|
hasWeeklyTrend: !!stats.weeklyTrend,
|
||||||
|
weeklyTrendLength: stats.weeklyTrend?.length || 0,
|
||||||
|
weeklyTrendSample: stats.weeklyTrend?.[0],
|
||||||
|
stats,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const error = err instanceof Error ? err : new Error(String(err));
|
const error = err instanceof Error ? err : new Error(String(err));
|
||||||
log.error("Failed to force fetch statistics", error);
|
log.error("Failed to force fetch statistics", error);
|
||||||
setError(error);
|
setError(error);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
fetchInProgressRef.current = false;
|
|
||||||
}
|
}
|
||||||
}, [user?.id, getToken]);
|
}, [user?.id, getToken]);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user