fix: refactoring monitorsList and minor fixes based on feedback

This commit is contained in:
mannilakash committed 2026-03-17 15:01:30 -04:00
1 parent d3d00353a1
commit 622446c308
4 files changed
+114 -141

No files matched your search

+2 -17
View File
@@ -29,6 +29,7 @@ import type { StatusPageFormData } from "@/Validation/statusPage";
import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi"; import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi";
import type { Monitor } from "@/Types/Monitor"; import type { Monitor } from "@/Types/Monitor";
import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage"; import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage";
import { getMonitorTypeLabel } from "@/Types/StatusPage";
import timezones from "@/Utils/timezones.json"; import timezones from "@/Utils/timezones.json";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import axios from "axios"; import axios from "axios";
@@ -39,22 +40,6 @@ interface TimezoneOption {
name: string; name: string;
} }
const MONITOR_TYPE_KEYS: Record<string, string> = {
http: "pages.common.monitors.monitorTypes.optionHttp",
ping: "pages.common.monitors.monitorTypes.optionPing",
docker: "pages.common.monitors.monitorTypes.optionDocker",
port: "pages.common.monitors.monitorTypes.optionPort",
game: "pages.common.monitors.monitorTypes.optionGame",
grpc: "pages.common.monitors.monitorTypes.optionGrpc",
websocket: "pages.common.monitors.monitorTypes.optionWebSocket",
hardware: "pages.common.monitors.monitorTypes.optionHardware",
};
const getMonitorTypeLabel = (type: string, t: (key: string) => string): string => {
const i18nMonitorKey = MONITOR_TYPE_KEYS[type];
return i18nMonitorKey ? t(i18nMonitorKey) : type;
};
const CreateStatusPage = () => { const CreateStatusPage = () => {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation(); const { t } = useTranslation();
@@ -111,7 +96,7 @@ const CreateStatusPage = () => {
}); });
return typesSet.size ? Array.from(typesSet) : ["uptime"]; return typesSet.size ? Array.from(typesSet) : ["uptime"];
}, [JSON.stringify(watchedMonitorIds), monitors]); }, [watchedMonitorIds, monitors]);
useEffect(() => { useEffect(() => {
form.setValue("type", computedTypes); form.setValue("type", computedTypes);
@@ -12,6 +12,7 @@ import { useSelector } from "react-redux";
import { useState } from "react"; import { useState } from "react";
import type { Monitor } from "@/Types/Monitor"; import type { Monitor } from "@/Types/Monitor";
import type { StatusPage } from "@/Types/StatusPage"; import type { StatusPage } from "@/Types/StatusPage";
import { getMonitorTypeLabel } from "@/Types/StatusPage";
import type { RootState } from "@/Types/state"; import type { RootState } from "@/Types/state";
import { LAYOUT, SPACING } from "@/Utils/Theme/constants"; import { LAYOUT, SPACING } from "@/Utils/Theme/constants";
@@ -25,123 +26,27 @@ interface MonitorsListProps {
} }
const getMonitorBadgeStyles = (monitorType: string, theme: Theme) => { const getMonitorBadgeStyles = (monitorType: string, theme: Theme) => {
// for future: specific badge styles can be defined here based on monitor type const bg =
const badgeConfig: Record<string, { color: string; bg: string }> = { monitorType === "hardware" ? theme.palette.info.light : theme.palette.success.light;
hardware: {
color: theme.palette.background.paper,
bg: theme.palette.info.light,
},
http: {
color: theme.palette.background.paper,
bg: theme.palette.success.light,
},
ping: {
color: theme.palette.background.paper,
bg: theme.palette.success.light,
},
port: {
color: theme.palette.background.paper,
bg: theme.palette.success.light,
},
docker: {
color: theme.palette.background.paper,
bg: theme.palette.success.light,
},
pagespeed: {
color: theme.palette.background.paper,
bg: theme.palette.success.light,
},
game: {
color: theme.palette.background.paper,
bg: theme.palette.success.light,
},
};
const config = badgeConfig[monitorType] ?? badgeConfig.http;
return { return {
backgroundColor: config.bg, backgroundColor: bg,
color: config.color, color: theme.palette.background.paper,
padding: `${theme.spacing(SPACING.SM)} ${theme.spacing(SPACING.LG)}`, padding: `${theme.spacing(SPACING.SM)} ${theme.spacing(SPACING.LG)}`,
borderRadius: theme.shape.borderRadius, borderRadius: theme.shape.borderRadius,
}; };
}; };
// Extracted component for monitor content below the header row const MonitorHeader = ({
const MonitorContent = ({
monitor, monitor,
statusPage, showURL,
chartType,
}: { }: {
monitor: Monitor & { checks?: Monitor["recentChecks"] }; monitor: StatusPageMonitor;
statusPage: StatusPage; showURL: boolean;
chartType: string;
}) => { }) => {
const theme = useTheme();
const isInfrastructureMonitor = monitor?.type === "hardware";
if (isInfrastructureMonitor) {
if (statusPage.showInfrastructure === false) {
return null;
}
return <InfrastructureMetrics monitor={monitor} />;
}
if (statusPage.showCharts === false) {
return null;
}
return (
<Box sx={{ overflow: "hidden", minWidth: 0, flex: 1, mb: theme.spacing(SPACING.LG) }}>
{chartType === "histogram" ? (
<HistogramResponseTime
height={{ xs: 50, md: 100 }}
gap={{ xs: theme.spacing(SPACING.SM), md: theme.spacing(LAYOUT.SM) }}
checks={monitor?.checks?.slice().reverse() ?? []}
/>
) : (
<HeatmapResponseTime checks={monitor?.checks?.slice().reverse() ?? []} />
)}
</Box>
);
};
export const MonitorsList = ({ statusPage, monitors }: MonitorsListProps) => {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation(); const { t } = useTranslation();
const showURL = useSelector((state: RootState) => state.ui?.showURL);
const [chartType, setChartType] = useState<"histogram" | "heatmap">("histogram");
return ( return (
<Stack gap={theme.spacing(LAYOUT.MD)}>
{statusPage.showCharts && (
<Stack
direction={"row"}
alignItems={"center"}
gap={theme.spacing(LAYOUT.SM)}
>
<Typography>{t("pages.statusPages.monitorsList.chartTypeHeatmap")}</Typography>
<SwitchComponent
dualOption
value={chartType}
checked={chartType === "histogram"}
onChange={(e) => {
setChartType(e.target.checked ? "histogram" : "heatmap");
}}
/>
<Typography>
{t("pages.statusPages.monitorsList.chartTypeHistogram")}
</Typography>
</Stack>
)}
{monitors?.map((monitor) => {
return (
<BaseBox
key={monitor.id}
padding={theme.spacing(LAYOUT.MD)}
>
<Stack <Stack
direction="row" direction="row"
alignItems="center" alignItems="center"
@@ -158,47 +63,109 @@ export const MonitorsList = ({ statusPage, monitors }: MonitorsListProps) => {
> >
<Typography <Typography
variant="h6" variant="h6"
sx={{ sx={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
}}
> >
{monitor.name} {monitor.name}
</Typography> </Typography>
<Typography <Typography
variant="caption" variant="caption"
sx={getMonitorBadgeStyles(monitor?.type ?? "", theme)} sx={getMonitorBadgeStyles(monitor.type ?? "", theme)}
> >
{t( {getMonitorTypeLabel(monitor.type, t)}
`pages.common.monitors.monitorTypes.option${monitor?.type.charAt(0).toUpperCase() + monitor?.type.slice(1)}`
)}
</Typography> </Typography>
</Stack> </Stack>
{showURL && monitor?.url && ( {showURL && monitor.url && (
<Typography <Typography
variant="body2" variant="body2"
color={theme.palette.text.secondary} color={theme.palette.text.secondary}
sx={{ sx={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
}}
> >
{monitor.url} {monitor.url}
</Typography> </Typography>
)} )}
</Box> </Box>
<StatusLabel status={monitor?.status} /> <StatusLabel status={monitor.status} />
</Stack> </Stack>
);
};
const MonitorContent = ({
monitor,
statusPage,
chartType,
}: {
monitor: StatusPageMonitor;
statusPage: StatusPage;
chartType: string;
}) => {
const theme = useTheme();
if (monitor.type === "hardware") {
if (statusPage.showInfrastructure === false) return null;
return <InfrastructureMetrics monitor={monitor} />;
}
if (statusPage.showCharts === false) return null;
const checks = monitor.checks?.slice().reverse() ?? [];
return (
<Box sx={{ overflow: "hidden", minWidth: 0, flex: 1, mb: theme.spacing(SPACING.LG) }}>
{chartType === "histogram" ? (
<HistogramResponseTime
height={{ xs: 50, md: 100 }}
gap={{ xs: theme.spacing(SPACING.SM), md: theme.spacing(LAYOUT.SM) }}
checks={checks}
/>
) : (
<HeatmapResponseTime checks={checks} />
)}
</Box>
);
};
export const MonitorsList = ({ statusPage, monitors }: MonitorsListProps) => {
const theme = useTheme();
const { t } = useTranslation();
const showURL = useSelector((state: RootState) => state.ui?.showURL);
const [chartType, setChartType] = useState<"histogram" | "heatmap">("histogram");
return (
<Stack gap={theme.spacing(LAYOUT.MD)}>
{statusPage.showCharts && (
<Stack
direction="row"
alignItems="center"
gap={theme.spacing(LAYOUT.SM)}
>
<Typography>{t("pages.statusPages.monitorsList.chartTypeHeatmap")}</Typography>
<SwitchComponent
dualOption
value={chartType}
checked={chartType === "histogram"}
onChange={(e) => setChartType(e.target.checked ? "histogram" : "heatmap")}
/>
<Typography>
{t("pages.statusPages.monitorsList.chartTypeHistogram")}
</Typography>
</Stack>
)}
{monitors.map((monitor) => (
<BaseBox
key={monitor.id}
padding={theme.spacing(LAYOUT.MD)}
>
<MonitorHeader
monitor={monitor}
showURL={showURL}
/>
<MonitorContent <MonitorContent
monitor={monitor} monitor={monitor}
statusPage={statusPage} statusPage={statusPage}
chartType={chartType} chartType={chartType}
/> />
</BaseBox> </BaseBox>
); ))}
})}
</Stack> </Stack>
); );
}; };
+21 -1
View File
@@ -1,6 +1,26 @@
import type { Monitor } from "@/Types/Monitor"; import type { Monitor, MonitorType } from "@/Types/Monitor";
export type MonitorDisplayType = "uptime" | "infrastructure"; export type MonitorDisplayType = "uptime" | "infrastructure";
export const MONITOR_TYPE_KEYS: Partial<Record<MonitorType, string>> = {
http: "pages.common.monitors.monitorTypes.optionHttp",
ping: "pages.common.monitors.monitorTypes.optionPing",
docker: "pages.common.monitors.monitorTypes.optionDocker",
port: "pages.common.monitors.monitorTypes.optionPort",
game: "pages.common.monitors.monitorTypes.optionGame",
grpc: "pages.common.monitors.monitorTypes.optionGrpc",
websocket: "pages.common.monitors.monitorTypes.optionWebSocket",
hardware: "pages.common.monitors.monitorTypes.optionHardware",
pagespeed: "pages.common.monitors.monitorTypes.optionPagespeed",
};
export const getMonitorTypeLabel = (
type: MonitorType,
t: (key: string) => string
): string => {
const key = MONITOR_TYPE_KEYS[type];
return key ? t(key) : type;
};
export interface StatusPage { export interface StatusPage {
id: string; id: string;
userId: string; userId: string;
+2 -1
View File
@@ -551,7 +551,8 @@
"optionWebSocketDescription": "Monitor WebSocket endpoints for connection health and response time.", "optionWebSocketDescription": "Monitor WebSocket endpoints for connection health and response time.",
"optionHttpDescription": "Use HTTP(S) to monitor your website or API endpoint.", "optionHttpDescription": "Use HTTP(S) to monitor your website or API endpoint.",
"optionPingDescription": "Use ICMP Ping to monitor if a server is online.", "optionPingDescription": "Use ICMP Ping to monitor if a server is online.",
"optionPortDescription": "Monitor if a specific port on a server is open." "optionPortDescription": "Monitor if a specific port on a server is open.",
"title": "Type"
}, },
"thresholds": { "thresholds": {
"title": "Alert thresholds", "title": "Alert thresholds",