Files
Homelab-manager/web/src/pages/UptimeKuma.tsx
T
bobbanandClaude Sonnet 5.5 846af02851 Lifecycle planning, and consistent empty and loading states
- Inventory: end of support and replacement cost per asset; a Lifecycle page showing what ages out year by year and what replacing it will cost; support ending within 90 days in the weekly digest
- Lists: a shared empty state saying what is missing, why, and the one thing to do; "nothing matches" with a clear button; skeleton rows instead of empty-looking lists while loading

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-18 23:18:42 +02:00

230 lines
10 KiB
TypeScript

import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { api, type CurrentUser, type IntegrationSummary, type UptimeKumaMonitor, type UptimeKumaMonitorsResponse } from "../api/client";
import { useSortable } from "../hooks/useSortable";
import SortableTh from "../components/SortableTh";
import { usePagination } from "../hooks/usePagination";
import Pagination from "../components/Pagination";
import { downloadCsv } from "../utils/csv";
import { readableError } from "../utils/errors";
import IntegrationPageTitle from "../components/IntegrationPageTitle";
import { ErrorNotice } from "../components/Notice";
import { SearchBox } from "../components/TableToolbar";
import { useTableSearch } from "../hooks/useTableSearch";
import DetailDrawer, { ObjectDetails } from "../components/DetailDrawer";
import { useRowDetail } from "../hooks/useRowDetail";
import { EmptyRow } from "../components/EmptyState";
function statusBadge(monitor: UptimeKumaMonitor) {
switch (monitor.status) {
case "up":
return <span className="badge bg-green-lt text-green">Up</span>;
case "down":
return <span className="badge bg-red-lt text-red">Down</span>;
case "pending":
return <span className="badge bg-yellow-lt text-yellow">Pending</span>;
case "maintenance":
return <span className="badge bg-blue-lt text-blue">Maintenance</span>;
default:
return <span className="badge bg-secondary-lt text-secondary">Unknown</span>;
}
}
function formatPercent(v: number | null): string {
return v === null ? "—" : `${v}%`;
}
function formatMs(v: number | null): string {
if (v === null || v < 0) return "—";
return `${Math.round(v)} ms`;
}
export default function UptimeKuma({ user: _user }: { user: CurrentUser }) {
const [integrations, setIntegrations] = useState<IntegrationSummary[] | null>(null);
const [selectedId, setSelectedId] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
const [data, setData] = useState<UptimeKumaMonitorsResponse | null>(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
api.integrations
.list()
.then((res) => {
const kumaIntegrations = res.integrations.filter((i) => i.type === "uptimekuma");
setIntegrations(kumaIntegrations);
if (!selectedId && kumaIntegrations.length > 0) setSelectedId(kumaIntegrations[0].id);
})
.catch((err) => setError(readableError(err)));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const selected = integrations?.find((i) => i.id === selectedId) ?? null;
function loadMonitors(id: number) {
setLoading(true);
setError(null);
api.integrations.uptimekuma
.monitors(id)
.then(setData)
.catch((err) => setError(readableError(err)))
.finally(() => setLoading(false));
}
useEffect(() => {
if (selected?.enabled) {
loadMonitors(selected.id);
} else {
setData(null);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedId]);
const tableSearch = useTableSearch(data?.monitors);
const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, "name", "asc", { url: true });
const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted);
const detail = useRowDetail<NonNullable<typeof pageItems>[number]>();
function exportCsv() {
if (!sorted) return;
downloadCsv(
"uptime-kuma-monitors.csv",
["Monitor", "Type", "Target", "Status", "Response (ms)", "Uptime 24h", "Uptime 30d", "Cert days left", "Server"],
sorted.map((m) => [
m.name,
m.type,
m.target ? `${m.target}${m.port ? `:${m.port}` : ""}` : "",
m.status,
m.responseTimeMs !== null && m.responseTimeMs >= 0 ? Math.round(m.responseTimeMs) : "",
m.uptime24h ?? "",
m.uptime30d ?? "",
m.certDaysRemaining ?? "",
m.matchedServer?.name ?? "",
]),
);
}
return (
<>
<IntegrationPageTitle title="Uptime Kuma" integration={selected} />
<ErrorNotice error={error} />
{integrations?.length === 0 ? (
<div className="card">
<div className="card-body text-secondary">
No Uptime Kuma integration configured yet. <Link to="/integrations">Add one under Integrations</Link>.
</div>
</div>
) : (
<>
{integrations && integrations.length > 1 && (
<div className="mb-3" style={{ maxWidth: 320 }}>
<select className="form-select" value={selectedId ?? ""} onChange={(e) => setSelectedId(Number(e.target.value))}>
{integrations.map((i) => (
<option key={i.id} value={i.id} disabled={!i.enabled}>
{i.name}
{!i.enabled ? " — disabled" : ""}
</option>
))}
</select>
</div>
)}
{selected && !selected.enabled ? (
<div className="card">
<div className="card-body text-secondary">
"{selected.name}" is disabled. Enable it under Integrations → Manage integrations to see its monitors.
</div>
</div>
) : (
<div className="card">
<div className="card-header">
<h3 className="card-title">
Monitors
{data && data.summary.down > 0 && <span className="text-red fw-normal ms-2">{data.summary.down} down</span>}
{data && data.summary.down === 0 && data.summary.pending > 0 && (
<span className="text-yellow fw-normal ms-2">{data.summary.pending} pending</span>
)}
</h3>
<SearchBox value={tableSearch.query} onChange={tableSearch.setQuery} className="ms-3" />
<div className="card-actions">
<button className="btn btn-sm btn-outline-secondary" onClick={exportCsv} disabled={!sorted || sorted.length === 0}>
Export CSV
</button>
<button className="btn btn-sm btn-outline-secondary" onClick={() => selected && loadMonitors(selected.id)} disabled={loading}>
{loading ? "Refreshing…" : "Refresh"}
</button>
</div>
</div>
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<SortableTh<UptimeKumaMonitor> label="Monitor" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<UptimeKumaMonitor> label="Target" sortKeyName="target" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<th>Server</th>
<SortableTh<UptimeKumaMonitor> label="Status" sortKeyName="status" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<UptimeKumaMonitor>
label="Response"
sortKeyName="responseTimeMs"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<UptimeKumaMonitor> label="Uptime 24h" sortKeyName="uptime24h" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<UptimeKumaMonitor> label="Uptime 30d" sortKeyName="uptime30d" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
</tr>
</thead>
<tbody>
{pageItems?.map((m) => (
<tr key={m.id} {...detail.rowProps(m)}>
<td>
{m.name}
<div className="text-secondary small">{m.type}</div>
</td>
<td className="text-secondary">
{m.target ? `${m.target}${m.port ? `:${m.port}` : ""}` : "—"}
{m.certDaysRemaining !== null && (
<div className={`small ${m.certDaysRemaining <= 14 ? "text-red" : "text-secondary"}`}>
cert: {m.certDaysRemaining}d left
</div>
)}
</td>
<td>
{m.matchedServer ? (
<Link to={`/servers/${m.matchedServer.id}`}>{m.matchedServer.name}</Link>
) : (
<span className="text-secondary">—</span>
)}
</td>
<td>{statusBadge(m)}</td>
<td className="text-secondary">{formatMs(m.responseTimeMs)}</td>
<td className="text-secondary">{formatPercent(m.uptime24h)}</td>
<td className="text-secondary">{formatPercent(m.uptime30d)}</td>
</tr>
))}
{sorted?.length === 0 && (
<EmptyRow colSpan={7} title="No monitors" hint="Add monitors in Uptime Kuma and they appear here, with the server each one watches." filtered={tableSearch.active ? () => tableSearch.setQuery("") : undefined} />
)}
</tbody>
</table>
</div>
<Pagination page={page} pageCount={pageCount} totalCount={totalCount} onPageChange={setPage} />
<div className="card-footer text-secondary small">
"Server" links a monitor to one of your servers when its target (an IP, or a TCP/HTTP hostname) matches a
server's own address or hostname — monitors of other kinds (groups, pushes, keyword/DNS checks without a
plain host) are left unmatched rather than guessed at.
</div>
</div>
)}
</>
)}
{detail.selected && (
<DetailDrawer title={((m) => m.name)(detail.selected)} subtitle={((m) => m.type)(detail.selected)} onClose={detail.close}>
<ObjectDetails data={detail.selected} />
</DetailDrawer>
)}
</>
);
}