feat: use a new ws implementation thats encapsulated

This commit is contained in:
Aarnav Tale
2025-03-06 17:32:33 -05:00
parent 45537620a6
commit 5dd4c41291
13 changed files with 370 additions and 101 deletions
+25
View File
@@ -0,0 +1,25 @@
import { useEffect } from 'react';
import { useFetcher } from 'react-router';
import { HostInfo } from '~/types';
export default function useAgent(nodeIds: string[], interval = 3000) {
const fetcher = useFetcher<Record<string, HostInfo>>();
useEffect(() => {
const qp = new URLSearchParams({ node_ids: nodeIds.join(',') });
fetcher.load(`/api/agent?${qp.toString()}`);
const intervalID = setInterval(() => {
fetcher.load(`/api/agent?${qp.toString()}`);
}, interval);
return () => {
clearInterval(intervalID);
};
}, [fetcher, interval, nodeIds]);
return {
data: fetcher.data,
isLoading: fetcher.state === 'loading',
};
}