| 4a006da | | | 1 | "use client"; |
| 4a006da | | | 2 | |
| 27902ea | | | 3 | import { useEffect, useState, useCallback } from "react"; |
| 4a006da | | | 4 | import { useRouter } from "next/navigation"; |
| 4a006da | | | 5 | import { useAuth } from "@/lib/auth"; |
| bf5fc33 | | | 6 | import { Skeleton } from "@/app/components/skeleton"; |
| 27902ea | | | 7 | |
| 27902ea | | | 8 | /* ── Types ── */ |
| 27902ea | | | 9 | |
| 27902ea | | | 10 | interface ServiceCheck { |
| 27902ea | | | 11 | name: string; |
| 27902ea | | | 12 | status: "operational" | "degraded" | "down"; |
| 27902ea | | | 13 | latency: number | null; |
| 27902ea | | | 14 | detail?: string; |
| 27902ea | | | 15 | } |
| 27902ea | | | 16 | |
| 27902ea | | | 17 | interface ContainerStats { |
| 27902ea | | | 18 | name: string; |
| 27902ea | | | 19 | status: string; |
| 27902ea | | | 20 | cpu_percent: number; |
| 27902ea | | | 21 | mem_usage_mb: number; |
| 27902ea | | | 22 | mem_limit_mb: number; |
| 27902ea | | | 23 | mem_percent: number; |
| 27902ea | | | 24 | net_rx_mb: number; |
| 27902ea | | | 25 | net_tx_mb: number; |
| 27902ea | | | 26 | uptime: string; |
| 27902ea | | | 27 | } |
| 27902ea | | | 28 | |
| 27902ea | | | 29 | interface SystemMetrics { |
| 27902ea | | | 30 | cpu_percent: number; |
| 27902ea | | | 31 | mem_total_mb: number; |
| 27902ea | | | 32 | mem_used_mb: number; |
| 27902ea | | | 33 | mem_percent: number; |
| 27902ea | | | 34 | disk_total_gb: number; |
| 27902ea | | | 35 | disk_used_gb: number; |
| 27902ea | | | 36 | disk_percent: number; |
| 27902ea | | | 37 | load_avg: number[]; |
| 27902ea | | | 38 | uptime: string; |
| 27902ea | | | 39 | } |
| 27902ea | | | 40 | |
| 27902ea | | | 41 | interface StatusResponse { |
| 27902ea | | | 42 | status: string; |
| 27902ea | | | 43 | services: ServiceCheck[]; |
| 27902ea | | | 44 | containers: ContainerStats[]; |
| 27902ea | | | 45 | system: SystemMetrics | null; |
| 27902ea | | | 46 | checked_at: string; |
| 27902ea | | | 47 | } |
| 27902ea | | | 48 | |
| 27902ea | | | 49 | /* ── Page ── */ |
| 4a006da | | | 50 | |
| 4a006da | | | 51 | export default function DashboardPage() { |
| 27902ea | | | 52 | const { user, loading: authLoading } = useAuth(); |
| 4a006da | | | 53 | const router = useRouter(); |
| 4a006da | | | 54 | |
| 27902ea | | | 55 | const [data, setData] = useState<StatusResponse | null>(null); |
| 27902ea | | | 56 | const [loaded, setLoaded] = useState(false); |
| 79efd41 | | | 57 | |
| 1da9874 | | | 58 | useEffect(() => { |
| 1da9874 | | | 59 | document.title = "Dashboard"; |
| 1da9874 | | | 60 | }, []); |
| 1da9874 | | | 61 | |
| 4a006da | | | 62 | useEffect(() => { |
| 4a006da | | | 63 | if (!authLoading && !user) { |
| 6dd74de | | | 64 | router.push(`/login?redirect=${encodeURIComponent(window.location.pathname)}`); |
| 4a006da | | | 65 | } |
| 4a006da | | | 66 | }, [authLoading, user, router]); |
| 4a006da | | | 67 | |
| 27902ea | | | 68 | const refresh = useCallback(async () => { |
| 27902ea | | | 69 | if (!user) return; |
| 27902ea | | | 70 | try { |
| 27902ea | | | 71 | const res = await fetch("/api/status", { cache: "no-store" }); |
| 27902ea | | | 72 | const json: StatusResponse = await res.json(); |
| 27902ea | | | 73 | setData(json); |
| 27902ea | | | 74 | } catch {} |
| 27902ea | | | 75 | setLoaded(true); |
| 27902ea | | | 76 | }, [user]); |
| 27902ea | | | 77 | |
| 4a006da | | | 78 | useEffect(() => { |
| 4a006da | | | 79 | if (!user) return; |
| 27902ea | | | 80 | refresh(); |
| 27902ea | | | 81 | const iv = setInterval(refresh, 30000); |
| 27902ea | | | 82 | return () => clearInterval(iv); |
| 27902ea | | | 83 | }, [user, refresh]); |
| 0fdef14 | | | 84 | |
| 27902ea | | | 85 | if (authLoading || !user) return null; |
| 0fdef14 | | | 86 | |
| 27902ea | | | 87 | const sys = data?.system; |
| 27902ea | | | 88 | const containers = data?.containers ?? []; |
| 27902ea | | | 89 | const services = data?.services ?? []; |
| 0fdef14 | | | 90 | |
| 27902ea | | | 91 | return ( |
| 27902ea | | | 92 | <div style={{ maxWidth: 1100, margin: "0 auto", padding: "1.25rem 1rem 2rem" }}> |
| 27902ea | | | 93 | {/* ── Header ── */} |
| 27902ea | | | 94 | <div className="flex items-center justify-between" style={{ marginBottom: "1rem" }}> |
| 27902ea | | | 95 | <span className="text-sm font-medium" style={{ color: "var(--text-primary)" }}>Dashboard</span> |
| 27902ea | | | 96 | <div className="flex items-center gap-3"> |
| 27902ea | | | 97 | {data?.checked_at && ( |
| 27902ea | | | 98 | <span className="text-xs" style={{ color: "var(--text-faint)" }}> |
| 27902ea | | | 99 | {new Date(data.checked_at).toLocaleTimeString()} |
| 27902ea | | | 100 | </span> |
| 27902ea | | | 101 | )} |
| 27902ea | | | 102 | <button onClick={refresh} className="btn-reset text-xs" style={{ color: "var(--text-muted)" }}> |
| 27902ea | | | 103 | Refresh |
| 27902ea | | | 104 | </button> |
| 27902ea | | | 105 | </div> |
| 27902ea | | | 106 | </div> |
| 4a006da | | | 107 | |
| 27902ea | | | 108 | {/* ── System gauges ── */} |
| 27902ea | | | 109 | <div |
| 27902ea | | | 110 | style={{ |
| 27902ea | | | 111 | display: "grid", |
| 27902ea | | | 112 | gridTemplateColumns: "repeat(4, 1fr)", |
| 27902ea | | | 113 | gap: "0.625rem", |
| 27902ea | | | 114 | marginBottom: "0.875rem", |
| 27902ea | | | 115 | }} |
| 27902ea | | | 116 | > |
| 27902ea | | | 117 | <GaugeCard |
| 27902ea | | | 118 | label="CPU" |
| 27902ea | | | 119 | value={sys?.cpu_percent ?? null} |
| 27902ea | | | 120 | detail={sys?.load_avg ? `Load ${sys.load_avg.map((l) => l.toFixed(1)).join(" ")}` : undefined} |
| 27902ea | | | 121 | loaded={loaded} |
| 27902ea | | | 122 | /> |
| 27902ea | | | 123 | <GaugeCard |
| 27902ea | | | 124 | label="Memory" |
| 27902ea | | | 125 | value={sys?.mem_percent ?? null} |
| 27902ea | | | 126 | detail={sys ? `${sys.mem_used_mb} / ${sys.mem_total_mb} MB` : undefined} |
| 27902ea | | | 127 | loaded={loaded} |
| 27902ea | | | 128 | /> |
| 27902ea | | | 129 | <GaugeCard |
| 27902ea | | | 130 | label="Disk" |
| 27902ea | | | 131 | value={sys?.disk_percent ?? null} |
| 27902ea | | | 132 | detail={sys ? `${sys.disk_used_gb} / ${sys.disk_total_gb} GB` : undefined} |
| 27902ea | | | 133 | loaded={loaded} |
| 27902ea | | | 134 | /> |
| 27902ea | | | 135 | <GaugeCard |
| 27902ea | | | 136 | label="Uptime" |
| 27902ea | | | 137 | value={null} |
| 27902ea | | | 138 | displayValue={sys?.uptime || null} |
| 27902ea | | | 139 | loaded={loaded} |
| 27902ea | | | 140 | /> |
| 27902ea | | | 141 | </div> |
| 0b1c50f | | | 142 | |
| 27902ea | | | 143 | {/* ── Services + Containers ── */} |
| 27902ea | | | 144 | <div |
| 27902ea | | | 145 | style={{ |
| 27902ea | | | 146 | display: "grid", |
| 27902ea | | | 147 | gridTemplateColumns: "1fr 1fr", |
| 27902ea | | | 148 | gap: "0.625rem", |
| 27902ea | | | 149 | }} |
| 27902ea | | | 150 | > |
| 27902ea | | | 151 | <Panel title="Services" count={services.length}> |
| 27902ea | | | 152 | {!loaded ? ( |
| 27902ea | | | 153 | <SkeletonRows n={6} /> |
| 27902ea | | | 154 | ) : services.length === 0 ? ( |
| 27902ea | | | 155 | <EmptyRow>No service data</EmptyRow> |
| 27902ea | | | 156 | ) : ( |
| 27902ea | | | 157 | <div className="flex flex-col"> |
| 27902ea | | | 158 | {services.map((svc) => ( |
| 27902ea | | | 159 | <div |
| 27902ea | | | 160 | key={svc.name} |
| 27902ea | | | 161 | className="flex items-center justify-between py-1.5 px-2.5" |
| 27902ea | | | 162 | style={{ borderBottom: "1px solid var(--divide)" }} |
| 27902ea | | | 163 | > |
| 27902ea | | | 164 | <div className="flex items-center gap-2"> |
| 27902ea | | | 165 | <StatusDot status={svc.status} /> |
| 27902ea | | | 166 | <span className="text-sm" style={{ color: "var(--text-primary)" }}>{svc.name}</span> |
| 27902ea | | | 167 | </div> |
| 27902ea | | | 168 | <span className="text-xs font-mono" style={{ color: "var(--text-faint)" }}> |
| 27902ea | | | 169 | {svc.latency !== null ? `${svc.latency}ms` : svc.detail ?? "—"} |
| 27902ea | | | 170 | </span> |
| 27902ea | | | 171 | </div> |
| 27902ea | | | 172 | ))} |
| 27902ea | | | 173 | </div> |
| 27902ea | | | 174 | )} |
| 27902ea | | | 175 | </Panel> |
| 4dfd09b | | | 176 | |
| 27902ea | | | 177 | <Panel title="Containers" count={containers.length}> |
| 27902ea | | | 178 | {!loaded ? ( |
| 27902ea | | | 179 | <SkeletonRows n={6} /> |
| 27902ea | | | 180 | ) : containers.length === 0 ? ( |
| 27902ea | | | 181 | <EmptyRow>No container data</EmptyRow> |
| 27902ea | | | 182 | ) : ( |
| 27902ea | | | 183 | <div className="flex flex-col"> |
| 27902ea | | | 184 | {containers.map((c) => ( |
| 27902ea | | | 185 | <div |
| 27902ea | | | 186 | key={c.name} |
| 27902ea | | | 187 | className="flex items-center justify-between py-1.5 px-2.5" |
| 27902ea | | | 188 | style={{ borderBottom: "1px solid var(--divide)" }} |
| 27902ea | | | 189 | > |
| 27902ea | | | 190 | <div className="flex items-center gap-2 min-w-0"> |
| 27902ea | | | 191 | <StatusDot status={c.status === "running" ? "operational" : "down"} /> |
| 27902ea | | | 192 | <span className="text-sm truncate" style={{ color: "var(--text-primary)" }}>{c.name}</span> |
| 27902ea | | | 193 | <span className="text-xs shrink-0" style={{ color: "var(--text-faint)" }}>{c.uptime}</span> |
| 27902ea | | | 194 | </div> |
| 27902ea | | | 195 | <div className="flex items-center gap-3 shrink-0"> |
| 27902ea | | | 196 | <MiniBar value={c.cpu_percent} max={100} label={`${c.cpu_percent.toFixed(1)}%`} color="var(--accent)" /> |
| 27902ea | | | 197 | <MiniBar value={c.mem_usage_mb} max={c.mem_limit_mb || 1} label={`${c.mem_usage_mb}M`} color="var(--status-merged-text)" /> |
| 27902ea | | | 198 | </div> |
| 27902ea | | | 199 | </div> |
| 27902ea | | | 200 | ))} |
| 27902ea | | | 201 | </div> |
| 27902ea | | | 202 | )} |
| 27902ea | | | 203 | </Panel> |
| 27902ea | | | 204 | </div> |
| 27902ea | | | 205 | </div> |
| 27902ea | | | 206 | ); |
| 27902ea | | | 207 | } |
| 27902ea | | | 208 | |
| 27902ea | | | 209 | /* ── Gauge card ── */ |
| 4a006da | | | 210 | |
| 27902ea | | | 211 | function GaugeCard({ |
| 27902ea | | | 212 | label, |
| 27902ea | | | 213 | value, |
| 27902ea | | | 214 | displayValue, |
| 27902ea | | | 215 | detail, |
| 27902ea | | | 216 | loaded, |
| 27902ea | | | 217 | }: { |
| 27902ea | | | 218 | label: string; |
| 27902ea | | | 219 | value: number | null; |
| 27902ea | | | 220 | displayValue?: string | null; |
| 27902ea | | | 221 | detail?: string; |
| 27902ea | | | 222 | loaded: boolean; |
| 27902ea | | | 223 | }) { |
| 27902ea | | | 224 | const pct = value !== null ? Math.min(100, Math.max(0, value)) : null; |
| 27902ea | | | 225 | const color = |
| 27902ea | | | 226 | pct === null |
| 27902ea | | | 227 | ? "var(--text-faint)" |
| 27902ea | | | 228 | : pct > 85 |
| 27902ea | | | 229 | ? "var(--status-closed-text)" |
| 27902ea | | | 230 | : pct > 60 |
| 27902ea | | | 231 | ? "var(--status-merged-text)" |
| 27902ea | | | 232 | : "var(--status-open-text)"; |
| 4a006da | | | 233 | |
| 4a006da | | | 234 | return ( |
| 27902ea | | | 235 | <div |
| 27902ea | | | 236 | style={{ |
| 27902ea | | | 237 | backgroundColor: "var(--bg-card)", |
| 27902ea | | | 238 | border: "1px solid var(--border-subtle)", |
| 27902ea | | | 239 | padding: "0.75rem 0.875rem", |
| 27902ea | | | 240 | position: "relative", |
| 27902ea | | | 241 | overflow: "hidden", |
| 27902ea | | | 242 | }} |
| 27902ea | | | 243 | > |
| 27902ea | | | 244 | {pct !== null && ( |
| 4dfd09b | | | 245 | <div |
| 4dfd09b | | | 246 | style={{ |
| 27902ea | | | 247 | position: "absolute", |
| 27902ea | | | 248 | left: 0, |
| 27902ea | | | 249 | bottom: 0, |
| 27902ea | | | 250 | width: `${pct}%`, |
| 27902ea | | | 251 | height: 3, |
| 27902ea | | | 252 | backgroundColor: color, |
| 27902ea | | | 253 | transition: "width 0.6s ease, background-color 0.3s", |
| 4dfd09b | | | 254 | }} |
| 27902ea | | | 255 | /> |
| 27902ea | | | 256 | )} |
| 27902ea | | | 257 | <div className="text-xs font-mono" style={{ color: "var(--text-faint)", marginBottom: 3 }}> |
| 27902ea | | | 258 | {label} |
| 27902ea | | | 259 | </div> |
| 27902ea | | | 260 | {!loaded ? ( |
| 27902ea | | | 261 | <Skeleton width="3rem" height="1.25rem" /> |
| 27902ea | | | 262 | ) : ( |
| 27902ea | | | 263 | <> |
| 27902ea | | | 264 | <div |
| 27902ea | | | 265 | className="font-mono" |
| 27902ea | | | 266 | style={{ fontSize: "1.375rem", fontWeight: 500, color: displayValue ? "var(--text-primary)" : color, lineHeight: 1.1 }} |
| 27902ea | | | 267 | > |
| 27902ea | | | 268 | {displayValue ?? (pct !== null ? `${pct}%` : "—")} |
| bdf6540 | | | 269 | </div> |
| 27902ea | | | 270 | {detail && ( |
| 27902ea | | | 271 | <div className="text-xs" style={{ color: "var(--text-faint)", marginTop: 2 }}>{detail}</div> |
| bdf6540 | | | 272 | )} |
| 27902ea | | | 273 | </> |
| 4a006da | | | 274 | )} |
| 27902ea | | | 275 | </div> |
| 27902ea | | | 276 | ); |
| 27902ea | | | 277 | } |
| 4a006da | | | 278 | |
| 27902ea | | | 279 | /* ── Mini bar ── */ |
| 4dfd09b | | | 280 | |
| 27902ea | | | 281 | function MiniBar({ value, max, label, color }: { value: number; max: number; label: string; color: string }) { |
| 27902ea | | | 282 | const pct = max > 0 ? Math.min(100, (value / max) * 100) : 0; |
| 27902ea | | | 283 | return ( |
| 27902ea | | | 284 | <div className="flex items-center gap-1.5" style={{ minWidth: 70 }}> |
| 27902ea | | | 285 | <div style={{ width: 36, height: 6, backgroundColor: "var(--bg-inset)", borderRadius: 3, overflow: "hidden" }}> |
| 27902ea | | | 286 | <div style={{ width: `${pct}%`, height: "100%", backgroundColor: color, borderRadius: 3, transition: "width 0.4s ease" }} /> |
| 4a006da | | | 287 | </div> |
| 27902ea | | | 288 | <span className="text-xs font-mono" style={{ color: "var(--text-faint)" }}>{label}</span> |
| 27902ea | | | 289 | </div> |
| 27902ea | | | 290 | ); |
| 27902ea | | | 291 | } |
| 4a006da | | | 292 | |
| 27902ea | | | 293 | /* ── Panel ── */ |
| 79efd41 | | | 294 | |
| 27902ea | | | 295 | function Panel({ title, count, children }: { title: string; count?: number; children: React.ReactNode }) { |
| 27902ea | | | 296 | return ( |
| 27902ea | | | 297 | <div |
| 27902ea | | | 298 | style={{ |
| 27902ea | | | 299 | backgroundColor: "var(--bg-card)", |
| 27902ea | | | 300 | border: "1px solid var(--border-subtle)", |
| 27902ea | | | 301 | display: "flex", |
| 27902ea | | | 302 | flexDirection: "column", |
| 27902ea | | | 303 | }} |
| 27902ea | | | 304 | > |
| 27902ea | | | 305 | <div className="flex items-center gap-2 px-2.5 py-1.5" style={{ borderBottom: "1px solid var(--divide)" }}> |
| 27902ea | | | 306 | <span className="text-sm font-medium" style={{ color: "var(--text-primary)" }}>{title}</span> |
| 27902ea | | | 307 | {count !== undefined && ( |
| 27902ea | | | 308 | <span className="text-xs" style={{ color: "var(--text-faint)" }}>{count}</span> |
| 79efd41 | | | 309 | )} |
| 79efd41 | | | 310 | </div> |
| 27902ea | | | 311 | <div style={{ flex: 1 }}>{children}</div> |
| 27902ea | | | 312 | </div> |
| 27902ea | | | 313 | ); |
| 27902ea | | | 314 | } |
| 79efd41 | | | 315 | |
| 4a006da | | | 316 | |
| 27902ea | | | 317 | /* ── Helpers ── */ |
| cf89d3c | | | 318 | |
| 27902ea | | | 319 | function StatusDot({ status }: { status: "operational" | "degraded" | "down" }) { |
| 27902ea | | | 320 | const color = |
| 27902ea | | | 321 | status === "operational" ? "var(--status-open-text)" : status === "degraded" ? "var(--status-merged-text)" : "var(--status-closed-text)"; |
| 27902ea | | | 322 | return ( |
| 27902ea | | | 323 | <span style={{ width: 7, height: 7, borderRadius: "50%", backgroundColor: color, display: "inline-block", flexShrink: 0 }} /> |
| 27902ea | | | 324 | ); |
| 27902ea | | | 325 | } |
| 27902ea | | | 326 | |
| 27902ea | | | 327 | function SkeletonRows({ n }: { n: number }) { |
| 27902ea | | | 328 | return ( |
| 27902ea | | | 329 | <div className="p-2 space-y-1"> |
| 27902ea | | | 330 | {Array.from({ length: n }, (_, i) => ( |
| 27902ea | | | 331 | <Skeleton key={i} width="100%" height="1.75rem" /> |
| 27902ea | | | 332 | ))} |
| 4a006da | | | 333 | </div> |
| 4a006da | | | 334 | ); |
| 4a006da | | | 335 | } |
| 27902ea | | | 336 | |
| 27902ea | | | 337 | function EmptyRow({ children }: { children: React.ReactNode }) { |
| 27902ea | | | 338 | return ( |
| 27902ea | | | 339 | <div className="py-4 text-center text-xs" style={{ color: "var(--text-faint)" }}>{children}</div> |
| 27902ea | | | 340 | ); |
| 27902ea | | | 341 | } |