Files
cyclone/src/hooks/useInboxLanes.ts
T
2026-06-20 18:41:44 -06:00

42 lines
1.1 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from "react";
import { fetchInboxLanes, type InboxLanes } from "@/lib/inbox-api";
/** Default poll interval. Tail-integration (T19) will replace this. */
const POLL_INTERVAL_MS = 5_000;
export function useInboxLanes() {
const [lanes, setLanes] = useState<InboxLanes>({
rejected: [],
candidates: [],
unmatched: [],
done_today: [],
});
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
const refetch = useCallback(async () => {
try {
const next = await fetchInboxLanes();
setLanes(next);
setError(null);
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
void refetch();
pollRef.current = setInterval(() => {
void refetch();
}, POLL_INTERVAL_MS);
return () => {
if (pollRef.current) clearInterval(pollRef.current);
};
}, [refetch]);
return { lanes, loading, error, refetch };
}