Files
Checkmate/client/src/Pages/Incidents/Components/IncidentTable.tsx
T

222 lines
5.6 KiB
TypeScript
Raw Normal View History

2026-01-30 23:35:33 +00:00
import { Table } from "@/Components/v2/design-elements";
2026-01-31 00:41:03 +00:00
import { Pagination } from "@/Components/v2/design-elements/Table";
2026-01-31 00:18:33 +00:00
import type { Header } from "@/Components/v2/design-elements/Table";
2026-01-31 01:12:05 +00:00
import { ValueLabel } from "@/Components/v2/design-elements";
2026-01-31 00:18:33 +00:00
import type { ValueType } from "@/Components/v2/design-elements/StatusLabel";
import { ActionsMenu } from "@/Components/v2/actions-menu";
2026-01-30 23:35:33 +00:00
import type { Incident } from "@/Types/Incident";
import type { Monitor } from "@/Types/Monitor";
2026-01-31 00:18:33 +00:00
import type { ActionMenuItem } from "@/Components/v2/actions-menu";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { TypeToPathMap } from "@/Utils/monitorUtilsLegacy.js";
import { formatDateWithTz } from "@/Utils/TimeUtils";
import { useSelector } from "react-redux";
import type { RootState } from "@/Types/state";
import Typography from "@mui/material/Typography";
import { useTheme } from "@mui/material/styles";
2026-01-31 00:41:03 +00:00
import Box from "@mui/material/Box";
2026-01-30 23:35:33 +00:00
interface IncidentsTableProps {
2026-02-02 17:49:36 +00:00
title?: string;
2026-01-30 23:35:33 +00:00
incidents?: Incident[];
monitors?: Monitor[];
2026-01-31 00:41:03 +00:00
incidentsCount: number;
page: number;
setPage: (page: number) => void;
rowsPerPage: number;
setRowsPerPage: (rowsPerPage: number) => void;
2026-01-31 00:18:33 +00:00
onOpenDetails?: (incidentId: string) => void;
onResolve?: (incidentId: string) => void;
2026-01-30 23:35:33 +00:00
}
2026-01-31 00:18:33 +00:00
export const IncidentsTable = ({
incidents,
monitors,
2026-01-31 00:41:03 +00:00
incidentsCount,
page,
setPage,
rowsPerPage,
setRowsPerPage,
2026-01-31 00:18:33 +00:00
onOpenDetails,
onResolve,
}: IncidentsTableProps) => {
const { t } = useTranslation();
const theme = useTheme();
const navigate = useNavigate();
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
const getActions = (incident: Incident): ActionMenuItem[] => {
const monitor = monitors?.find((m) => m.id === incident.monitorId);
const isActive = incident.status === true;
const actions: ActionMenuItem[] = [
{
id: "details",
2026-01-31 01:12:05 +00:00
label: t("pages.incidents.table.actions.details"),
2026-01-31 00:18:33 +00:00
action: () => onOpenDetails?.(incident.id),
closeMenu: true,
},
{
id: "goToMonitor",
2026-01-31 01:12:05 +00:00
label: t("pages.incidents.table.actions.goToMonitor"),
2026-01-31 00:18:33 +00:00
action: () => {
if (monitor) {
const path = TypeToPathMap[monitor.type as keyof typeof TypeToPathMap];
if (path && monitor.id) {
navigate(`/${path}/${monitor.id}`);
}
}
},
closeMenu: true,
},
];
if (isActive) {
actions.push({
id: "resolve",
2026-01-31 01:12:05 +00:00
label: t("pages.incidents.table.actions.resolveManually"),
2026-01-31 00:18:33 +00:00
action: () => onResolve?.(incident.id),
closeMenu: true,
});
}
return actions;
};
const getHeaders = (): Header<Incident>[] => {
return [
{
id: "monitorName",
content: t("common.table.headers.monitor"),
render: (row) => {
const monitor = monitors?.find((m) => m.id === row.monitorId);
return monitor?.name || "N/A";
},
},
{
id: "status",
content: t("common.table.headers.status"),
render: (row) => {
2026-01-31 01:12:05 +00:00
const isActive = row.status === true;
return (
<ValueLabel
value={isActive ? "negative" : "positive"}
2026-02-02 17:36:42 +00:00
text={
isActive
? t("pages.incidents.table.status.active")
: t("pages.incidents.table.status.resolved")
}
2026-01-31 01:12:05 +00:00
/>
);
2026-01-31 00:18:33 +00:00
},
},
{
id: "startTime",
2026-01-31 01:12:05 +00:00
content: t("pages.incidents.table.headers.startTime"),
2026-01-31 00:18:33 +00:00
render: (row) => {
2026-02-02 17:36:42 +00:00
return formatDateWithTz(row.createdAt, "YYYY-MM-DD HH:mm:ss A", uiTimezone);
2026-01-31 00:18:33 +00:00
},
},
{
id: "endTime",
2026-01-31 01:12:05 +00:00
content: t("pages.incidents.table.headers.endTime"),
2026-01-31 00:18:33 +00:00
render: (row) => {
if (row.endTime) {
2026-02-02 17:36:42 +00:00
return formatDateWithTz(row.endTime, "YYYY-MM-DD HH:mm:ss A", uiTimezone);
2026-01-31 00:18:33 +00:00
}
return "-";
},
},
{
id: "resolutionType",
2026-01-31 01:12:05 +00:00
content: t("pages.incidents.table.headers.resolutionType"),
2026-01-31 00:18:33 +00:00
render: (row) => {
if (row.resolutionType) {
return (
<Typography
variant="body2"
sx={{
textTransform: "capitalize",
color:
row.resolutionType === "manual"
? theme.palette.warning.main
: theme.palette.success.main,
}}
>
{row.resolutionType}
</Typography>
);
}
return "-";
},
},
{
id: "statusCode",
2026-01-31 01:12:05 +00:00
content: t("pages.incidents.table.headers.statusCode"),
2026-01-31 00:18:33 +00:00
render: (row) => {
const code = row.statusCode;
if (!code) return "N/A";
let value: ValueType = "neutral";
if (code < 300) value = "positive";
else if (code >= 400) value = "negative";
2026-02-02 17:36:42 +00:00
return (
<ValueLabel
value={value}
text={String(code)}
/>
);
2026-01-31 00:18:33 +00:00
},
},
{
id: "message",
content: t("common.table.headers.message"),
render: (row) => row.message || "-",
},
{
id: "actions",
content: t("common.table.headers.actions"),
render: (row) => <ActionsMenu items={getActions(row)} />,
},
];
};
2026-01-31 00:41:03 +00:00
const handlePageChange = (
_e: React.MouseEvent<HTMLButtonElement> | null,
newPage: number
) => {
setPage(newPage);
};
const handleRowsPerPageChange = (
e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
) => {
const value = Number(e.target.value);
setPage(0);
setRowsPerPage(value);
};
2026-01-30 23:35:33 +00:00
if (!incidents || !monitors) {
return null;
}
2026-01-31 00:41:03 +00:00
return (
<Box>
<Table
headers={getHeaders()}
data={incidents}
onRowClick={(row) => onOpenDetails?.(row.id)}
emptyViewText={t("common.table.empty")}
/>
<Pagination
component="div"
count={incidentsCount}
page={page}
rowsPerPage={rowsPerPage}
onPageChange={handlePageChange}
onRowsPerPageChange={handleRowsPerPageChange}
2026-02-05 18:30:43 +00:00
itemsOnPage={incidents.length}
2026-01-31 00:41:03 +00:00
/>
</Box>
);
2026-01-30 23:35:33 +00:00
};