Files
Checkmate/src/Pages/DistributedUptime/Details/Hooks/useSubscribeToDetails.jsx
T

64 lines
1.6 KiB
React
Raw Normal View History

2025-03-04 11:19:21 -08:00
import { useState, useEffect } from "react";
2025-02-06 16:07:39 -08:00
import { networkService } from "../../../../main";
const useSubscribeToDetails = ({ monitorId, isPublic, isPublished, dateRange }) => {
2025-02-06 16:07:39 -08:00
const [isLoading, setIsLoading] = useState(true);
const [connectionStatus, setConnectionStatus] = useState(undefined);
const [retryCount, setRetryCount] = useState(0);
const [networkError, setNetworkError] = useState(false);
const [monitor, setMonitor] = useState(undefined);
const [lastUpdateTrigger, setLastUpdateTrigger] = useState(0);
useEffect(() => {
if (typeof monitorId === "undefined") {
return;
}
// If this page is public and not published, don't subscribe to details
if (isPublic && isPublished === false) {
return;
}
2025-02-06 16:07:39 -08:00
try {
const cleanup = networkService.subscribeToDistributedUptimeDetails({
monitorId,
dateRange: dateRange,
normalize: true,
onUpdate: (data) => {
if (isLoading === true) {
setIsLoading(false);
}
2025-02-06 16:07:39 -08:00
if (networkError === true) {
setNetworkError(false);
}
setLastUpdateTrigger(Date.now());
2025-03-04 11:19:21 -08:00
setMonitor(data.monitor);
2025-02-06 16:07:39 -08:00
},
onOpen: () => {
setConnectionStatus("up");
setRetryCount(0); // Reset retry count on successful connection
},
onError: () => {
setIsLoading(false);
2025-02-06 16:07:39 -08:00
setNetworkError(true);
setConnectionStatus("down");
},
});
return cleanup;
} catch (error) {
setNetworkError(true);
}
}, [
dateRange,
monitorId,
retryCount,
setConnectionStatus,
networkError,
isLoading,
2025-02-24 14:33:25 -08:00
isPublic,
isPublished,
2025-02-06 16:07:39 -08:00
]);
return [isLoading, networkError, connectionStatus, monitor, lastUpdateTrigger];
};
2025-02-07 14:48:14 -08:00
export { useSubscribeToDetails };