Files
Homelab-manager/web/src/pages/SharedStatus.tsx
T
bobbanandClaude Sonnet 5.5 f86661f6c9 Speedtest Tracker, Costs, "Having trouble" on status pages, grouped Integrations and Dashboard
- Speedtest Tracker integration: charts, alerts for a slow line (median of the
  last three tests under a share of what is paid for) and for failing tests.
- Costs page (Records): things paid for by hand plus Hetzner's estimate, totals
  per currency, bills rolled forward into Coming up as Renewals.
- Status pages: a "Having trouble" state for things that answer but have an open
  alert; Speedtest reads as the internet.
- Integrations page and kind picker grouped like the sidebar (one shared types
  file); Notifications "What to send" shown one group at a time; Dashboard
  integration sections follow the menu groups, with tiles for integrations that
  have no widget.
- Docs, tests, and ROLES.md menu table brought up to date.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-26 23:23:46 +01:00

146 lines
6.3 KiB
TypeScript

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" },
trouble: { label: "Having trouble", badge: "bg-yellow-lt text-yellow" },
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} /> },
trouble: { alert: "alert-warning", icon: <IconAlertTriangle 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 "trouble":
return counts.trouble === 1 ? "One thing is having trouble" : `${counts.trouble} things are having trouble`;
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>
);
}