diff --git a/client/src/Pages/Incidents/Components/IncidentItem.tsx b/client/src/Pages/Incidents/Components/IncidentItem.tsx new file mode 100644 index 000000000..71793abd1 --- /dev/null +++ b/client/src/Pages/Incidents/Components/IncidentItem.tsx @@ -0,0 +1,104 @@ +import Stack from "@mui/material/Stack"; +import Box from "@mui/material/Box"; +import Divider from "@mui/material/Divider"; +import Grid from "@mui/material/Grid2"; +import Typography from "@mui/material/Typography"; +import { ValueLabel } from "@/Components/v2/design-elements"; +import { Globe } from "lucide-react"; + +import type { Incident } from "@/Types/Incident"; +import type { Monitor } from "@/Types/Monitor"; +import { useTheme } from "@mui/material"; +import { getIncidentsDuration } from "@/Pages/Incidents/utils"; + +export const IncidentItem = ({ + incident, + monitor, +}: { + incident: Incident; + monitor?: Monitor | null; +}) => { + const theme = useTheme(); + const duration = getIncidentsDuration(incident); + return ( + + + + + {monitor ? monitor.name : "N/A"} + + + + + + + + + {duration} + + + ); +}; + +interface SummaryItemProps { + icon: React.ReactNode; + label: string; + value: string | number; +} + +export const SummaryItem = ({ icon, label, value }: SummaryItemProps) => { + const theme = useTheme(); + return ( + + + {icon} + + {label}: {value} + + + + + ); +}; diff --git a/client/src/Pages/Incidents/Components/IncidentTable.tsx b/client/src/Pages/Incidents/Components/IncidentTable.tsx new file mode 100644 index 000000000..e69de29bb diff --git a/client/src/Pages/Incidents/Components/SummaryCard.tsx b/client/src/Pages/Incidents/Components/SummaryCard.tsx new file mode 100644 index 000000000..c1477cc5d --- /dev/null +++ b/client/src/Pages/Incidents/Components/SummaryCard.tsx @@ -0,0 +1,141 @@ +import Stack from "@mui/material/Stack"; +import Typography from "@mui/material/Typography"; +import Divider from "@mui/material/Divider"; +import { BaseBox } from "@/Components/v2/design-elements"; +import { CircleCheck, TriangleAlert, Bell, Wrench } from "lucide-react"; +import Box from "@mui/material/Box"; +import { IncidentItem, SummaryItem } from "@/Pages/Incidents/Components/IncidentItem"; + +import { useTranslation } from "react-i18next"; +import { useTheme } from "@mui/material"; +import type { Incident, IncidentSummary } from "@/Types/Incident"; +import type { Monitor } from "@/Types/Monitor"; + +interface SummaryCardProps { + title: string; + sx?: React.CSSProperties; +} + +const SummaryCard = ({ + title, + sx, + children, +}: React.PropsWithChildren) => { + const theme = useTheme(); + return ( + + + + {title} + + + {children} + + + ); +}; + +interface SummaryCardIncidentsProps { + incidents?: Incident[]; + monitors?: Monitor[] | null; +} + +export const SummaryCardActiveIncidents = ({ incidents }: SummaryCardIncidentsProps) => { + const { t } = useTranslation(); + const theme = useTheme(); + + if (!incidents) return null; + + const hasActive = incidents.some((incident) => incident.status === true); + const activeCount = hasActive + ? incidents.filter((incident) => incident.status === true).length + : 0; + const color = hasActive ? theme.palette.error.main : theme.palette.success.main; + const icon = hasActive ? ( + + ) : ( + + ); + const msg = hasActive + ? `${activeCount} ${t("pages.incidents.summaryCard.activeIncidents.active")}` + : t("pages.incidents.summaryCard.activeIncidents.noActive"); + + return ( + + + {icon} + {msg} + + + ); +}; + +export const SummaryCardLatestIncidents = ({ + incidents, + monitors, +}: SummaryCardIncidentsProps) => { + const { t } = useTranslation(); + const theme = useTheme(); + return ( + + + {incidents?.slice(0, 3).map((incident, index) => ( + + m.id === incident.monitorId)} + /> + {index < incidents.length - 1 && } + + ))} + + + ); +}; + +interface SummaryCardStatsProps { + summary?: IncidentSummary; +} + +export const SummaryCardStats = ({ summary }: SummaryCardStatsProps) => { + const { t } = useTranslation(); + if (!summary) return null; + const mostAffected = + !summary.total || summary.total === 0 + ? "N/A" + : summary.topMonitor?.monitorName || t("N/A"); + return ( + + } + label="Total incidents" + value={summary?.total || 0} + /> + } + label="Most affected monitor" + value={mostAffected} + /> + } + label="Average resolution time" + value={summary.total > 0 ? `${summary.avgResolutionTimeHours || 0} hours` : "N/A"} + /> + + ); +}; diff --git a/client/src/Pages/Incidents/index.tsx b/client/src/Pages/Incidents/index.tsx index 0bdfbc44d..667af29e0 100644 --- a/client/src/Pages/Incidents/index.tsx +++ b/client/src/Pages/Incidents/index.tsx @@ -1,7 +1,131 @@ import { BasePage } from "@/Components/v2/design-elements"; +import Stack from "@mui/material/Stack"; +import { + SummaryCardActiveIncidents, + SummaryCardLatestIncidents, + SummaryCardStats, +} from "@/Pages/Incidents/Components/SummaryCard"; + +import { useGet } from "@/Hooks/UseApi"; +import { useState, useEffect, useMemo } from "react"; +import { useParams } from "react-router-dom"; +import type { IncidentsResponse, IncidentSummary } from "@/Types/Incident"; +import type { Monitor } from "@/Types/Monitor"; +import { useTheme } from "@mui/material"; + +interface MonitorLookup { + [key: string]: { id: string; name: string; type: string }; +} const IncidentsPage = () => { - return Incidents Page; + const theme = useTheme(); + const { monitorId } = useParams(); + + // Filter state + const [selectedMonitor, setSelectedMonitor] = useState(monitorId || "0"); + const [filter, setFilter] = useState("all"); + const [dateRange, setDateRange] = useState("all"); + const [page, setPage] = useState(0); + const [rowsPerPage, setRowsPerPage] = useState(10); + + // Build incidents URL with query params + const incidentsUrl = useMemo(() => { + const params = new URLSearchParams(); + if (selectedMonitor !== "0") params.append("monitorId", selectedMonitor); + if (filter === "active") params.append("status", "true"); + else if (filter === "resolved") params.append("status", "false"); + else if (filter === "manual" || filter === "automatic") + params.append("resolutionType", filter); + params.append("sortOrder", "desc"); + if (dateRange !== "all") params.append("dateRange", dateRange); + params.append("page", String(page)); + params.append("rowsPerPage", String(rowsPerPage)); + return `/incidents/team?${params.toString()}`; + }, [selectedMonitor, filter, dateRange, page, rowsPerPage]); + + // Fetch incidents + const { + data: incidentsData, + isLoading: isLoadingIncidents, + error: incidentsError, + refetch: refetchIncidents, + } = useGet(incidentsUrl); + + // Fetch monitors for lookup + const { + data: monitorsData, + isLoading: isLoadingMonitors, + error: monitorsError, + } = useGet("/monitors/team"); + + // Fetch incident summary + const { + data: summaryData, + isLoading: isLoadingSummary, + error: summaryError, + refetch: refetchSummary, + } = useGet("/incidents/team/summary"); + + // Build monitor lookup + const monitorLookup = useMemo(() => { + if (!monitorsData) return undefined; + return monitorsData.reduce((acc, monitor) => { + acc[monitor.id] = { + id: monitor.id, + name: monitor.name, + type: monitor.type, + }; + return acc; + }, {}); + }, [monitorsData]); + + // Reset page when filters change + useEffect(() => { + setPage(0); + }, [selectedMonitor, filter, dateRange]); + + // Derived state + const incidents = incidentsData?.incidents ?? []; + const incidentsCount = incidentsData?.count ?? 0; + const networkError = !!incidentsError || !!monitorsError || !!summaryError; + + // Expose state and handlers for future UI use + void selectedMonitor; + void setSelectedMonitor; + void filter; + void setFilter; + void dateRange; + void setDateRange; + void page; + void setPage; + void rowsPerPage; + void setRowsPerPage; + void isLoadingIncidents; + void isLoadingMonitors; + void isLoadingSummary; + void refetchIncidents; + void refetchSummary; + void monitorLookup; + void incidents; + void incidentsCount; + void summaryData; + void networkError; + + return ( + + + + + + + + ); }; export default IncidentsPage; diff --git a/client/src/Pages/Incidents/utils.ts b/client/src/Pages/Incidents/utils.ts new file mode 100644 index 000000000..4843e0d55 --- /dev/null +++ b/client/src/Pages/Incidents/utils.ts @@ -0,0 +1,25 @@ +import type { Incident } from "@/Types/Incident"; +import { getHumanReadableDuration } from "@/Utils/timeUtilsLegacy.js"; +export const getIncidentsDuration = (incident: Incident) => { + if (!incident?.startTime) { + return "-"; + } + const startTime = new Date(incident?.startTime); + const endTime = incident.status + ? new Date() + : incident?.endTime + ? new Date(incident.endTime) + : null; + + if (!endTime) { + return "-"; + } + + const durationMs = endTime.getTime() - startTime.getTime(); + + if (durationMs < 0) { + return "-"; + } + + return getHumanReadableDuration(durationMs); +}; diff --git a/client/src/Routes/index.jsx b/client/src/Routes/index.jsx index aa0f52d3b..ecda66cd1 100644 --- a/client/src/Routes/index.jsx +++ b/client/src/Routes/index.jsx @@ -216,7 +216,13 @@ const Routes = () => { /> } + element={ + <> + + + + + } />