CD — Kai-Sa-Cam Deployment / deploy (push) Successful in 1m59s
- add apiFetch wrapper with re-auth popup, retry and session monitor - proxy go2rtc through the app (/go2rtc) and drop the public stream hostname - route WebSocket upgrades manually (the talk WSS was rejecting go2rtc MSE) - harden talkback: paplay error handling, heartbeat, single speaker, reconnect - sync recording state with the server on load and on 409 - optionally verify Cf-Access-Jwt-Assertion (CF_ACCESS_TEAM_DOMAIN/AUD) - keep go2rtc on the internal network only - remove unused env vars, update README and cam-watch script
112 lines
3.4 KiB
TypeScript
112 lines
3.4 KiB
TypeScript
import { useState, useCallback, useRef, useEffect } from "react";
|
|
import { apiFetch, SessionExpiredError } from "../lib/api";
|
|
|
|
interface UseRecorderOptions {
|
|
onStart?: () => void;
|
|
onStop?: () => void;
|
|
onTimeUpdate?: (seconds: number) => void;
|
|
}
|
|
|
|
const errMsg = (e: unknown, fallback: string) =>
|
|
e instanceof SessionExpiredError
|
|
? "Session expirée, reconnecte-toi"
|
|
: e instanceof Error
|
|
? e.message
|
|
: fallback;
|
|
|
|
async function readError(res: Response, fallback: string) {
|
|
try {
|
|
return (await res.json()).error || fallback;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function useRecorder({
|
|
onStart,
|
|
onStop,
|
|
onTimeUpdate,
|
|
}: UseRecorderOptions) {
|
|
const [isRecording, setIsRecording] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const intervalRef = useRef<number>();
|
|
const isRecordingRef = useRef(false);
|
|
isRecordingRef.current = isRecording;
|
|
|
|
// Callbacks toujours à jour sans recréer les fonctions
|
|
const cb = useRef({ onStart, onStop, onTimeUpdate });
|
|
cb.current = { onStart, onStop, onTimeUpdate };
|
|
|
|
const clearTimer = () => {
|
|
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
intervalRef.current = undefined;
|
|
};
|
|
|
|
const beginTimer = (startedAt: number) => {
|
|
clearTimer();
|
|
const tick = () =>
|
|
cb.current.onTimeUpdate?.(Math.floor((Date.now() - startedAt) / 1000));
|
|
tick();
|
|
intervalRef.current = window.setInterval(tick, 1000);
|
|
};
|
|
|
|
const markStopped = () => {
|
|
clearTimer();
|
|
setIsRecording(false);
|
|
cb.current.onStop?.();
|
|
};
|
|
|
|
// Récupère un enregistrement déjà en cours côté serveur (reload, autre onglet, 409)
|
|
const adoptRunning = useCallback(async (): Promise<boolean> => {
|
|
const res = await apiFetch("/api/recordings/status");
|
|
const s = await res.json();
|
|
if (!s.isRecording) return false;
|
|
setIsRecording(true);
|
|
cb.current.onStart?.();
|
|
beginTimer(s.startTime ?? Date.now());
|
|
return true;
|
|
}, []);
|
|
|
|
const startRecording = useCallback(async () => {
|
|
setError(null);
|
|
try {
|
|
const res = await apiFetch("/api/recordings/start", { method: "POST" });
|
|
if (res.status === 409 && (await adoptRunning())) return;
|
|
if (!res.ok)
|
|
throw new Error(await readError(res, "Failed to start recording"));
|
|
setIsRecording(true);
|
|
cb.current.onStart?.();
|
|
beginTimer(Date.now());
|
|
} catch (e) {
|
|
setError(errMsg(e, "Erreur démarrage enregistrement"));
|
|
}
|
|
}, [adoptRunning]);
|
|
|
|
const stopRecording = useCallback(async () => {
|
|
setError(null);
|
|
try {
|
|
const res = await apiFetch("/api/recordings/stop", { method: "POST" });
|
|
// 409 = plus rien en cours côté serveur : l'UI doit juste se resynchroniser
|
|
if (!res.ok && res.status !== 409)
|
|
throw new Error(await readError(res, "Failed to stop recording"));
|
|
markStopped();
|
|
} catch (e) {
|
|
setError(errMsg(e, "Erreur arrêt enregistrement"));
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
adoptRunning().catch(() => {});
|
|
const handleBeforeUnload = () => {
|
|
if (isRecordingRef.current) navigator.sendBeacon("/api/recordings/stop");
|
|
};
|
|
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
return () => {
|
|
window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
clearTimer();
|
|
};
|
|
}, [adoptRunning]);
|
|
|
|
return { isRecording, error, startRecording, stopRecording };
|
|
}
|