1
0
voyager-frontend/src/hooks/useExtractionRunPolling.js
Marcio c32ac5fd3c Refactor App structure and improve styling.
Reorganized component structure for modularity, introducing `AppHeader`, `Sidebar`, and page-specific components. Simplified state management with `LogProvider` context. Added custom Tailwind CSS themes for consistent app styling.
2026-02-09 22:20:04 -03:00

55 lines
1.4 KiB
JavaScript

import { useEffect, useRef, useState } from 'react';
export function useExtractionRunPolling(extractionApi, runId, { intervalMs = 1500 } = {}) {
const [run, setRun] = useState(null);
const [error, setError] = useState(null);
const [isPolling, setIsPolling] = useState(false);
const timerRef = useRef(null);
useEffect(() => {
if (!extractionApi || !runId) {
setRun(null);
setError(null);
setIsPolling(false);
if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
return;
}
let cancelled = false;
async function tick() {
try {
const data = await extractionApi.getRun(runId);
if (cancelled) return;
setRun(data);
setError(null);
const status = data?.status;
const shouldPoll = status === 'queued' || status === 'running';
setIsPolling(shouldPoll);
if (!shouldPoll && timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
} catch (e) {
if (!cancelled) setError(e?.message ?? 'Erro ao consultar run');
}
}
tick();
timerRef.current = setInterval(tick, intervalMs);
return () => {
cancelled = true;
if (timerRef.current) clearInterval(timerRef.current);
timerRef.current = null;
};
}, [extractionApi, runId, intervalMs]);
return { run, error, isPolling };
}