Add status pages (a share link kind) and a downloadable Grafana dashboard
- Share links can open a status page: an admin picks servers, integrations and Uptime Kuma monitors and names them; the public sees only those names, Working / Not working / Being worked on / Can't tell, an optional message and the 30-day figure (share_links.kind/ title/message and status_items, migration 0033). A status address doesn't open the wall and the other way round; Uptime Kuma is asked at most every 30 seconds - Grafana dashboard built from code (download in Settings -> API and webhooks); a test checks it only uses series /metrics serves - Tests and docs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2744a84719
commit
5cc51c57de
24 files changed
+4880
-35
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { IconAlertTriangle, IconCircleCheck, IconHelpCircle, IconTool } from "@tabler/icons-react";
|
||||
import { api, type PublicStatus, type PublicStatusItem } from "../api/client";
|
||||
import { formatDateTime, setDateTimeSettings } from "../utils/date";
|
||||
|
||||
const REFRESH_MS = 30_000;
|
||||
|
||||
const STATE: Record<PublicStatusItem["state"], { label: string; badge: string }> = {
|
||||
up: { label: "Working", badge: "bg-green-lt text-green" },
|
||||
down: { label: "Not working", badge: "bg-red-lt text-red" },
|
||||
maintenance: { label: "Being worked on", badge: "bg-blue-lt text-blue" },
|
||||
unknown: { label: "Can't tell right now", badge: "bg-secondary-lt text-secondary" },
|
||||
};
|
||||
|
||||
const BANNER: Record<PublicStatus["level"], { alert: string; icon: JSX.Element }> = {
|
||||
ok: { alert: "alert-success", icon: <IconCircleCheck size={22} /> },
|
||||
issue: { alert: "alert-danger", icon: <IconAlertTriangle size={22} /> },
|
||||
maintenance: { alert: "alert-info", icon: <IconTool size={22} /> },
|
||||
unknown: { alert: "alert-secondary", icon: <IconHelpCircle size={22} /> },
|
||||
};
|
||||
|
||||
function headline(page: PublicStatus): string {
|
||||
const { counts } = page;
|
||||
if (page.items.length === 0) return "Nothing to show yet";
|
||||
switch (page.level) {
|
||||
case "issue":
|
||||
return counts.down === 1 ? "One thing isn't working" : `${counts.down} things aren't working`;
|
||||
case "maintenance":
|
||||
return counts.maintenance === 1 ? "One thing is being worked on" : `${counts.maintenance} things are being worked on`;
|
||||
case "unknown":
|
||||
return "Can't tell right now";
|
||||
default:
|
||||
return counts.unknown > 0 ? "Everything we can see is working" : "Everything is working";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A status page behind a share link: is each of the things somebody chose working? For people at home who only want to know whether the movies
|
||||
* are up. No sign-in, nothing to click. It follows the device's light or dark setting, and looks again every half minute. A link that doesn't
|
||||
* work (never made, ended, taken back) says so and stops asking.
|
||||
*/
|
||||
export default function SharedStatus() {
|
||||
const { token = "" } = useParams();
|
||||
const [page, setPage] = useState<PublicStatus | null>(null);
|
||||
const [state, setState] = useState<"loading" | "ok" | "dead" | "unreachable">("loading");
|
||||
const [updatedAt, setUpdatedAt] = useState<Date | null>(null);
|
||||
|
||||
// Light or dark as the device likes it.
|
||||
useEffect(() => {
|
||||
const media = window.matchMedia?.("(prefers-color-scheme: dark)");
|
||||
const apply = () => document.documentElement.setAttribute("data-bs-theme", media?.matches ? "dark" : "light");
|
||||
apply();
|
||||
media?.addEventListener?.("change", apply);
|
||||
return () => media?.removeEventListener?.("change", apply);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
let timer: ReturnType<typeof setInterval> | undefined;
|
||||
const load = () =>
|
||||
api.publicStatus
|
||||
.get(token)
|
||||
.then((p) => {
|
||||
if (!alive) return;
|
||||
setDateTimeSettings({ dateFormat: p.display.dateFormat as "ymd" | "dmy" | "mdy", timeFormat: p.display.timeFormat as "24h" | "12h" });
|
||||
setPage(p);
|
||||
setUpdatedAt(new Date());
|
||||
setState("ok");
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (!alive) return;
|
||||
const status = (err as { status?: number }).status;
|
||||
if (status === 404) {
|
||||
setState("dead");
|
||||
if (timer) clearInterval(timer);
|
||||
} else if (status !== 429) setState((s) => (s === "ok" ? "ok" : "unreachable"));
|
||||
});
|
||||
void load();
|
||||
timer = setInterval(load, REFRESH_MS);
|
||||
return () => {
|
||||
alive = false;
|
||||
if (timer) clearInterval(timer);
|
||||
};
|
||||
}, [token]);
|
||||
|
||||
useEffect(() => {
|
||||
document.title = page?.title ?? "Status";
|
||||
}, [page?.title]);
|
||||
|
||||
return (
|
||||
<div className="page-wrapper" style={{ minHeight: "100vh" }}>
|
||||
<div className="container-tight py-4" style={{ maxWidth: 640, margin: "0 auto", paddingLeft: 16, paddingRight: 16 }}>
|
||||
{state === "dead" ? (
|
||||
<div className="card">
|
||||
<div className="card-body text-center py-5">
|
||||
<div className="h3 mb-1">This link doesn't work any more</div>
|
||||
<div className="text-secondary">It may have ended or been taken back. Ask whoever gave it to you for a new one.</div>
|
||||
</div>
|
||||
</div>
|
||||
) : !page ? (
|
||||
<div className="text-center text-secondary py-5">{state === "unreachable" ? "Can't reach the server right now. Trying again…" : "Loading…"}</div>
|
||||
) : (
|
||||
<>
|
||||
<h1 className="h2 mb-3">{page.title}</h1>
|
||||
<div className={`alert ${BANNER[page.level].alert} mb-3`} role="status">
|
||||
<div className="d-flex gap-2 align-items-center">
|
||||
{BANNER[page.level].icon}
|
||||
<strong>{headline(page)}</strong>
|
||||
</div>
|
||||
</div>
|
||||
{page.message && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-body" style={{ whiteSpace: "pre-wrap" }}>
|
||||
{page.message}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{page.items.length > 0 && (
|
||||
<div className="card">
|
||||
<div className="list-group list-group-flush">
|
||||
{page.items.map((it) => (
|
||||
<div className="list-group-item d-flex flex-wrap align-items-center gap-2" key={it.id}>
|
||||
<span className="fw-bold flex-fill">{it.label}</span>
|
||||
{it.uptime30d !== null && <span className="text-secondary small">{it.uptime30d}% over 30 days</span>}
|
||||
<span className={`badge ${STATE[it.state].badge}`}>{STATE[it.state].label}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="text-secondary small text-center mt-3">
|
||||
{updatedAt ? `Checked ${formatDateTime(updatedAt)}` : ""} · looks again every 30 seconds
|
||||
{state === "unreachable" ? " · can't reach the server right now" : ""}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user