Spaces:
Sleeping
Sleeping
File size: 1,410 Bytes
2857363 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import { useState, useCallback, useEffect } from 'react';
import { getCacheStats } from '../services/api';
import type { CacheStats } from '../types';
interface UseCacheStatsReturn {
stats: CacheStats | null;
isLoading: boolean;
error: string | null;
refresh: () => Promise<void>;
}
export const useCacheStats = (autoRefreshInterval: number = 30000): UseCacheStatsReturn => {
const [stats, setStats] = useState<CacheStats | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const fetchStats = useCallback(async () => {
setIsLoading(true);
setError(null);
try {
const response = await getCacheStats();
setStats(response);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Failed to load cache stats';
setError(errorMessage);
} finally {
setIsLoading(false);
}
}, []);
const refresh = useCallback(async () => {
await fetchStats();
}, [fetchStats]);
// Initial fetch
useEffect(() => {
fetchStats();
}, [fetchStats]);
// Auto-refresh
useEffect(() => {
if (autoRefreshInterval > 0) {
const interval = setInterval(fetchStats, autoRefreshInterval);
return () => clearInterval(interval);
}
}, [fetchStats, autoRefreshInterval]);
return {
stats,
isLoading,
error,
refresh,
};
};
|