66 lines
2.0 KiB
JavaScript
66 lines
2.0 KiB
JavaScript
import { useState, useEffect, useRef } from 'react';
|
||
import { apiFetch } from '../api/apiClient';
|
||
import { API_URL } from '../config/authConstants';
|
||
|
||
const TERMINAL_STATUSES = ['READY', 'FAILED'];
|
||
const POLL_INTERVAL_MS = 3000;
|
||
const MAX_POLL_ATTEMPTS = 100;
|
||
|
||
/**
|
||
* Поллинг статуса транскодинга медиа.
|
||
* @param {number|string} advertisingId — id сущности
|
||
* @param {boolean} enabled — включать поллинг
|
||
* @param {string} [path="ads"] — сегмент пути (например, "ads" или "fullscreen-ads")
|
||
* @returns {string} транскодинг-статус и его производные (PROCESSING/READY/FAILED/TIMEOUT)
|
||
*/
|
||
export function useTranscodingStatusPolling(advertisingId, enabled, path = "ads") {
|
||
const [status, setStatus] = useState('PROCESSING');
|
||
const attemptsRef = useRef(0);
|
||
|
||
useEffect(() => {
|
||
if (!enabled || !advertisingId) return;
|
||
|
||
attemptsRef.current = 0;
|
||
let cancelled = false;
|
||
let timeoutId;
|
||
|
||
const poll = async () => {
|
||
if (cancelled) return;
|
||
|
||
try {
|
||
const res = await apiFetch(`${API_URL}/api/v1/admin/${path}/${advertisingId}/status`);
|
||
|
||
if (!res.ok) {
|
||
throw new Error(`Status check failed: ${res.status}`);
|
||
}
|
||
|
||
const data = await res.json();
|
||
if (cancelled) return;
|
||
|
||
setStatus(data.transcodingStatus);
|
||
|
||
attemptsRef.current += 1;
|
||
const isTerminal = TERMINAL_STATUSES.includes(data.transcodingStatus);
|
||
const exceededAttempts = attemptsRef.current >= MAX_POLL_ATTEMPTS;
|
||
|
||
if (!isTerminal && !exceededAttempts) {
|
||
timeoutId = setTimeout(poll, POLL_INTERVAL_MS);
|
||
} else if (exceededAttempts && !isTerminal) {
|
||
setStatus('TIMEOUT');
|
||
}
|
||
} catch (err) {
|
||
console.warn('Status poll failed, retrying', err);
|
||
timeoutId = setTimeout(poll, POLL_INTERVAL_MS);
|
||
}
|
||
};
|
||
|
||
poll();
|
||
|
||
return () => {
|
||
cancelled = true;
|
||
clearTimeout(timeoutId);
|
||
};
|
||
}, [advertisingId, enabled, path]);
|
||
|
||
return status;
|
||
} |