Files
Homelab-manager/web/src/components/WallView.tsx
T
bobbanandClaude Sonnet 5.5 44e393f172 Add saved filter views, a customisable dashboard, server comparison, row keyboard navigation and share links
- Table search, status, tag and sort live in the address; Save view pins one as a favourite.
- Dashboard cards can be moved and hidden per user (users.dashboard_layout, migration 0022).
- Compare two to four servers' load, memory or disk on one chart.
- j/k move through table rows that open a detail panel.
- Share links open a read-only wall without signing in, numbers only or with names
  (share_links, migration 0023); the wall is split into a shared view over two data sources.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 22:15:29 +02:00

200 lines
8.8 KiB
TypeScript

import { useEffect, useState, type ReactNode } from "react";
import { formatDate, formatTime } from "../utils/date";
import { formatRemaining } from "../utils/duration";
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",
};
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;
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 ? "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. */
function useWakeLock() {
useEffect(() => {
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(() => {});
};
}, []);
}
/**
* 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 }: { data: WallData; idle: boolean; footerActions: ReactNode }) {
const now = useClock();
useWakeLock();
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" : ""}`} data-bs-theme="dark">
<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="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="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>
</section>
{showsDetail ? (
<section className="wall-columns">
<div>
{data.problems && (
<>
<h2 className="wall-heading">Needs attention</h2>
{data.problems.length === 0 ? (
<div className="wall-empty">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">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">Integrations</h2>
{data.integrations.length === 0 ? (
<div className="wall-empty">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">This board shows the numbers only.</div>
</section>
)}
<footer className="wall-footer">
<span>
{data.updatedAt ? `Updated ${formatTime(data.updatedAt)}` : "Loading…"} · refreshes every {REFRESH_MS / 1000} s
</span>
<span className="wall-footer-actions">{footerActions}</span>
</footer>
</div>
);
}