All files / src/pages MonitorDetail.tsx

100% Statements 150/150
100% Branches 29/29
100% Functions 6/6
100% Lines 150/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 1861x 1x 1x 1x 1x 1x 1x 1x   1x   21x 21x 21x 21x   21x 21x 21x 21x 21x 21x 21x   21x 21x 21x   21x 1x 1x 1x 1x 1x 1x   1x   21x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x   3x   17x 17x 17x 17x 17x 17x 17x 17x 17x   17x 17x 17x 17x 17x     17x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x   16x 16x 16x 16x 16x 16x       21x 21x   21x 21x 21x     21x 21x 21x   21x 21x 21x 21x 21x   21x 1x 1x 1x 16x 3x   3x   13x 13x 13x 69x 69x 69x   69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 68x   69x 69x 1x 1x 1x   69x 69x 13x 13x     13x 13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x 13x   21x 21x 21x   21x   1x 65x 65x 65x 65x 65x   65x  
import { useState } from "react";
import { useParams, useNavigate } from "react-router-dom";
import { useMonitor } from "../hooks/useMonitors";
import { useMonitorStats, useMonitorPings } from "../hooks/usePings";
import { formatPercentage, formatDateTime } from "../utils/formatters";
import DashboardShell from "../components/layout/DashboardShell";
import StatusIndicator from "../components/monitors/StatusIndicator";
import UptimeChart from "../components/monitors/UptimeChart";
 
const PINGS_PAGE_SIZE = 10;
 
export default function MonitorDetail() {
  const { id } = useParams<{ id: string }>();
  const navigate = useNavigate();
  const [pingsPage, setPingsPage] = useState(0);
 
  const { data: monitor, isLoading: monitorLoading } = useMonitor(id!);
  const { data: stats } = useMonitorStats(id!);
  const { data: pingsResponse, isLoading: pingsLoading } = useMonitorPings(
    id!,
    pingsPage * PINGS_PAGE_SIZE,
    PINGS_PAGE_SIZE,
  );
 
  const pings = pingsResponse?.items ?? [];
  const totalPings = pingsResponse?.total ?? 0;
  const totalPingsPages = Math.max(1, Math.ceil(totalPings / PINGS_PAGE_SIZE));
 
  if (monitorLoading) {
    return (
      <DashboardShell>
        <div className="flex justify-center py-12">
          <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600" />
        </div>
      </DashboardShell>
    );
  }
 
  if (!monitor) {
    return (
      <DashboardShell>
        <div className="text-center py-12">
          <p className="text-gray-500">Monitor not found</p>
          <button
            onClick={() => navigate("/dashboard")}
            className="mt-4 text-blue-600 hover:underline"
          >
            Back to Dashboard
          </button>
        </div>
      </DashboardShell>
    );
  }
 
  return (
    <DashboardShell>
      <div className="space-y-6">
        <div className="flex items-center justify-between">
          <div>
            <button
              onClick={() => navigate("/dashboard")}
              className="text-sm text-gray-500 hover:text-gray-700 mb-2 cursor-pointer"
            >
              ← Back
            </button>
            <h1 className="text-2xl font-bold text-gray-900">{monitor.url}</h1>
          </div>
          <StatusIndicator status={monitor.alert_status} />
        </div>
 
        {/* Stats Grid */}
        {stats && (
          <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
            <StatCard
              label="Uptime"
              value={formatPercentage(stats.uptime_percent)}
            />
            <StatCard label="Checks" value={String(stats.total_checks)} />
            <StatCard
              label="Avg Response"
              value={
                stats.avg_response_ms ? `${stats.avg_response_ms}ms` : "N/A"
              }
            />
            <StatCard
              label="24h Uptime"
              value={formatPercentage(stats.last_24h.uptime_percent)}
            />
          </div>
        )}
 
        {/* Chart */}
        <div className="rounded-lg border border-gray-200 bg-white p-6">
          <h2 className="text-lg font-semibold text-gray-900 mb-4">
            Response Time
          </h2>
          <UptimeChart monitorId={id!} />
        </div>
 
        {/* Recent Pings — Paginated */}
        <div className="rounded-lg border border-gray-200 bg-white p-6">
          <div className="flex items-center justify-between mb-4">
            <h2 className="text-lg font-semibold text-gray-900">
              Recent Checks
            </h2>
            <span className="text-sm text-gray-500">
              {totalPings} total checks
            </span>
          </div>
 
          {pingsLoading ? (
            <div className="flex justify-center py-8">
              <div className="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-600" />
            </div>
          ) : pings.length === 0 ? (
            <p className="text-sm text-gray-500 text-center py-4">
              No check data available yet.
            </p>
          ) : (
            <>
              <div className="space-y-2">
                {pings.map((ping) => (
                  <div
                    key={ping.id}
                    className="flex items-center justify-between py-2 border-b border-gray-100 last:border-0"
                  >
                    <div className="flex items-center gap-3">
                      <div
                        className={`w-2 h-2 rounded-full ${ping.is_up ? "bg-emerald-400" : "bg-red-400"}`}
                      />
                      <span className="text-sm text-gray-600">
                        {formatDateTime(ping.timestamp)}
                      </span>
                    </div>
                    <div className="text-sm text-gray-500">
                      {ping.status_code && (
                        <span className="mr-3">HTTP {ping.status_code}</span>
                      )}
                      {ping.response_ms && <span>{ping.response_ms}ms</span>}
                      {ping.error_message && (
                        <span className="text-red-500">
                          {ping.error_message}
                        </span>
                      )}
                    </div>
                  </div>
                ))}
              </div>
 
              {/* Pagination Controls */}
              <div className="flex justify-between items-center mt-4 pt-4 border-t border-gray-100">
                <button
                  onClick={() => setPingsPage((p) => Math.max(0, p - 1))}
                  disabled={pingsPage === 0}
                  className="px-3 py-1 border rounded text-sm disabled:opacity-50 enabled:hover:bg-gray-100 enabled:cursor-pointer cursor-default transition-colors"
                >
                  Previous
                </button>
                <span className="text-sm text-gray-500">
                  Page {pingsPage + 1} of {totalPingsPages}
                </span>
                <button
                  onClick={() => setPingsPage((p) => p + 1)}
                  disabled={pingsPage >= totalPingsPages - 1}
                  className="px-3 py-1 border rounded text-sm disabled:opacity-50 enabled:hover:bg-gray-100 enabled:cursor-pointer cursor-default transition-colors"
                >
                  Next
                </button>
              </div>
            </>
          )}
        </div>
      </div>
    </DashboardShell>
  );
}
 
export function StatCard({ label, value }: { label: string; value: string }) {
  return (
    <div className="rounded-lg border border-gray-200 bg-white p-4">
      <p className="text-sm text-gray-500 mb-1">{label}</p>
      <p className="text-2xl font-bold text-gray-900">{value}</p>
    </div>
  );
}