diff --git a/web/src/components/StatusStrip.tsx b/web/src/components/StatusStrip.tsx new file mode 100644 index 0000000..28ce510 --- /dev/null +++ b/web/src/components/StatusStrip.tsx @@ -0,0 +1,97 @@ +import { Link } from "react-router-dom"; +import { IconAlertTriangle, IconCircleCheck, IconInfoCircle } from "@tabler/icons-react"; +import { alertBadge, useAlertsReport } from "../hooks/useAlertsReport"; + +const SEVERITY_ORDER = { critical: 0, warning: 1, info: 2 } as const; + +/** One line at the top of the dashboard: is anything wrong right now? Reads the same alert report as the Alerts page. */ +export default function StatusStrip() { + const { report, failed, reload } = useAlertsReport(); + + if (!report) { + return ( +
+ +
{failed ? "Couldn't load the alert status." : "Checking status…"}
+ {failed && ( + + )} +
+ ); + } + + const badge = alertBadge(report); + const active = report.alerts + .filter((a) => !a.silenced && a.severity !== "info") + .sort((a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity]); + const silenced = report.counts.silenced; + const blind = report.couldntCheck.length; + + const notes: string[] = []; + if (silenced > 0) notes.push(`${silenced} silenced by maintenance`); + if (blind > 0 && badge) notes.push(`${blind} check${blind === 1 ? "" : "s"} couldn't run`); + + if (!badge) { + // Nothing is wrong that we can see — but if some checks couldn't run, "all clear" would claim more than is known. + const unsure = blind > 0; + return ( +
+
+ {unsure ? : } +
+ {unsure ? "No active alerts, but some checks couldn't run." : "All clear."}{" "} + {unsure ? ( + + {report.couldntCheck.map((c) => c.name).join(", ")} — see Alerts for what that leaves unchecked. + + ) : ( + No active alerts. + )} + {silenced > 0 && {silenced} silenced by maintenance.} +
+
+
+ ); + } + + const parts = [ + report.counts.critical > 0 ? `${report.counts.critical} critical` : null, + report.counts.warning > 0 ? `${report.counts.warning} warning${report.counts.warning === 1 ? "" : "s"}` : null, + ].filter(Boolean); + + return ( +
+
+ +
+
+
+ + {badge.count} {badge.count === 1 ? "thing needs" : "things need"} attention + {" "} + — {parts.join(", ")} + {notes.length > 0 && · {notes.join(" · ")}} +
+ + View all alerts + +
+ {active.length > 0 && ( +
    + {active.slice(0, 3).map((a) => ( +
  • + + {a.message} + +
  • + ))} + {active.length > 3 &&
  • and {active.length - 3} more
  • } +
+ )} +
+
+
+ ); +} diff --git a/web/src/hooks/useAlertsReport.ts b/web/src/hooks/useAlertsReport.ts new file mode 100644 index 0000000..e2fdfa4 --- /dev/null +++ b/web/src/hooks/useAlertsReport.ts @@ -0,0 +1,64 @@ +import { useSyncExternalStore } from "react"; +import { api, type AlertsReport } from "../api/client"; + +/** + * The current alert report, shared by everything on the page that shows it (the sidebar badge and the dashboard strip), so + * one request every minute serves them all. The server keeps the result for a minute as well, so this costs no extra checks. + */ +interface State { + report: AlertsReport | null; + /** The last attempt failed (the previous report, if any, is kept). */ + failed: boolean; +} + +const POLL_MS = 60_000; + +let state: State = { report: null, failed: false }; +const listeners = new Set<() => void>(); +let timer: ReturnType | null = null; +let inFlight = false; + +function set(next: State) { + state = next; + for (const l of listeners) l(); +} + +async function refresh(): Promise { + if (inFlight) return; + inFlight = true; + try { + set({ report: await api.alerts.list(), failed: false }); + } catch { + set({ report: state.report, failed: true }); + } finally { + inFlight = false; + } +} + +function subscribe(listener: () => void): () => void { + listeners.add(listener); + if (listeners.size === 1) { + void refresh(); + timer = setInterval(() => void refresh(), POLL_MS); + } + return () => { + listeners.delete(listener); + if (listeners.size === 0 && timer) { + clearInterval(timer); + timer = null; + } + }; +} + +export function useAlertsReport(): State & { reload: () => void } { + const s = useSyncExternalStore(subscribe, () => state); + return { ...s, reload: () => void refresh() }; +} + +/** What the sidebar shows next to Alerts: how many problems are active, coloured by the worst one. Silenced and informational ones don't count. */ +export function alertBadge(report: AlertsReport | null): { count: number; level: "critical" | "warning" } | null { + if (!report) return null; + const count = report.counts.critical + report.counts.warning; + if (count === 0) return null; + return { count, level: report.counts.critical > 0 ? "critical" : "warning" }; +} diff --git a/web/src/layout/AppShell.tsx b/web/src/layout/AppShell.tsx index 8b9d2c5..c452174 100644 --- a/web/src/layout/AppShell.tsx +++ b/web/src/layout/AppShell.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState, type ReactNode } from "react"; +import { useEffect, useRef, useState, type ReactNode } from "react"; import { Link, NavLink, useLocation } from "react-router-dom"; import { IconLayoutDashboard, @@ -33,12 +33,27 @@ import { IconPlug, IconExternalLink, IconAlertTriangle, + IconChevronUp, + IconLogout, } from "@tabler/icons-react"; import { api, type CurrentUser, type MaintenanceWindow } from "../api/client"; import { formatRemaining } from "../utils/duration"; import { getTheme, setTheme, type Theme } from "../utils/theme"; import CommandPalette from "../components/CommandPalette"; import { isLightColor, useEnvironment } from "../utils/environment"; +import { alertBadge, useAlertsReport } from "../hooks/useAlertsReport"; + +interface NavBadge { + text: string; + className: string; + title: string; +} + +/** Up to two initials for the little avatar in the user menu. */ +function initialsOf(name: string): string { + const words = name.replace(/@.*$/, "").split(/[\s._-]+/).filter(Boolean); + return (words.length > 1 ? words[0][0] + words[1][0] : (words[0] ?? "?").slice(0, 2)).toUpperCase(); +} type Role = "operator" | "admin"; @@ -165,6 +180,24 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr const [theme, setThemeState] = useState(() => getTheme()); const [maintenance, setMaintenance] = useState([]); const environment = useEnvironment(); + const { report } = useAlertsReport(); + const [menuOpen, setMenuOpen] = useState(false); + const menuRef = useRef(null); + + // Closes the user menu when you click elsewhere or press Escape. + useEffect(() => { + if (!menuOpen) return; + const onDown = (e: MouseEvent) => { + if (menuRef.current && !menuRef.current.contains(e.target as Node)) setMenuOpen(false); + }; + const onKey = (e: KeyboardEvent) => e.key === "Escape" && setMenuOpen(false); + document.addEventListener("mousedown", onDown); + document.addEventListener("keydown", onKey); + return () => { + document.removeEventListener("mousedown", onDown); + document.removeEventListener("keydown", onKey); + }; + }, [menuOpen]); // Everyone sees what's currently silenced, so nobody is left wondering why an alert never came. useEffect(() => { @@ -214,6 +247,32 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr }); } + // What needs a look, shown next to the page that deals with it. + const badges: Record = {}; + const alerts = alertBadge(report); + if (alerts) { + badges["/alerts"] = { + text: String(alerts.count), + className: alerts.level === "critical" ? "bg-red text-red-fg" : "bg-yellow text-yellow-fg", + title: `${alerts.count} active alert${alerts.count === 1 ? "" : "s"}`, + }; + } + if (maintenance.length > 0) { + badges["/maintenance"] = { + text: String(maintenance.length), + className: "bg-blue text-blue-fg", + title: `${maintenance.length} maintenance window${maintenance.length === 1 ? "" : "s"} open`, + }; + } + const badgeOf = (to: string) => { + const b = badges[to]; + return b ? ( + + {b.text} + + ) : null; + }; + function toggleTheme() { const next: Theme = theme === "dark" ? "light" : "dark"; setTheme(next); @@ -252,6 +311,7 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr setSidebarOpen(false)}> {entry.icon} {entry.label} + {badgeOf(entry.to)} ); @@ -268,16 +328,29 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr > {entry.icon} {entry.label} + {/* A closed group still says that something inside it needs a look. */} + {!open && + (() => { + const inside = entry.items.map((item) => badges[item.to]).filter(Boolean); + if (inside.length === 0) return null; + const worst = inside.find((b) => b.className.includes("bg-red")) ?? inside.find((b) => b.className.includes("bg-yellow")) ?? inside[0]; + return ( + b.title).join(", ")}> + {inside.reduce((n, b) => n + Number(b.text), 0)} + + ); + })()}
{entry.items.map((item) => ( `dropdown-item ${isActive ? "active" : ""}`} + className={({ isActive }) => `dropdown-item d-flex align-items-center ${isActive ? "active" : ""}`} onClick={() => setSidebarOpen(false)} > {item.label} + {badgeOf(item.to)} ))}
@@ -285,29 +358,46 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr ); })} -
+
+ {menuOpen && ( +
+ + { + setMenuOpen(false); + setSidebarOpen(false); + }} + > + + Privacy + + + + Sign out + +
+ )} - setSidebarOpen(false)} - > - - Privacy - -
- Signed in as {user.name ?? user.email ?? user.sub} · {user.role} -
- - Sign out -
diff --git a/web/src/pages/Dashboard.tsx b/web/src/pages/Dashboard.tsx index e7ce718..07cd6d2 100644 --- a/web/src/pages/Dashboard.tsx +++ b/web/src/pages/Dashboard.tsx @@ -13,6 +13,7 @@ import { type TailscaleDevicesResponse, } from "../api/client"; import { formatAgo } from "../utils/duration"; +import StatusStrip from "../components/StatusStrip"; const DNS_TYPE_COLORS: Record = { A: "#60a5fa", @@ -180,7 +181,8 @@ function MiniStat({ label, value, accent }: { label: string; value: ReactNode; a } /** The single card shape shared by every dashboard widget — DNS, Secrets, and each integration — so they all read as one family: a label + status badge, then whatever stats/breakdown that widget has. */ -function WidgetCard({ label, badge, children }: { label: string; badge?: ReactNode; children: ReactNode }) { +function WidgetCard({ label, badge, hide, children }: { label: string; badge?: ReactNode; /** Nothing is set up for this yet: it is listed under "Not set up yet" instead of taking a card. */ hide?: boolean; children: ReactNode }) { + if (hide) return null; return (
@@ -535,6 +537,18 @@ export default function Dashboard({ user }: { user: CurrentUser }) { }, {}), ).map(([type, count]) => ({ label: SECRET_TYPE_LABELS[type as SecretRecord["type"]] ?? type, count })); + // What has nothing set up yet. These get one list at the bottom instead of a grey "Not configured" card each. + const notSetUp: { label: string; to: string; what: string }[] = []; + if (dnsStats?.totalProviders === 0) notSetUp.push({ label: "DNS", to: "/dns", what: "connect a DNS provider to see zones and records" }); + if (secrets !== null && totalSecrets === 0) notSetUp.push({ label: "Secrets", to: "/secrets", what: "track when a token, certificate or password expires" }); + if (serverSummary?.total === 0) notSetUp.push({ label: "Servers", to: "/servers", what: "add a server and install its agent" }); + if (domainList !== null && domains.length === 0) notSetUp.push({ label: "Domains", to: "/domains", what: "appear after a DNS zone is added and the daily check has run" }); + if (integrations !== null) { + for (const { type, label } of WIDGETS) { + if (!integrations.some((i) => i.type === type)) notSetUp.push({ label, to: "/integrations", what: "connect it under Integrations" }); + } + } + return ( <>
@@ -543,6 +557,7 @@ export default function Dashboard({ user }: { user: CurrentUser }) {

Welcome back, {user.name ?? user.email ?? user.sub}.

+
@@ -550,6 +565,7 @@ export default function Dashboard({ user }: { user: CurrentUser }) {
Not configured @@ -597,6 +613,7 @@ export default function Dashboard({ user }: { user: CurrentUser }) { Not configured @@ -635,6 +652,7 @@ export default function Dashboard({ user }: { user: CurrentUser }) { Not configured @@ -729,6 +747,7 @@ export default function Dashboard({ user }: { user: CurrentUser }) { Not configured @@ -847,6 +866,7 @@ export default function Dashboard({ user }: { user: CurrentUser }) { i.type === type)} badge={ isLive ? ( Connected @@ -1072,6 +1092,22 @@ export default function Dashboard({ user }: { user: CurrentUser }) { ); })}
+ + {notSetUp.length > 0 && ( +
+
+

Not set up yet

+
+
+ {notSetUp.map((n) => ( + + {n.label} + {n.what} + + ))} +
+
+ )} ); } diff --git a/web/src/styles.css b/web/src/styles.css index cebad70..60e8d43 100644 --- a/web/src/styles.css +++ b/web/src/styles.css @@ -85,6 +85,66 @@ html[data-env] .navbar-vertical { border-top: 4px solid var(--hlm-env-color); } +/* The user menu at the foot of the sidebar: who is signed in, with theme, privacy and sign-out behind one button. */ +.user-menu { + position: relative; +} + +.user-menu-toggle { + display: flex; + align-items: center; + gap: 0.6rem; + width: 100%; + padding: 0.4rem 0.5rem; + color: inherit; + text-align: left; + background: color-mix(in srgb, currentColor 7%, transparent); + border: 1px solid color-mix(in srgb, currentColor 20%, transparent); + border-radius: var(--tblr-border-radius, 4px); +} + +.user-menu-toggle:hover, +.user-menu-toggle[aria-expanded="true"] { + background: color-mix(in srgb, currentColor 14%, transparent); +} + +.user-menu-who { + flex: 1; + min-width: 0; + line-height: 1.2; +} + +.user-menu-chevron-closed { + transform: rotate(180deg); +} + +.user-menu-list { + margin-bottom: 0.4rem; + padding: 0.25rem; + background: color-mix(in srgb, currentColor 9%, transparent); + border: 1px solid color-mix(in srgb, currentColor 20%, transparent); + border-radius: var(--tblr-border-radius, 4px); +} + +.user-menu-item { + display: flex; + align-items: center; + gap: 0.6rem; + width: 100%; + padding: 0.4rem 0.5rem; + color: inherit; + text-align: left; + text-decoration: none; + background: none; + border: 0; + border-radius: var(--tblr-border-radius, 4px); +} + +.user-menu-item:hover { + color: inherit; + background: color-mix(in srgb, currentColor 14%, transparent); +} + /* Sidebar submenus: a little tighter than Tabler's dropdown rows, so several open groups still fit on one screen. */ .navbar-vertical .dropdown-menu .dropdown-item { padding-top: 0.3rem;