Spaces:
Sleeping
Sleeping
Download dashboard/src/hooks/usePollingResource.ts from SandeepChakka/store-intelligence: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/SandeepChakka/store-intelligence/resolve/main/dashboard/src/hooks/usePollingResource.ts
- Command line
-
hf download hf://spaces/SandeepChakka/store-intelligence/dashboard/src/hooks/usePollingResource.ts
-
curl -L -o usePollingResource.ts https://huggingface.co/spaces/SandeepChakka/store-intelligence/resolve/main/dashboard/src/hooks/usePollingResource.ts
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 }; | |
| } | |