Download src/hooks/useBackendHealth.ts from asnannp/DocDoeAI: direct link, hf CLI and curl.
- Browser
- Download file 887 Bytes
-
https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/hooks/useBackendHealth.ts
- Command line
-
hf download hf://spaces/asnannp/DocDoeAI/src/hooks/useBackendHealth.ts
-
curl -L -o useBackendHealth.ts https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/hooks/useBackendHealth.ts
887 Bytes
| "use client"; | |
| import { useCallback, useEffect, useState } from "react"; | |
| import { isBackendAvailable } from "@/lib/api/client"; | |
| export type BackendHealth = "checking" | "online" | "offline"; | |
| export function useBackendHealth(pollMs = 30_000) { | |
| const [health, setHealth] = useState<BackendHealth>("checking"); | |
| const check = useCallback(async () => { | |
| setHealth("checking"); | |
| const ok = await isBackendAvailable(); | |
| setHealth(ok ? "online" : "offline"); | |
| return ok; | |
| }, []); | |
| useEffect(() => { | |
| let cancelled = false; | |
| async function run() { | |
| const ok = await isBackendAvailable(); | |
| if (!cancelled) setHealth(ok ? "online" : "offline"); | |
| } | |
| void run(); | |
| const timer = setInterval(() => void run(), pollMs); | |
| return () => { | |
| cancelled = true; | |
| clearInterval(timer); | |
| }; | |
| }, [pollMs]); | |
| return { health, refresh: check }; | |
| } | |