import { Table } from "@/Components/v2/design-elements"; import { Pagination } from "@/Components/v2/design-elements/Table"; import type { Header } from "@/Components/v2/design-elements/Table"; import { ValueLabel } from "@/Components/v2/design-elements"; import type { ValueType } from "@/Components/v2/design-elements/StatusLabel"; import { ActionsMenu } from "@/Components/v2/actions-menu"; import type { Incident } from "@/Types/Incident"; import type { Monitor } from "@/Types/Monitor"; import type { ActionMenuItem } from "@/Components/v2/actions-menu"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; 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"; import Box from "@mui/material/Box"; import { getMonitorPath } from "@/Utils/MonitorUtils"; interface IncidentsTableProps { title?: string; incidents?: Incident[]; monitors?: Monitor[]; incidentsCount: number; page: number; setPage: (page: number) => void; rowsPerPage: number; setRowsPerPage: (rowsPerPage: number) => void; onOpenDetails?: (incidentId: string) => void; onResolve?: (incidentId: string) => void; } export const IncidentsTable = ({ incidents, monitors, incidentsCount, page, setPage, rowsPerPage, setRowsPerPage, 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", label: t("pages.incidents.table.actions.details"), action: () => onOpenDetails?.(incident.id), closeMenu: true, }, { id: "goToMonitor", label: t("pages.incidents.table.actions.goToMonitor"), action: () => { if (monitor) { const path = getMonitorPath(monitor.type); if (path && monitor.id) { navigate(`/${path}/${monitor.id}`); } } }, closeMenu: true, }, ]; if (isActive) { actions.push({ id: "resolve", label: t("pages.incidents.table.actions.resolveManually"), action: () => onResolve?.(incident.id), closeMenu: true, }); } return actions; }; const getHeaders = (): Header[] => { 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) => { const isActive = row.status === true; return ( ); }, }, { id: "startTime", content: t("pages.incidents.table.headers.startTime"), render: (row) => { return formatDateWithTz(row.createdAt, "YYYY-MM-DD HH:mm:ss A", uiTimezone); }, }, { id: "endTime", content: t("pages.incidents.table.headers.endTime"), render: (row) => { if (row.endTime) { return formatDateWithTz(row.endTime, "YYYY-MM-DD HH:mm:ss A", uiTimezone); } return "-"; }, }, { id: "resolutionType", content: t("pages.incidents.table.headers.resolutionType"), render: (row) => { if (row.resolutionType) { return ( {row.resolutionType} ); } return "-"; }, }, { id: "statusCode", content: t("pages.incidents.table.headers.statusCode"), 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"; return ( ); }, }, { id: "message", content: t("common.table.headers.message"), render: (row) => row.message || "-", }, { id: "actions", content: t("common.table.headers.actions"), render: (row) => , }, ]; }; const handlePageChange = ( _e: React.MouseEvent | null, newPage: number ) => { setPage(newPage); }; const handleRowsPerPageChange = ( e: React.ChangeEvent ) => { const value = Number(e.target.value); setPage(0); setRowsPerPage(value); }; if (!incidents || !monitors) { return null; } return ( onOpenDetails?.(row.id)} emptyViewText={t("common.table.empty")} /> ); };