Repository Public
1"use client";2import Link from "next/link";3import { useCallback, useEffect, useState, useSyncExternalStore } from "react";4import { useI18n, type TKey } from "@/i18n";5import { localTimeZone } from "@/lib/format";6import { RequirePermission } from "@/shell/AppShell";7import { useRealtimeStatus } from "@/shell/realtime";8import { Icon, IconButton, StateDot, cx, type Tone } from "@/ui";9import type { Widget, WidgetType } from "./layout";10import { WidgetView } from "./widgets";1112export const ROTATE_MS = 15_000;1314const PANELS: { id: string; title: TKey; widgets: WidgetType[] }[] = [15 { id: "cluster", title: "overview.ops.panel.cluster", widgets: ["nodes", "capacity"] },16 { id: "alerts", title: "overview.ops.panel.alerts", widgets: ["alerts", "incidents"] },17 { id: "workloads", title: "overview.ops.panel.workloads", widgets: ["jobs", "services", "models"] },18 { id: "operations", title: "overview.ops.panel.operations", widgets: ["maintenance", "actions", "storage"] },19];20const asWidget = (type: WidgetType): Widget => ({ id: type, type, x: 0, y: 0, w: 4, h: 3, config: {} });2122function useMedia(query: string): boolean {23 return useSyncExternalStore(24 (cb) => {25 const m = window.matchMedia(query);26 m.addEventListener("change", cb);27 return () => m.removeEventListener("change", cb);28 },29 () => window.matchMedia(query).matches,30 () => false,31 );32}33function useFullscreen(): boolean {34 return useSyncExternalStore(35 (cb) => {36 document.addEventListener("fullscreenchange", cb);37 return () => document.removeEventListener("fullscreenchange", cb);38 },39 () => document.fullscreenElement !== null,40 () => false,41 );42}43/** Wall clock, ticking every second; null until the first tick so server and client renders agree. */44function useClock(): number | null {45 const [now, setNow] = useState<number | null>(null);46 useEffect(() => {47 const first = window.setTimeout(() => setNow(Date.now()), 0);48 const id = window.setInterval(() => setNow(Date.now()), 1000);49 return () => {50 window.clearTimeout(first);51 window.clearInterval(id);52 };53 }, []);54 return now;55}5657export function OpsCentreScreen() {58 return (59 <RequirePermission permission="cluster.read">60 <OpsCentre />61 </RequirePermission>62 );63}6465function OpsCentre() {66 const { t, fmt } = useI18n();67 const reduced = useMedia("(prefers-reduced-motion: reduce)");68 const fullscreen = useFullscreen();69 const now = useClock();70 const conn = useRealtimeStatus();71 const [index, setIndex] = useState(0);72 // null = follow the default: rotate automatically unless the user asked for reduced motion73 const [choice, setChoice] = useState<boolean | null>(null);74 const playing = choice ?? !reduced;75 const count = PANELS.length;76 const go = useCallback((delta: number) => setIndex((i) => (i + delta + count) % count), [count]);7778 useEffect(() => {79 document.title = `${t("overview.ops.title")} · MacLustr Command Center`;80 }, [t]);8182 // restarts after every panel change, so a manual jump gets a full interval83 useEffect(() => {84 if (!playing) return;85 const id = window.setTimeout(() => setIndex((i) => (i + 1) % count), ROTATE_MS);86 return () => window.clearTimeout(id);87 }, [playing, index, count]);8889 useEffect(() => {90 const onKey = (e: KeyboardEvent) => {91 if (e.metaKey || e.ctrlKey || e.altKey) return;92 const target = e.target as HTMLElement | null;93 if (target && /^(INPUT|SELECT|TEXTAREA)$/.test(target.tagName)) return;94 if (e.key === "ArrowRight") { e.preventDefault(); go(1); }95 else if (e.key === "ArrowLeft") { e.preventDefault(); go(-1); }96 else if (e.key === " " && !(target && /^(BUTTON|A)$/.test(target.tagName))) { e.preventDefault(); setChoice((c) => !(c ?? !reduced)); }97 };98 window.addEventListener("keydown", onKey);99 return () => window.removeEventListener("keydown", onKey);100 }, [go, reduced]);101102 const toggleFullscreen = () => {103 if (document.fullscreenElement) void document.exitFullscreen();104 else void document.documentElement.requestFullscreen?.().catch(() => undefined);105 };106107 const panel = PANELS[index] ?? PANELS[0];108 if (!panel) return null;109 const connTone: Tone = conn.state === "live" ? "ok" : conn.state === "offline" ? "critical" : conn.state === "closed" ? "offline" : "warning";110 const connText = conn.state === "live" ? t("shell.conn.live") : conn.state === "connecting" ? t("shell.conn.connecting") : conn.state === "reconnecting" ? t("shell.conn.reconnecting") : conn.state === "offline" ? t("shell.conn.offlineSince", { time: fmt.time(conn.since) }) : t("shell.conn.closed");111112 return (113 <div className={cx("ops", reduced && "reduced")} data-testid="screen-ops" data-playing={playing} data-panel={panel.id}>114 <header className="ops-top">115 <div className="ops-brand">116 <h1>{t("overview.ops.title")}</h1>117 <span className="ops-conn" role="status" data-testid="ops-connection" data-state={conn.state}><StateDot tone={connTone} label={`${t("shell.conn.label")} : ${connText}`} /></span>118 </div>119 <div className="ops-clock" data-testid="ops-clock">120 {now !== null ? <time dateTime={new Date(now).toISOString()} className="num">{fmt.time(now)}</time> : <span className="num" aria-hidden="true">--:--:--</span>}121 <span className="ops-zone">{now !== null ? fmt.date(now) : ""} · {localTimeZone()}</span>122 </div>123 <div className="ops-tools">124 <IconButton icon={fullscreen ? "collapse" : "expand"} variant="secondary" size="lg" tip={false} label={fullscreen ? t("overview.ops.exitFullscreen") : t("overview.ops.fullscreen")} aria-pressed={fullscreen} onClick={toggleFullscreen} data-testid="ops-fullscreen" />125 <Link href="/" className="mcc-btn mcc-btn-lg" data-testid="ops-exit"><Icon name="close" size={16} />{t("overview.ops.exit")}</Link>126 </div>127 </header>128129 <section className="ops-stage" role="region" aria-roledescription={t("overview.ops.carousel")} aria-label={t("overview.ops.panels")} aria-live={playing ? "off" : "polite"} data-testid="ops-stage">130 <div className="ops-panel" key={panel.id} role="group" aria-roledescription={t("overview.ops.slide")} aria-label={t("overview.ops.position", { index: index + 1, count, title: t(panel.title) })}>131 <h2 className="ops-panel-title">{t(panel.title)}</h2>132 <div className="ops-widgets" data-count={panel.widgets.length}>133 {panel.widgets.map((type) => <WidgetView key={type} widget={asWidget(type)} />)}134 </div>135 </div>136 </section>137138 <footer className="ops-bottom">139 <div className="ops-nav" role="group" aria-label={t("overview.ops.controls")}>140 <IconButton icon="chevron-left" variant="secondary" size="lg" tip={false} label={t("overview.ops.previous")} onClick={() => go(-1)} data-testid="ops-prev" />141 <IconButton icon={playing ? "stop" : "play"} variant="secondary" size="lg" tip={false} label={playing ? t("overview.ops.pause") : t("overview.ops.play")} aria-pressed={playing} onClick={() => setChoice(!playing)} data-testid="ops-toggle" />142 <IconButton icon="chevron-right" variant="secondary" size="lg" tip={false} label={t("overview.ops.next")} onClick={() => go(1)} data-testid="ops-next" />143 </div>144 <ol className="ops-dots" aria-label={t("overview.ops.panels")}>145 {PANELS.map((p, i) => (146 <li key={p.id}>147 <button type="button" className="ops-dot" aria-current={i === index ? "true" : undefined} onClick={() => setIndex(i)} data-testid={`ops-dot-${p.id}`}>148 <span className="n num" aria-hidden="true">{i + 1}</span>149 <span>{t(p.title)}</span>150 </button>151 </li>152 ))}153 </ol>154 <p className="ops-hint" data-testid="ops-rotation">155 {playing ? t("overview.ops.rotating", { seconds: ROTATE_MS / 1000 }) : reduced && choice === null ? t("overview.ops.reducedMotion") : t("overview.ops.paused")}156 </p>157 </footer>158 </div>159 );160}161