2025-11-09 21:21:32 +03:00

74 lines
1.6 KiB
TypeScript

/**
* Custom hook for polling data at regular intervals
*/
import { useEffect, useRef, useState } from 'react';
interface UsePollingOptions {
interval?: number;
enabled?: boolean;
}
interface UsePollingResult<T> {
data: T | null;
loading: boolean;
error: Error | null;
refetch: () => Promise<void>;
}
export function usePolling<T>(
fetchFn: () => Promise<T>,
options: UsePollingOptions = {}
): UsePollingResult<T> {
const { interval = 5000, enabled = true } = options;
const [data, setData] = useState<T | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const intervalRef = useRef<NodeJS.Timeout | null>(null);
const fetchData = async () => {
try {
setIsLoading(true);
setError(null);
const result = await fetchFn();
setData(result);
} catch (err) {
const error = err instanceof Error ? err : new Error('Unknown error');
setError(error);
} finally {
setIsLoading(false);
}
};
useEffect(() => {
if (!enabled) {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
return;
}
// Fetch immediately
fetchData();
// Set up polling
intervalRef.current = setInterval(fetchData, interval);
// Cleanup
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, [enabled, interval]);
return {
data,
loading: isLoading,
error,
refetch: fetchData,
};
}