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 = "";
if (monitor.uptimePercentage !== undefined) {
uptimePercentage =
monitor.uptimePercentage === 0
? "0"
: (monitor.uptimePercentage * 100).toFixed(2);
percentageColor =
monitor.uptimePercentage < 0.25
? theme.palette.error.main
: monitor.uptimePercentage < 0.5
? theme.palette.warning.main
: monitor.uptimePercentage < 0.75
? 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";
if (monitor.isActive === false) return "paused";
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 };