|
1 | | -import { AlertTriangle, CircleCheck } from "lucide-react"; |
| 1 | +import { |
| 2 | + AlertTriangle, |
| 3 | + CircleCheck, |
| 4 | + CircleX, |
| 5 | + Loader, |
| 6 | + PauseCircle, |
| 7 | + ShieldAlert, |
| 8 | + Wrench, |
| 9 | +} from "lucide-react"; |
2 | 10 | import Stack from "@mui/material/Stack"; |
3 | 11 | import Typography from "@mui/material/Typography"; |
4 | 12 |
|
5 | 13 | import { useTranslation } from "react-i18next"; |
6 | | -import type { Theme } from "@mui/material"; |
7 | 14 | import { useTheme } from "@mui/material"; |
8 | | -import type { Monitor } from "@/Types/Monitor"; |
| 15 | +import type { Theme } from "@mui/material"; |
| 16 | +import type { Monitor, MonitorStatus } from "@/Types/Monitor"; |
| 17 | + |
| 18 | +interface StatusDisplay { |
| 19 | + icon: JSX.Element; |
| 20 | + msg?: string; |
| 21 | + color?: string; |
| 22 | +} |
9 | 23 |
|
10 | 24 | const getMonitorStatus = (monitors: Monitor[], theme: Theme, t: Function) => { |
11 | | - const monitorsStatus: Record<string, any> = { |
| 25 | + const monitorsStatus: StatusDisplay = { |
12 | 26 | icon: <AlertTriangle size={24} />, |
13 | 27 | }; |
14 | 28 |
|
15 | | - if (monitors.every((monitor) => monitor.status === "up")) { |
| 29 | + // Handle empty monitors array |
| 30 | + if (monitors.length === 0) { |
| 31 | + monitorsStatus.msg = t("pages.statusPages.statusBar.noMonitors"); |
| 32 | + monitorsStatus.color = theme.palette.warning.main; |
| 33 | + monitorsStatus.icon = <CircleX size={24} />; |
| 34 | + return monitorsStatus; |
| 35 | + } |
| 36 | + |
| 37 | + const allOf = (...statuses: MonitorStatus[]) => |
| 38 | + monitors.every((m) => statuses.includes(m.status)); |
| 39 | + const someOf = (...statuses: MonitorStatus[]) => |
| 40 | + monitors.some((m) => statuses.includes(m.status)); |
| 41 | + const noneOf = (...statuses: MonitorStatus[]) => |
| 42 | + monitors.every((m) => !statuses.includes(m.status)); |
| 43 | + |
| 44 | + // All monitors in a single state |
| 45 | + if (allOf("up")) { |
16 | 46 | monitorsStatus.msg = t("pages.statusPages.statusBar.allUp"); |
17 | 47 | monitorsStatus.color = theme.palette.success.main; |
18 | 48 | monitorsStatus.icon = <CircleCheck size={24} />; |
19 | | - return monitorsStatus; |
20 | | - } else if (monitors.every((monitor) => monitor.status === "down")) { |
| 49 | + } else if (allOf("breached")) { |
| 50 | + monitorsStatus.msg = t("pages.statusPages.statusBar.allBreached"); |
| 51 | + monitorsStatus.color = theme.palette.error.main; |
| 52 | + monitorsStatus.icon = <ShieldAlert size={24} />; |
| 53 | + } else if (allOf("maintenance")) { |
| 54 | + monitorsStatus.msg = t("pages.statusPages.statusBar.allMaintenance"); |
| 55 | + monitorsStatus.color = theme.palette.warning.main; |
| 56 | + monitorsStatus.icon = <Wrench size={24} />; |
| 57 | + } else if (allOf("down")) { |
21 | 58 | monitorsStatus.msg = t("pages.statusPages.statusBar.allDown"); |
22 | 59 | monitorsStatus.color = theme.palette.error.main; |
23 | | - return monitorsStatus; |
24 | | - } else if (monitors.some((monitor) => monitor.status === "down")) { |
| 60 | + monitorsStatus.icon = <CircleX size={24} />; |
| 61 | + } else if (allOf("paused")) { |
| 62 | + monitorsStatus.msg = t("pages.statusPages.statusBar.allPaused"); |
| 63 | + monitorsStatus.color = theme.palette.warning.main; |
| 64 | + monitorsStatus.icon = <PauseCircle size={24} />; |
| 65 | + } else if (allOf("initializing")) { |
| 66 | + monitorsStatus.msg = t("pages.statusPages.statusBar.allInitializing"); |
| 67 | + monitorsStatus.color = theme.palette.warning.main; |
| 68 | + monitorsStatus.icon = <Loader size={24} />; |
| 69 | + |
| 70 | + // Breached takes highest priority in mixed states |
| 71 | + } else if (someOf("breached") && someOf("down")) { |
| 72 | + monitorsStatus.msg = t("pages.statusPages.statusBar.breachedAndDown"); |
| 73 | + monitorsStatus.color = theme.palette.error.main; |
| 74 | + monitorsStatus.icon = <ShieldAlert size={24} />; |
| 75 | + } else if (someOf("breached")) { |
| 76 | + monitorsStatus.msg = t("pages.statusPages.statusBar.breached"); |
| 77 | + monitorsStatus.color = theme.palette.error.main; |
| 78 | + monitorsStatus.icon = <ShieldAlert size={24} />; |
| 79 | + |
| 80 | + // Maintenance combinations |
| 81 | + } else if (someOf("maintenance") && someOf("down")) { |
| 82 | + monitorsStatus.msg = t("pages.statusPages.statusBar.maintenanceAndDown"); |
| 83 | + monitorsStatus.color = theme.palette.error.main; |
| 84 | + monitorsStatus.icon = <Wrench size={24} />; |
| 85 | + } else if (someOf("maintenance") && noneOf("down")) { |
| 86 | + monitorsStatus.msg = t("pages.statusPages.statusBar.maintenance"); |
| 87 | + monitorsStatus.color = theme.palette.warning.main; |
| 88 | + monitorsStatus.icon = <Wrench size={24} />; |
| 89 | + |
| 90 | + // Degraded (some down, no maintenance/breached) |
| 91 | + } else if (someOf("down")) { |
25 | 92 | monitorsStatus.msg = t("pages.statusPages.statusBar.degraded"); |
26 | 93 | monitorsStatus.color = theme.palette.warning.main; |
27 | | - return monitorsStatus; |
| 94 | + monitorsStatus.icon = <AlertTriangle size={24} />; |
| 95 | + |
| 96 | + // Some Paused |
| 97 | + } else if (someOf("paused")) { |
| 98 | + monitorsStatus.msg = t("pages.statusPages.statusBar.partiallyPaused"); |
| 99 | + monitorsStatus.color = theme.palette.warning.main; |
| 100 | + monitorsStatus.icon = <PauseCircle size={24} />; |
| 101 | + |
| 102 | + // Initializing |
| 103 | + } else if (someOf("initializing")) { |
| 104 | + monitorsStatus.msg = t("pages.statusPages.statusBar.initializing"); |
| 105 | + monitorsStatus.color = theme.palette.info.main; |
| 106 | + monitorsStatus.icon = <Loader size={24} />; |
28 | 107 | } else { |
29 | 108 | monitorsStatus.msg = t("pages.statusPages.statusBar.unknown"); |
30 | 109 | monitorsStatus.color = theme.palette.warning.main; |
31 | | - return monitorsStatus; |
32 | 110 | } |
| 111 | + |
| 112 | + return monitorsStatus; |
33 | 113 | }; |
34 | 114 |
|
35 | 115 | interface StatusBarProps { |
|
0 commit comments