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 = { 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 = { ok: { alert: "alert-success", icon: }, trouble: { alert: "alert-warning", icon: }, issue: { alert: "alert-danger", icon: }, maintenance: { alert: "alert-info", icon: }, unknown: { alert: "alert-secondary", icon: }, }; 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(null); const [state, setState] = useState<"loading" | "ok" | "dead" | "unreachable">("loading"); const [updatedAt, setUpdatedAt] = useState(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 | 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 (
{state === "dead" ? (
This link doesn't work any more
It may have ended or been taken back. Ask whoever gave it to you for a new one.
) : !page ? (
{state === "unreachable" ? "Can't reach the server right now. Trying again…" : "Loading…"}
) : ( <>

{page.title}

{BANNER[page.level].icon} {headline(page)}
{page.message && (
{page.message}
)} {page.items.length > 0 && (
{page.items.map((it) => (
{it.label} {it.uptime30d !== null && {it.uptime30d}% over 30 days} {STATE[it.state].label}
))}
)}
{updatedAt ? `Checked ${formatDateTime(updatedAt)}` : ""} · looks again every 30 seconds {state === "unreachable" ? " · can't reach the server right now" : ""}
)}
); }