Files
Checkmate/src/Pages/DistributedUptimeStatus/Status/Hooks/useStatusPageFetchByUrl.jsx
T

57 lines
1.8 KiB
React
Raw Normal View History

2025-02-18 09:47:54 -08:00
import { useState, useEffect } from "react";
import { networkService } from "../../../../main";
import { createToast } from "../../../../Utils/toastUtils";
import { useSelector } from "react-redux";
2025-02-21 15:06:11 -08:00
import { useTheme } from "@emotion/react";
import { useMonitorUtils } from "../../../../Hooks/useMonitorUtils";
2025-02-18 09:47:54 -08:00
2025-02-21 15:06:11 -08:00
const useStatusPageFetchByUrl = ({ url, timeFrame }) => {
2025-02-18 09:47:54 -08:00
const [isLoading, setIsLoading] = useState(true);
const [networkError, setNetworkError] = useState(false);
const [statusPage, setStatusPage] = useState(undefined);
const [monitorId, setMonitorId] = useState(undefined);
const [isPublished, setIsPublished] = useState(false);
const { authToken } = useSelector((state) => state.auth);
2025-02-21 15:06:11 -08:00
const theme = useTheme();
const { getMonitorWithPercentage } = useMonitorUtils();
2025-02-18 09:47:54 -08:00
useEffect(() => {
const fetchStatusPageByUrl = async () => {
try {
2025-02-21 15:06:11 -08:00
const response = await networkService.getDistributedStatusPageByUrl({
2025-02-18 09:47:54 -08:00
authToken,
url,
type: "distributed",
2025-02-21 15:06:11 -08:00
timeFrame,
2025-02-18 09:47:54 -08:00
});
if (!response?.data?.data) return;
const statusPage = response.data.data;
2025-02-21 15:06:11 -08:00
const monitorsWithPercentage = statusPage?.subMonitors.map((monitor) =>
getMonitorWithPercentage(monitor, theme)
);
const statusPageWithSubmonitorPercentages = {
...statusPage,
subMonitors: monitorsWithPercentage,
};
setStatusPage(statusPageWithSubmonitorPercentages);
2025-02-18 09:47:54 -08:00
setMonitorId(statusPage?.monitors[0]);
setIsPublished(statusPage?.isPublished);
} catch (error) {
setNetworkError(true);
createToast({
body: error.message,
});
} finally {
setIsLoading(false);
}
};
fetchStatusPageByUrl();
2025-02-21 15:06:11 -08:00
}, [authToken, url, getMonitorWithPercentage, theme, timeFrame]);
2025-02-18 09:47:54 -08:00
return [isLoading, networkError, statusPage, monitorId, isPublished];
};
export { useStatusPageFetchByUrl };