import { ArrowLeft, BellOff, CheckCircle2, Clock3, ExternalLink, RefreshCw, Zap } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { LatencySparkline } from '../components/charts/LatencySparkline';
import { UptimeBar } from '../components/charts/UptimeBar';
import { navigate } from '../lib/router';
import { useLogoutMutation } from '../queries/auth';
import {
useMonitorChecksQuery,
useMonitorIncidentsQuery,
useMonitorQuery,
useMonitorStatsQuery,
useRunCheckMutation,
} from '../queries/monitors';
function formatDate(value: string) {
return new Intl.DateTimeFormat('en', { dateStyle: 'medium', timeStyle: 'short' }).format(new Date(value));
}
function formatDuration(ms: number | null, startedAt: string) {
const duration = ms ?? Math.max(0, Date.now() - new Date(startedAt).getTime());
if (duration < 60_000) return `${Math.max(1, Math.round(duration / 1000))} sec`;
if (duration < 3_600_000) return `${Math.round(duration / 60_000)} min`;
if (duration < 86_400_000) return `${Math.round(duration / 3_600_000)} hr`;
return `${Math.round(duration / 86_400_000)} days`;
}
function statusDetails(lastOk: boolean | null) {
if (lastOk === true) return { label: 'Operational', className: 'online' };
if (lastOk === false) return { label: 'Down', className: 'offline' };
return { label: 'Awaiting first check', className: 'checking' };
}
export function MonitorDetailPage({ id }: { id: number }) {
const monitorQuery = useMonitorQuery(id);
const checksQuery = useMonitorChecksQuery(id);
const statsQuery = useMonitorStatsQuery(id);
const incidentsQuery = useMonitorIncidentsQuery(id);
const checkMutation = useRunCheckMutation();
const logoutMutation = useLogoutMutation();
const monitor = monitorQuery.data?.monitor;
const checks = checksQuery.data?.checks ?? [];
const incidents = incidentsQuery.data?.incidents ?? [];
const status = statusDetails(monitor?.lastOk ?? null);
const openIncident = incidents.find((incident) => incident.resolvedAt === null);
if (monitorQuery.isPending)
return (
);
if (monitorQuery.isError || !monitor)
return (
Monitor not found
The requested monitor could not be loaded.
navigate('/dashboard')}>
Return to dashboard
);
return (
navigate('/dashboard')}>
All monitors
{status.label}
{!monitor.alertsEnabled && (
Alerts muted
)}
checkMutation.mutate(id)}
disabled={checkMutation.isPending}
>
{checkMutation.isPending ? 'Checking…' : 'Check now'}
{(['24h', '7d', '30d', '90d'] as const).map((key) => {
const window = statsQuery.data?.windows[key];
return (
{key} uptime
{window?.uptimePct == null ? '—' : `${window.uptimePct.toFixed(3)}%`}
{window?.totalChecks ?? 0} checks · {window?.avgLatencyMs ?? '—'} ms avg
);
})}
Current incident
{openIncident ? formatDuration(null, openIncident.startedAt) : 'None'}
{openIncident ? `Open since ${formatDate(openIncident.startedAt)}` : 'Everything is operational'}
Last {checks.length} checks
Availability
Recent uptime
{checks.filter((check) => check.ok).length}/{checks.length} successful
Event stream
Recent checks
{Math.min(checks.length, 20)} shown
{/* Keyboard focus makes this horizontally scrollable region accessible without a pointer. */}
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}
Status
Response
Latency
Checked
{checks.slice(0, 20).map((check) => (
{check.ok ? 'Up' : 'Down'}
{check.statusCode ? `HTTP ${check.statusCode}` : (check.error ?? 'Failed')}
{check.latencyMs} ms
{formatDate(check.checkedAt)}
))}
{checks.length === 0 && No checks recorded.
}
{incidents.length} recorded
{incidents.map((incident) => (
{incident.resolvedAt ? : }
{incident.resolvedAt ? 'Resolved incident' : 'Incident in progress'}
{incident.startError ??
(incident.startStatusCode ? `HTTP ${incident.startStatusCode}` : 'Endpoint became unavailable')}
{formatDate(incident.startedAt)} · {formatDuration(incident.durationMs, incident.startedAt)}
))}
{incidents.length === 0 &&
No downtime incidents recorded.
}
);
}