Files
Homelab-manager/web/src/components/WallView.tsx
T
bobbanandClaude Sonnet 5.5 6e39430a1f Swedish interface: labels and short texts across every page
About 1,600 more headings, buttons, table columns, status words, placeholders and short messages are translated. Tables of words built when the app loads are now read when they are drawn, so they follow the language, and a test fails if text is translated at load time.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 20:16:39 +01:00

424 lines
19 KiB
TypeScript

import { useEffect, useState, type ReactNode } from "react";
import type { WallGroup, WallTile } from "../api/client";
import { formatDate, formatTime } from "../utils/date";
import { formatRemaining } from "../utils/duration";
import { applyDisplay, groupMembers, nightState, pageOrOverview, ROTATE_CHOICES, stepPage, WAKE_FOR_MS, wallPages, type WallNight } from "../utils/wallPages";
import { t } from "../i18n";
export const REFRESH_MS = 30_000;
const TYPE_LABELS: Record<string, string> = {
tailscale: "Tailscale",
proxmox: "Proxmox",
synology: "Synology",
semaphore: "Semaphore",
gitea: "Gitea",
dockhand: "Docker",
uptimekuma: "Uptime Kuma",
phpipam: "phpIPAM",
pbs: "Proxmox Backup",
osticket: "osTicket",
unifi: "UniFi",
npm: "Nginx Proxy Manager",
homeassistant: "Home Assistant",
ups: "UPS",
jellyfin: "Jellyfin",
authentik: "Authentik",
prometheus: "Prometheus",
immich: "Immich",
paperless: "Paperless-ngx",
n8n: "n8n",
netbox: "NetBox",
nextcloud: "Nextcloud",
hetzner: "Hetzner Cloud",
speedtest: "Speedtest Tracker",
fragnet: "Fragnet game servers",
sonarr: "Sonarr",
radarr: "Radarr",
prowlarr: "Prowlarr",
adguard: "AdGuard Home",
technitium: "Technitium DNS",
grafana: "Grafana",
lidarr: "Lidarr",
readarr: "Readarr",
qbittorrent: "qBittorrent",
sabnzbd: "SABnzbd",
hetznerrobot: "Hetzner Robot",
};
export type WallLevel = "critical" | "warning" | "ok" | "unsure";
/** Everything the board shows. A list that is null is not shown on this board (a share link that only gives numbers). */
export interface WallData {
level: WallLevel;
headline: string;
servers: { online: number; total: number } | null;
alerts: { critical: number; warning: number } | null;
maintenance: number;
integrationsFailing: number | null;
problems: { id: string; severity: "critical" | "warning"; message: string }[] | null;
windows: { id: number; name: string; endsAt: string }[] | null;
integrations: { id: number; name: string; type: string; status: "ok" | "failing" | "unknown" }[] | null;
/** What else is worth a glance, and a summary of each group of integrations; absent on a board that has none. */
tiles?: WallTile[];
groups?: WallGroup[];
environmentName: string | null;
updatedAt: Date | null;
}
/** The headline in words for how many things need a look, or why there is no answer yet. */
export function wallHeadline(level: WallLevel, count: number, state: { loaded: boolean; failed: boolean }): string {
if (level === "critical" || level === "warning") return `${count} ${count === 1 ? "thing needs" : "things need"} attention`;
if (level === "unsure") return state.loaded ? t("No alerts — but some checks couldn't run") : state.failed ? "Can't reach the server" : "Checking…";
return "All clear";
}
function useClock(): Date {
const [now, setNow] = useState(() => new Date());
useEffect(() => {
const t = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(t);
}, []);
return now;
}
/** Keeps the screen awake while the page is open, where the browser allows it — and lets go while `enabled` is false (a blank night screen, so the device may sleep). */
function useWakeLock(enabled = true) {
useEffect(() => {
if (!enabled) return;
let lock: { release: () => Promise<void> } | null = null;
let gone = false;
const request = async () => {
try {
const nav = navigator as Navigator & { wakeLock?: { request: (type: "screen") => Promise<{ release: () => Promise<void> }> } };
if (nav.wakeLock && !gone) lock = await nav.wakeLock.request("screen");
} catch {
// not allowed (battery saver, no permission): the screen may sleep like any other
}
};
void request();
const onVisible = () => document.visibilityState === "visible" && void request();
document.addEventListener("visibilitychange", onVisible);
return () => {
gone = true;
document.removeEventListener("visibilitychange", onVisible);
void lock?.release().catch(() => {});
};
}, [enabled]);
}
/**
* The board itself: a full-screen status picture in type big enough to read across a room. It only draws what it is given, so the
* signed-in wall and the one a share link opens look the same and can't drift apart.
*/
export default function WallView({
data,
idle,
footerActions,
rotation,
display,
}: {
data: WallData;
idle: boolean;
footerActions: ReactNode;
/** A named display: its name (shown in the footer) and which pages it rotates through, in order; none listed is every page. */
display?: { name: string; pages: string[]; night?: WallNight | null } | null;
/** How the pages rotate (seconds each, null for the overview alone) and how to change it; absent on a board that does not rotate. */
rotation?: { seconds: number | null; onChange: (seconds: number | null) => void };
}) {
const now = useClock();
// The night: dimmed or blank by the screen's own clock, unless something is critical (and the schedule says to show it) or somebody has just touched the screen.
const [wokenUntil, setWokenUntil] = useState<number | null>(null);
const night = nightState(now, display?.night, { critical: data.level === "critical", wokenUntil });
useWakeLock(night !== "off");
useEffect(() => {
if (!display?.night) return;
const wake = () => setWokenUntil(Date.now() + WAKE_FOR_MS);
window.addEventListener("pointerdown", wake);
window.addEventListener("keydown", wake);
return () => {
window.removeEventListener("pointerdown", wake);
window.removeEventListener("keydown", wake);
};
}, [display?.night]);
// The pages: the overview, what needs attention (while something does), and one for each group of integrations.
const rotating = !!rotation && rotation.seconds !== null;
const pages = applyDisplay(wallPages(data), display?.pages ?? []);
const pagesKey = pages.map((p) => p.id).join(",");
const [pageId, setPageId] = useState("overview");
const [paused, setPaused] = useState(false);
const page = rotating ? pageOrOverview(pages, pageId) : pages[0];
const seconds = rotation?.seconds ?? null;
useEffect(() => {
if (!rotating || paused || pages.length < 2 || seconds === null) return;
const t = setTimeout(() => setPageId(stepPage(pages, page.id, 1).id), seconds * 1000);
return () => clearTimeout(t);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [rotating, paused, seconds, page.id, pagesKey]);
// The arrow keys step through the pages by hand (and start the wait again).
useEffect(() => {
if (!rotating) return;
const onKey = (e: KeyboardEvent) => {
const el = e.target as HTMLElement | null;
if (el && (el.tagName === "SELECT" || el.tagName === "INPUT")) return;
if (e.key === "ArrowRight") setPageId(stepPage(pages, page.id, 1).id);
else if (e.key === "ArrowLeft") setPageId(stepPage(pages, page.id, -1).id);
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [rotating, page.id, pagesKey]);
const { level } = data;
const offline = data.servers ? data.servers.total - data.servers.online : 0;
const alertCount = data.alerts ? data.alerts.critical + data.alerts.warning : 0;
const showsDetail = data.problems !== null || data.windows !== null || data.integrations !== null;
return (
<div className={`wall ${idle ? "wall-idle" : ""} ${night !== "day" ? `wall-night-${night}` : ""}`} data-bs-theme="dark" aria-hidden={night === "off" ? true : undefined}>
{night === "off" && <div className="wall-night-note">{t("Night — touch the screen to wake it")}</div>}
<header className="wall-header">
<img src="/logo.webp" alt="Homelab Manager" width={901} height={460} className="wall-logo" />
{data.environmentName && <span className="badge env-badge">{data.environmentName}</span>}
<div className="wall-clock" aria-label={t("Time")}>
<div className="wall-time">{formatTime(now, false)}</div>
<div className="wall-date">
{now.toLocaleDateString([], { weekday: "long" })} {formatDate(now)}
</div>
</div>
</header>
<section className={`wall-status wall-status-${level}`} role="status" aria-live="polite">
<span className="wall-status-symbol" aria-hidden="true">
{level === "critical" ? "✕" : level === "warning" ? "▲" : level === "ok" ? "✓" : "?"}
</span>
<span>{data.headline}</span>
</section>
<section className="wall-tiles" aria-label={t("Summary")}>
<div className={`wall-tile ${offline > 0 ? "wall-tile-bad" : ""}`}>
<div className="wall-tile-figure">{data.servers ? (data.servers.total === 0 ? "—" : `${data.servers.online}/${data.servers.total}`) : "…"}</div>
<div className="wall-tile-label">servers online</div>
</div>
<div className={`wall-tile ${(data.alerts?.critical ?? 0) > 0 ? "wall-tile-bad" : (data.alerts?.warning ?? 0) > 0 ? "wall-tile-warn" : ""}`}>
<div className="wall-tile-figure">{data.alerts ? alertCount : "…"}</div>
<div className="wall-tile-label">alerts{data.alerts && alertCount > 0 ? ` (${data.alerts.critical} critical, ${data.alerts.warning} warning)` : ""}</div>
</div>
<div className={`wall-tile ${data.maintenance > 0 ? "wall-tile-info" : ""}`}>
<div className="wall-tile-figure">{data.maintenance}</div>
<div className="wall-tile-label">in maintenance</div>
</div>
<div className={`wall-tile ${(data.integrationsFailing ?? 0) > 0 ? "wall-tile-bad" : ""}`}>
<div className="wall-tile-figure">{data.integrationsFailing === null ? "…" : data.integrationsFailing}</div>
<div className="wall-tile-label">integrations failing</div>
</div>
{(data.tiles ?? []).map((t) => (
<div key={t.id} className={`wall-tile ${t.state === "bad" ? "wall-tile-bad" : t.state === "warn" ? "wall-tile-warn" : t.state === "info" ? "wall-tile-info" : ""}`}>
<div className="wall-tile-figure">{t.figure}</div>
<div className="wall-tile-label">{t.label}</div>
</div>
))}
</section>
{page.kind === "alerts" && <AlertsPage data={data} now={now} />}
{page.kind === "group" && <GroupPage data={data} groupId={page.groupId ?? ""} label={page.label} />}
{page.kind === "overview" && (data.groups ?? []).length > 0 && (
<section className="wall-groups" aria-label={t("By area")}>
{(data.groups ?? []).map((g) => {
const alerts = g.alerts.critical + g.alerts.warning;
const level = g.failing > 0 || g.alerts.critical > 0 ? "bad" : alerts > 0 ? "warn" : "ok";
const parts = [g.failing > 0 ? `${g.failing} of ${g.integrations} failing` : null, alerts > 0 ? `${alerts} ${alerts === 1 ? "alert" : "alerts"}` : null].filter(Boolean);
return (
<div key={g.id} className={`wall-group wall-group-${level}`}>
<span aria-hidden="true">{level === "bad" ? "✕" : level === "warn" ? "▲" : "✓"}</span> <strong>{g.label}</strong>{" "}
<span className="wall-dim">{parts.length > 0 ? parts.join(" · ") : `${g.integrations} connected`}</span>
</div>
);
})}
</section>
)}
{page.kind !== "overview" ? null : showsDetail ? (
<section className="wall-columns">
<div>
{data.problems && (
<>
<h2 className="wall-heading">{t("Needs attention")}</h2>
{data.problems.length === 0 ? (
<div className="wall-empty">{t("Nothing right now.")}</div>
) : (
<ul className="wall-list">
{data.problems.slice(0, 7).map((a) => (
<li key={a.id} className={a.severity === "critical" ? "wall-bad" : "wall-warn"}>
<span aria-hidden="true">{a.severity === "critical" ? "✕" : "▲"}</span> <span className="visually-hidden">{a.severity}: </span>
{a.message}
</li>
))}
{alertCount > 7 && <li className="wall-more">and {alertCount - 7} more</li>}
</ul>
)}
</>
)}
{data.windows && data.windows.length > 0 && (
<>
<h2 className="wall-heading mt-4">{t("In maintenance")}</h2>
<ul className="wall-list wall-list-small">
{data.windows.slice(0, 8).map((w) => (
<li key={w.id}>
{w.name} <span className="wall-dim">· {formatRemaining(w.endsAt, now.getTime())} left</span>
</li>
))}
{data.maintenance > 8 && <li className="wall-more">and {data.maintenance - 8} more</li>}
</ul>
</>
)}
</div>
<div>
{data.integrations && (
<>
<h2 className="wall-heading">{t("Integrations")}</h2>
{data.integrations.length === 0 ? (
<div className="wall-empty">{t("None connected.")}</div>
) : (
<ul className="wall-list wall-list-small">
{data.integrations.map((i) => (
<li key={i.id} className={i.status === "failing" ? "wall-bad" : i.status === "ok" ? "wall-good" : "wall-dim"}>
<span aria-hidden="true">{i.status === "failing" ? "✕" : i.status === "ok" ? "✓" : "·"}</span> {i.name}{" "}
<span className="wall-dim">
{TYPE_LABELS[i.type] ?? i.type} — {i.status === "failing" ? "failing" : i.status === "ok" ? "working" : "not checked yet"}
</span>
</li>
))}
</ul>
)}
</>
)}
</div>
</section>
) : (
<section className="wall-columns">
<div className="wall-empty">{t("This board shows the numbers only.")}</div>
</section>
)}
<footer className="wall-footer">
<span>
{display?.name && <strong>{display.name} · </strong>}
{data.updatedAt ? `Updated ${formatTime(data.updatedAt)}` : t("Loading…")} · refreshes every {REFRESH_MS / 1000} s
{rotating && pages.length > 1 && (
<span className="wall-pager" aria-label={`Page ${pages.findIndex((p) => p.id === page.id) + 1} of ${pages.length}: ${page.label}`}>
{" "}
· {page.label}{" "}
{pages.map((p) => (
<span key={p.id} aria-hidden="true" className={p.id === page.id ? "wall-pager-on" : ""}>
{p.id === page.id ? "●" : "○"}
</span>
))}
</span>
)}
</span>
<span className="wall-footer-actions">
{rotation && pages.length > 1 && (
<>
<label className="wall-rotate">
Rotate{" "}
<select
aria-label={t("Rotate through the pages every")}
value={rotation.seconds === null ? "off" : String(rotation.seconds)}
onChange={(e) => {
setPaused(false);
setPageId(pages[0].id);
rotation.onChange(e.target.value === "off" ? null : Number(e.target.value));
}}
>
<option value="off">off</option>
{[...new Set([...ROTATE_CHOICES, ...(rotation.seconds !== null ? [rotation.seconds] : [])])]
.sort((a, b) => a - b)
.map((s) => (
<option key={s} value={s}>
every {s} s
</option>
))}
</select>
</label>
{rotating && pages.length > 1 && (
<button type="button" className="wall-link" onClick={() => setPaused((p) => !p)} aria-pressed={paused}>
{paused ? t("Resume") : t("Pause")}
</button>
)}
</>
)}
{footerActions}
</span>
</footer>
</div>
);
}
/** What needs attention, in the whole screen's type: the open alerts, and what is in maintenance. */
function AlertsPage({ data, now }: { data: WallData; now: Date }) {
const problems = data.problems ?? [];
const alertCount = data.alerts ? data.alerts.critical + data.alerts.warning : problems.length;
const shown = problems.slice(0, 12);
return (
<section className="wall-page" aria-label={t("Needs attention")}>
<h2 className="wall-heading">{t("Needs attention")}</h2>
<ul className="wall-list wall-list-cols">
{shown.map((a) => (
<li key={a.id} className={a.severity === "critical" ? "wall-bad" : "wall-warn"}>
<span aria-hidden="true">{a.severity === "critical" ? "✕" : "▲"}</span> <span className="visually-hidden">{a.severity}: </span>
{a.message}
</li>
))}
{alertCount > shown.length && <li className="wall-more">and {alertCount - shown.length} more</li>}
</ul>
{data.windows && data.windows.length > 0 && (
<>
<h2 className="wall-heading mt-4">{t("In maintenance")}</h2>
<ul className="wall-list wall-list-small wall-list-cols">
{data.windows.slice(0, 8).map((w) => (
<li key={w.id}>
{w.name} <span className="wall-dim">· {formatRemaining(w.endsAt, now.getTime())} left</span>
</li>
))}
</ul>
</>
)}
</section>
);
}
/** One group of integrations (Infrastructure, Apps …): how it is, and each of them with its status. */
function GroupPage({ data, groupId, label }: { data: WallData; groupId: string; label: string }) {
const members = groupMembers(data.integrations ?? [], groupId);
const summary = (data.groups ?? []).find((g) => g.id === groupId);
const alerts = summary ? summary.alerts.critical + summary.alerts.warning : 0;
const failing = members.filter((m) => m.status === "failing").length;
const level = failing > 0 || (summary?.alerts.critical ?? 0) > 0 ? "bad" : alerts > 0 ? "warn" : "ok";
return (
<section className="wall-page" aria-label={label}>
<h2 className="wall-heading">
{label}{" "}
<span className={`wall-group-${level}`}>
<span aria-hidden="true">{level === "bad" ? "✕" : level === "warn" ? "▲" : "✓"}</span>{" "}
{failing > 0 ? `${failing} of ${members.length} failing` : `${members.length} connected`}
{alerts > 0 ? ` · ${alerts} ${alerts === 1 ? "alert" : "alerts"}` : ""}
</span>
</h2>
<ul className="wall-list wall-list-cols">
{members.map((i) => (
<li key={i.id} className={i.status === "failing" ? "wall-bad" : i.status === "ok" ? "wall-good" : "wall-dim"}>
<span aria-hidden="true">{i.status === "failing" ? "✕" : i.status === "ok" ? "✓" : "·"}</span> {i.name}{" "}
<span className="wall-dim">
{TYPE_LABELS[i.type] ?? i.type} — {i.status === "failing" ? "failing" : i.status === "ok" ? "working" : "not checked yet"}
</span>
</li>
))}
</ul>
</section>
);
}