import Stack from "@mui/material/Stack"; import { BaseChart, BaseBox } from "@/Components/design-elements"; import { Clock } from "lucide-react"; import { Dot } from "@/Components/design-elements"; import { AreaChart, Area, XAxis, YAxis, Tooltip, CartesianGrid, ResponsiveContainer, } from "recharts"; import type { TooltipProps } from "recharts"; import type { NameType, ValueType } from "recharts/types/component/DefaultTooltipContent"; import Typography from "@mui/material/Typography"; import { XTick } from "@/Components/monitors/charts/XTick"; import { useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; import { formatDateWithTz, formatMs, tooltipDateFormatLookup } from "@/Utils/TimeUtils"; import { useTheme } from "@mui/material/styles"; import type { Theme } from "@mui/material/styles"; import type { GroupedCheck, GroupedUptimeCheck } from "@/Types/Check"; import type { RootState } from "@/Types/state"; import { computeYAxisCap } from "@/Components/monitors/charts/ChartUtils"; import { useMemo, useState } from "react"; import { LAYOUT, SPACING } from "@/Utils/Theme/constants"; import { PHASE_COLOR_KEYS, PHASE_KEYS, PHASE_LABEL_KEYS, } from "@/Components/monitors/charts/ChartUtils"; type DetailsCheck = GroupedCheck & Partial>; type ResponseTimeToolTipProps = TooltipProps & { range: string; theme: Theme; uiTimezone: string; }; const ResponseTimeToolTip = ({ active, payload, label, range, theme, uiTimezone, }: ResponseTimeToolTipProps) => { const { t } = useTranslation(); if (!label) return null; if (!payload) return null; if (!active) return null; const format = tooltipDateFormatLookup(range); const responseTime = payload[0]?.payload?.avgResponseTime ?? 0; return ( {formatDateWithTz(String(label), format, uiTimezone)} {t("common.charts.histogram.responseTime", { value: formatMs(responseTime), })} ); }; type TimingPhaseToolTipProps = TooltipProps & { range: string; theme: Theme; uiTimezone: string; }; const TimingPhasesToolTip = ({ active, payload, range, theme, uiTimezone, }: TimingPhaseToolTipProps) => { const { t } = useTranslation(); if (!active || !payload?.length) return null; const bucket = payload[0].payload as GroupedUptimeCheck; const total = PHASE_KEYS.reduce((sum, key) => sum + bucket[key], 0); const format = tooltipDateFormatLookup(range); return ( {formatDateWithTz(bucket.bucketDate, format, uiTimezone)} {t("common.charts.histogram.total", { value: formatMs(total) })} {PHASE_KEYS.map((key) => ( {t(PHASE_LABEL_KEYS[key])}: {formatMs(bucket[key])} ))} ); }; export const HistogramDetails = ({ checks, range, }: { checks: DetailsCheck[]; range: string; }) => { const [showTimingPhases, setShowTimingPhases] = useState(false); const [hoveredPhase, setHoveredPhase] = useState(null); const { t } = useTranslation(); const theme = useTheme(); const uiTimezone = useSelector((state: RootState) => state.ui.timezone); const yMax = useMemo(() => { const totals = checks.map((check) => { const phaseSum = PHASE_KEYS.reduce((sum, key) => sum + (check[key] ?? 0), 0); return Math.max(check.avgResponseTime ?? 0, phaseSum); }); return computeYAxisCap(totals); }, [checks]); const hasTimingPhases = useMemo( () => (checks ?? []).some((check) => PHASE_KEYS.some((key) => (check[key] ?? 0) > 0)), [checks] ); return ( } title={ showTimingPhases ? t("common.charts.labels.timingPhases") : t("common.charts.labels.responseTime") } onClick={() => { if (!hasTimingPhases) return; setShowTimingPhases((prev) => !prev); }} > {PHASE_KEYS.map((key) => ( ))} ( )} /> {showTimingPhases ? ( ( )} /> ) : ( ( )} /> )} {!showTimingPhases && ( )} {hasTimingPhases && showTimingPhases && PHASE_KEYS.map((key) => { const dimmed = hoveredPhase !== null && hoveredPhase !== key; return ( ); })} {showTimingPhases ? ( PHASE_KEYS.map((key) => { return ( setHoveredPhase(key)} onMouseLeave={() => setHoveredPhase(null)} > {t(PHASE_LABEL_KEYS[key])} ); }) ) : ( setHoveredPhase("avgResponseTime")} onMouseLeave={() => setHoveredPhase(null)} > {t("common.charts.labels.totalResponseTime")} )} ); };