This commit is contained in:
legop3
2025-11-15 22:02:44 -05:00
parent 7a7a194fde
commit d55726eb1f
11 changed files with 90 additions and 68 deletions
+19 -14
View File
@@ -1,24 +1,17 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { useSocket } from '../context/SocketContext.jsx';
export function useVideoRequests(roverIds = []) {
const socket = useSocket();
const [sources, setSources] = useState({});
const normalizedKey = Array.isArray(roverIds) ? roverIds.filter(Boolean).join('|') : '';
useEffect(() => {
const ids = Array.from(new Set(roverIds.filter(Boolean)));
if (!ids.length) {
setSources({});
return;
if (!normalizedKey) {
return undefined;
}
const ids = normalizedKey.split('|');
let cancelled = false;
setSources((prev) => {
const next = {};
ids.forEach((id) => {
if (prev[id]) next[id] = prev[id];
});
return next;
});
ids.forEach((roverId) => {
socket.emit('video:request', { roverId }, (resp = {}) => {
if (cancelled) return;
@@ -32,7 +25,19 @@ export function useVideoRequests(roverIds = []) {
return () => {
cancelled = true;
};
}, [socket, roverIds.join('|')]);
}, [socket, normalizedKey]);
return sources;
const filtered = useMemo(() => {
if (!normalizedKey) return {};
const ids = normalizedKey.split('|');
const next = {};
ids.forEach((id) => {
if (sources[id]) {
next[id] = sources[id];
}
});
return next;
}, [normalizedKey, sources]);
return filtered;
}