store-intelligence / dashboard /src /hooks /usePollingResource.ts
SandeepChakka's picture
Cursor
Deploy Store Intelligence: API, dashboard, events (no CCTV/binaries)
312fdff
Raw History Blame Contribute Delete
1.19 kB
import { useCallback, useEffect, useState } from 'react';
const DEFAULT_INTERVAL_MS = 10_000;
export interface PollingState<T> {
data: T | null;
loading: boolean;
error: string | null;
lastUpdated: Date | null;
refresh: () => void;
}
export function usePollingResource<T>(
fetcher: () => Promise<T>,
intervalMs = DEFAULT_INTERVAL_MS,
): PollingState<T> {
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
const refresh = useCallback(() => {
void (async () => {
try {
const result = await fetcher();
setData(result);
setError(null);
setLastUpdated(new Date());
} catch (err) {
setError(err instanceof Error ? err.message : 'Request failed');
} finally {
setLoading(false);
}
})();
}, [fetcher]);
useEffect(() => {
refresh();
const id = setInterval(refresh, intervalMs);
return () => clearInterval(id);
}, [refresh, intervalMs]);
return { data, loading, error, lastUpdated, refresh };
}