Files
LaunchPad/apps/frontend/src/hooks/useBackendHealth.ts

35 lines
846 B
TypeScript

import { useEffect, useState } from "react";
import type { HealthStatus } from "@launchpad/shared";
export function useBackendHealth() {
const [health, setHealth] = useState<HealthStatus | null>(null);
const [error, setError] = useState(false);
useEffect(() => {
let cancelled = false;
async function check() {
try {
const res = await fetch("/api/health");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data: HealthStatus = await res.json();
if (!cancelled) {
setHealth(data);
setError(false);
}
} catch {
if (!cancelled) setError(true);
}
}
check();
const interval = setInterval(check, 10_000);
return () => {
cancelled = true;
clearInterval(interval);
};
}, []);
return { health, error };
}