Files
Checkmate/client/src/Hooks/useMonitorUtils.js
T

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2025-02-06 14:55:10 -08:00
import { useCallback } from "react";
2025-05-21 23:59:22 +05:30
import { useTheme } from "@mui/material";
2025-02-06 14:55:10 -08:00
const useMonitorUtils = () => {
const getMonitorWithPercentage = useCallback((monitor, theme) => {
let uptimePercentage = "";
let percentageColor = "";
2025-07-24 13:51:00 -07:00
if (monitor?.uptimePercentage !== undefined) {
2025-02-06 14:55:10 -08:00
uptimePercentage =
2025-07-24 13:51:00 -07:00
monitor?.uptimePercentage === 0
2025-02-06 14:55:10 -08:00
? "0"
2025-07-24 13:51:00 -07:00
: (monitor?.uptimePercentage * 100).toFixed(2);
2025-02-06 14:55:10 -08:00
percentageColor =
2025-07-24 13:51:00 -07:00
monitor?.uptimePercentage < 0.25
2025-02-06 14:55:10 -08:00
? theme.palette.error.main
2025-07-24 13:51:00 -07:00
: monitor?.uptimePercentage < 0.5
2025-02-06 14:55:10 -08:00
? theme.palette.warning.main
2025-07-24 13:51:00 -07:00
: monitor?.uptimePercentage < 0.75
2025-02-06 14:55:10 -08:00
? theme.palette.success.main
: theme.palette.success.main;
}
return {
2025-02-07 14:44:56 -08:00
...monitor,
2025-02-06 14:55:10 -08:00
percentage: uptimePercentage,
percentageColor,
monitor: monitor,
};
}, []);
const determineState = useCallback((monitor) => {
if (typeof monitor === "undefined") return "pending";
2025-07-24 13:51:00 -07:00
if (monitor?.isActive === false) return "paused";
2025-02-06 14:55:10 -08:00
if (monitor?.status === undefined) return "pending";
return monitor?.status == true ? "up" : "down";
}, []);
2025-05-21 23:59:22 +05:30
const theme = useTheme();
const statusColor = {
up: theme.palette.success.lowContrast,
down: theme.palette.error.lowContrast,
paused: theme.palette.warning.lowContrast,
pending: theme.palette.warning.lowContrast,
};
2025-06-12 12:44:35 +08:00
const statusToTheme = {
up: "success",
down: "error",
paused: "warning",
pending: "secondary",
"cannot resolve": "tertiary",
};
const pagespeedStatusMsg = {
up: "Live (collecting data)",
down: "Inactive",
paused: "Paused",
};
return {
getMonitorWithPercentage,
determineState,
statusColor,
statusToTheme,
pagespeedStatusMsg,
};
2025-02-06 14:55:10 -08:00
};
export { useMonitorUtils };