| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useRef, useState } from "react"; |
| 4 | import { use } |
| 5 | import { useRouter } |
| 6 | import { useQuery, useMutation |
| 7 | import { |
| 8 | Globe, |
| 9 | Cpu, |
| 10 | HardDrive, |
| 11 | ArrowDown, |
| 12 | ArrowUp, |
| 13 | Copy, |
| 14 | SendHorizontal, |
| 15 | MemoryStick, |
| 16 | Timer, |
| 17 | } from "lucide-react"; |
| 18 | import type { LucideIcon |
| 19 | import { useTranslations } |
| 20 | import { orpc } |
| 21 | import { authClient } |
| 22 | import { toast } |
| 23 | import Loader from "@/components/loader |
| 24 | import { |
| 25 | Dialog, |
| 26 | DialogPopup, |
| 27 | DialogHeader, |
| 28 | DialogFooter, |
| 29 | DialogTitle, |
| 30 | DialogDescription, |
| 31 | } from "@struxa/ui/components/dialog"; |
| 32 | import { |
| 33 | DropdownMenu, |
| 34 | DropdownMenuContent, |
| 35 | DropdownMenuItem, |
| 36 | DropdownMenuTrigger, |
| 37 | } from "@struxa/ui/components/dropdown-menu"; |
| 38 | import { ChevronDown } |
| 39 | import { Sparkline } |
| 40 | |
| 41 | function fmtUptime(ms: |
| 42 | if (ms <= |
| 43 | const secs = |
| 44 | const d = |
| 45 | const h = |
| 46 | const m = |
| 47 | const s = |
| 48 | const parts: |
| 49 | if (d > |
| 50 | if (h > |
| 51 | if (m > |
| 52 | parts.push(`${ |
| 53 | return parts.join |
| 54 | } |
| 55 | |
| 56 | function fmtMb(mb: |
| 57 | return mb >= |
| 58 | } |
| 59 | |
| 60 | function fmtBytes(bytes: |
| 61 | if (bytes < |
| 62 | if (bytes < |
| 63 | return `${(bytes |
| 64 | } |
| 65 | |
| 66 | function StatRow({ |
| 67 | icon: Icon, |
| 68 | label, |
| 69 | chart, |
| 70 | children, |
| 71 | }: { |
| 72 | icon: LucideIcon |
| 73 | label: string |
| 74 | chart?: React |
| 75 | children: React |
| 76 | }) { |
| 77 | return ( |
| 78 | <div className="flex flex-col border-b border-border last:border-b-0 |
| 79 | <div className="flex flex-col gap-1.5 px-4 pt-3 pb-2.5 |
| 80 | <div className="flex items-center gap-1.5 text-[10px] font-medium uppercase tracking-widest text-muted-foreground |
| 81 | <Icon className=" |
| 82 | {label} |
| 83 | </div> |
| 84 | {children} |
| 85 | </div> |
| 86 | {chart} |
| 87 | </div> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | type WsStatus |
| 92 | |
| 93 | const ANSI_FG: Record |
| 94 | 30: "#4a4a4a", 31 |
| 95 | 34: "#3b82f6", 35 |
| 96 | 90: "#777777", 91 |
| 97 | 94: "#60a5fa", 95 |
| 98 | }; |
| 99 | const ANSI_BG: Record |
| 100 | 40: "#4a4a4a", 41 |
| 101 | 44: "#3b82f6", 45 |
| 102 | 100: "#777777", 101 |
| 103 | 104: "#60a5fa", 105 |
| 104 | }; |
| 105 | |
| 106 | interface AnsiSpan { |
| 107 | |
| 108 | function parseAnsi(raw: |
| 109 | const spans: |
| 110 | const re = |
| 111 | let pos = |
| 112 | let color: |
| 113 | let bg: |
| 114 | let bold = |
| 115 | |
| 116 | let m: |
| 117 | while ((m = |
| 118 | if (m.index |
| 119 | spans.push({ |
| 120 | } |
| 121 | const codes = |
| 122 | for (const code |
| 123 | if (code === |
| 124 | else if (code |
| 125 | else if (code |
| 126 | else if (ANSI_FG[ |
| 127 | else if (ANSI_BG[ |
| 128 | } |
| 129 | pos = m. |
| 130 | } |
| 131 | if (pos < |
| 132 | return spans; |
| 133 | } |
| 134 | |
| 135 | function processConsoleLine(raw: |
| 136 | // Split on cursor-horizontal-absolute (\x1b[nG) - spinner animations emit |
| 137 | // \x1b[1G\x1b[0K<char> in a loop to overwrite the same position. Treat each |
| 138 | // G-delimited segment as a "frame" and keep the last one with visible content. |
| 139 | const frames = |
| 140 | let result = |
| 141 | for (let i |
| 142 | const frame = |
| 143 | if (frame.replace |
| 144 | } |
| 145 | // Strip any remaining non-SGR escape sequences (erase, cursor, etc.) |
| 146 | return result.replace |
| 147 | } |
| 148 | |
| 149 | function AnsiLine({ raw |
| 150 | const spans = |
| 151 | return ( |
| 152 | <> |
| 153 | {spans.map(( |
| 154 | <span |
| 155 | key={i} |
| 156 | style={{ |
| 157 | color: s.color |
| 158 | backgroundColor: s.bg |
| 159 | fontWeight: s.bold |
| 160 | }} |
| 161 | > |
| 162 | {s.text} |
| 163 | </span> |
| 164 | ))} |
| 165 | </> |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | const EMPTY_HISTORY = Array |
| 170 | |
| 171 | export default function ServerPage |
| 172 | const t = |
| 173 | const tStatus = |
| 174 | const tc = |
| 175 | const router = |
| 176 | const { data: |
| 177 | const { id |
| 178 | const queryClient = |
| 179 | |
| 180 | useEffect(() => |
| 181 | if (!isPending && |
| 182 | }, [isPending, |
| 183 | |
| 184 | const wsRef = |
| 185 | const intentionalClose = |
| 186 | const consoleEndRef = |
| 187 | const prevRxRef = |
| 188 | const prevTxRef = |
| 189 | const [lines, |
| 190 | const [command, |
| 191 | const [wsStatus, |
| 192 | const [connected, |
| 193 | const ZERO_STATS = |
| 194 | const [stats, |
| 195 | const statsRef = |
| 196 | const [cpuHistory, |
| 197 | const [ramHistory, |
| 198 | const [diskHistory, |
| 199 | const [rxHistory, |
| 200 | const [txHistory, |
| 201 | const [eulaDialogOpen, |
| 202 | const [javaDialogOpen, |
| 203 | const [javaSelectedImage, |
| 204 | const eulaShownRef = |
| 205 | const javaShownRef = |
| 206 | |
| 207 | const { data: |
| 208 | const eggFeaturesRef = |
| 209 | const serverImageRef = |
| 210 | useEffect(() => |
| 211 | const prev = |
| 212 | try { |
| 213 | const parsed = |
| 214 | eggFeaturesRef.current = |
| 215 | } catch { |
| 216 | eggFeaturesRef.current = |
| 217 | } |
| 218 | serverImageRef.current = |
| 219 | const features = |
| 220 | if (prev.length |
| 221 | if (features.includes |
| 222 | eulaShownRef.current = |
| 223 | setEulaDialogOpen(true); |
| 224 | } |
| 225 | if (features.includes |
| 226 | javaShownRef.current = |
| 227 | setJavaSelectedImage(serverImageRef.current |
| 228 | setJavaDialogOpen(true); |
| 229 | } |
| 230 | } |
| 231 | }, [server?.egg |
| 232 | |
| 233 | const [reconnectKey, |
| 234 | |
| 235 | useEffect(() => |
| 236 | let cancelled = |
| 237 | intentionalClose.current = |
| 238 | |
| 239 | const ZERO = |
| 240 | |
| 241 | void queryClient |
| 242 | .fetchQuery({ |
| 243 | ...orpc.servers. |
| 244 | staleTime: 0, |
| 245 | }) |
| 246 | .then((wsData) |
| 247 | if (cancelled) return |
| 248 | |
| 249 | const ws = |
| 250 | wsRef.current = |
| 251 | setConnected(false); |
| 252 | |
| 253 | ws.onopen = |
| 254 | setConnected(true); |
| 255 | ws.send(JSON |
| 256 | }; |
| 257 | |
| 258 | ws.onmessage = |
| 259 | const msg = |
| 260 | |
| 261 | if (msg.event |
| 262 | ws.send(JSON |
| 263 | ws.send(JSON |
| 264 | } else if (msg |
| 265 | const line = |
| 266 | setLines((prev) |
| 267 | const features = |
| 268 | if (features.includes |
| 269 | eulaShownRef.current = |
| 270 | setEulaDialogOpen(true); |
| 271 | } |
| 272 | if (features.includes |
| 273 | javaShownRef.current = |
| 274 | setJavaSelectedImage(serverImageRef.current |
| 275 | setJavaDialogOpen(true); |
| 276 | } |
| 277 | } else if (msg |
| 278 | try { |
| 279 | const raw = |
| 280 | cpu_absolute?: number |
| 281 | memory_bytes?: number |
| 282 | memory_limit_bytes?: number |
| 283 | disk_bytes?: number |
| 284 | uptime?: number |
| 285 | network?: { |
| 286 | }; |
| 287 | const cpu = |
| 288 | const memBytes = |
| 289 | const memLimitBytes = |
| 290 | const diskMb = |
| 291 | const rxBytes = |
| 292 | const txBytes = |
| 293 | const uptimeMs = |
| 294 | statsRef.current = |
| 295 | setStats({ cpu, |
| 296 | } catch {} |
| 297 | } else if (msg |
| 298 | const status = |
| 299 | setWsStatus(status); |
| 300 | if (status === |
| 301 | statsRef.current = |
| 302 | setStats(ZERO); |
| 303 | } |
| 304 | } else if (msg |
| 305 | void queryClient |
| 306 | .fetchQuery({ |
| 307 | ...orpc.servers |
| 308 | staleTime: 0, |
| 309 | }) |
| 310 | .then((fresh) |
| 311 | if (ws.readyState |
| 312 | ws.send(JSON |
| 313 | } |
| 314 | }); |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | ws.onclose = |
| 319 | if (cancelled) return |
| 320 | setConnected(false); |
| 321 | statsRef.current = |
| 322 | setStats(ZERO); |
| 323 | if (!intentionalClose.current |
| 324 | setTimeout(() => |
| 325 | } |
| 326 | }; |
| 327 | }); |
| 328 | |
| 329 | return () => |
| 330 | cancelled = true; |
| 331 | intentionalClose.current = |
| 332 | wsRef.current?.close |
| 333 | wsRef.current = |
| 334 | }; |
| 335 | }, [reconnectKey, |
| 336 | |
| 337 | useEffect(() => |
| 338 | consoleEndRef.current?.scrollIntoView |
| 339 | }, [lines]); |
| 340 | |
| 341 | useEffect(() => |
| 342 | const intervalId = |
| 343 | const s = |
| 344 | const rxRate = |
| 345 | const txRate = |
| 346 | prevRxRef.current = |
| 347 | prevTxRef.current = |
| 348 | setCpuHistory((prev) |
| 349 | setRamHistory((prev) |
| 350 | setDiskHistory((prev) |
| 351 | setRxHistory((prev) |
| 352 | setTxHistory((prev) |
| 353 | }, 2000); |
| 354 | return () => |
| 355 | }, []); |
| 356 | |
| 357 | const powerMutation = |
| 358 | |
| 359 | const [eulaAccepting, |
| 360 | async function acceptEula |
| 361 | setEulaAccepting(true); |
| 362 | try { |
| 363 | const res = |
| 364 | method: "POST", |
| 365 | headers: { "Content-Type": |
| 366 | body: "#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://aka.ms/MinecraftEULA).\neula=true\n |
| 367 | }); |
| 368 | if (!res.ok |
| 369 | setEulaDialogOpen(false); |
| 370 | toast.success(t |
| 371 | } catch (e) |
| 372 | toast.error(e |
| 373 | } finally { |
| 374 | setEulaAccepting(false); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | const updateImageMutation = |
| 379 | ...orpc.servers. |
| 380 | onSuccess: () => |
| 381 | setJavaDialogOpen(false); |
| 382 | void queryClient.invalidateQueries( |
| 383 | toast.success(t |
| 384 | }, |
| 385 | }); |
| 386 | |
| 387 | function sendCommand() { |
| 388 | if (!command.trim |
| 389 | wsRef.current.send |
| 390 | setCommand(""); |
| 391 | } |
| 392 | |
| 393 | function copy(text |
| 394 | void navigator.clipboard. |
| 395 | } |
| 396 | |
| 397 | if (isPending || |
| 398 | |
| 399 | const alloc = |
| 400 | const allocDisplay = |
| 401 | const canStart = |
| 402 | const canStop = |
| 403 | const canKill = |
| 404 | const canRestart = |
| 405 | |
| 406 | const wsStatusColor = |
| 407 | wsStatus === "running" |
| 408 | ? "#22c55e" |
| 409 | : wsStatus === " |
| 410 | ? "#f59e0b" |
| 411 | : "#71717a"; |
| 412 | |
| 413 | const dockerImageOptions: |
| 414 | try { |
| 415 | const parsed = |
| 416 | return Object.entries |
| 417 | } catch { return |
| 418 | })(); |
| 419 | |
| 420 | return ( |
| 421 | <> |
| 422 | <div className="flex flex-1 flex-col gap-3 overflow-auto px-4 py-4 md:flex-row md:overflow-hidden |
| 423 | {/* Console panel */} |
| 424 | <div className="flex min-h-[50vh] flex-1 flex-col overflow-hidden rounded-xl border border-border bg-background md:min-h-0 |
| 425 | <div className="flex h-10 shrink-0 items-center justify-between rounded-t-xl border-b border-border/50 bg-card px-3 |
| 426 | <div className="flex items-center gap-2 |
| 427 | <span |
| 428 | className={`h-2 w-2 rounded-full ${ |
| 429 | !connected |
| 430 | ? "animate-pulse bg-zinc-600" |
| 431 | : wsStatus === " |
| 432 | ? "bg-green-500" |
| 433 | : wsStatus === " |
| 434 | ? "animate-pulse bg-amber-500" |
| 435 | : "bg-zinc-600" |
| 436 | }`} |
| 437 | /> |
| 438 | <span className="text-xs font-medium |
| 439 | {tStatus(wsStatus)} |
| 440 | </span> |
| 441 | </div> |
| 442 | <div className="flex items-center gap-2 |
| 443 | <button |
| 444 | type="button" |
| 445 | disabled={!canStart |
| 446 | onClick={() => |
| 447 | className="rounded-md bg-green-500/20 px-2.5 py-1 text-xs font-medium text-green-400 transition-colors hover:bg-green-500/30 disabled:cursor-not-allowed disabled:opacity-30" |
| 448 | > |
| 449 | {t("start") |
| 450 | </button> |
| 451 | <button |
| 452 | type="button" |
| 453 | disabled={!canRestart |
| 454 | onClick={() => |
| 455 | className="rounded-md bg-zinc-700/60 px-2.5 py-1 text-xs font-medium text-zinc-300 transition-colors hover:bg-zinc-700 disabled:cursor-not-allowed disabled:opacity-30" |
| 456 | > |
| 457 | {t("restart") |
| 458 | </button> |
| 459 | {canKill ? ( |
| 460 | <button |
| 461 | type="button" |
| 462 | disabled={powerMutation.isPending |
| 463 | onClick={() => |
| 464 | className="rounded-md bg-red-500/20 px-2.5 py-1 text-xs font-medium text-red-400 transition-colors hover:bg-red-500/30 disabled:cursor-not-allowed disabled:opacity-30" |
| 465 | > |
| 466 | {t("kill") |
| 467 | </button> |
| 468 | ) : ( |
| 469 | <button |
| 470 | type="button" |
| 471 | disabled={!canStop |
| 472 | onClick={() => |
| 473 | className="rounded-md bg-red-500/20 px-2.5 py-1 text-xs font-medium text-red-400 transition-colors hover:bg-red-500/30 disabled:cursor-not-allowed disabled:opacity-30" |
| 474 | > |
| 475 | {t("stop") |
| 476 | </button> |
| 477 | )} |
| 478 | </div> |
| 479 | </div> |
| 480 | |
| 481 | <div className="flex-1 overflow-y-auto p-3 font-mono text-xs |
| 482 | {lines.length === |
| 483 | <div className="text-zinc-700 |
| 484 | ) : ( |
| 485 | lines.map((line |
| 486 | <div key={i |
| 487 | <AnsiLine raw={ |
| 488 | </div> |
| 489 | )) |
| 490 | )} |
| 491 | <div ref={consoleEndRef |
| 492 | </div> |
| 493 | |
| 494 | <div className="flex h-11 shrink-0 items-center rounded-b-xl border-t border-border/50 bg-card |
| 495 | <span className="px-3 font-mono text-sm text-zinc-600 |
| 496 | <input |
| 497 | className="flex-1 bg-transparent font-mono text-sm text-zinc-200 outline-none placeholder:text-zinc-700" |
| 498 | placeholder={t(" |
| 499 | value={command} |
| 500 | onChange={(e) |
| 501 | onKeyDown={(e) |
| 502 | if (e.key |
| 503 | }} |
| 504 | /> |
| 505 | <button |
| 506 | type="button" |
| 507 | onClick={sendCommand} |
| 508 | className="px-3 text-zinc-600 transition-colors hover:text-blue-400" |
| 509 | > |
| 510 | <SendHorizontal className=" |
| 511 | </button> |
| 512 | </div> |
| 513 | </div> |
| 514 | |
| 515 | {/* Stats panel */} |
| 516 | <aside className="flex w-full shrink-0 flex-col overflow-hidden rounded-xl border border-border bg-card md:w-[260px] |
| 517 | <div className="overflow-y-auto |
| 518 | <StatRow icon={ |
| 519 | <div className="flex items-center justify-between |
| 520 | <span className="text-sm font-semibold text-foreground |
| 521 | {allocDisplay} |
| 522 | </span> |
| 523 | {alloc && ( |
| 524 | <button |
| 525 | type="button" |
| 526 | onClick={() => |
| 527 | className="rounded p-0.5 text-muted-foreground/50 transition-colors hover:bg-muted hover:text-foreground" |
| 528 | > |
| 529 | <Copy className=" |
| 530 | </button> |
| 531 | )} |
| 532 | </div> |
| 533 | </StatRow> |
| 534 | <StatRow icon={ |
| 535 | <span className="text-sm font-semibold text-foreground |
| 536 | </StatRow> |
| 537 | <StatRow icon={ |
| 538 | <div className="flex items-baseline gap-1 |
| 539 | <span className="text-xl font-bold text-foreground |
| 540 | <span className="text-xs text-muted-foreground |
| 541 | </div> |
| 542 | </StatRow> |
| 543 | <StatRow icon={ |
| 544 | <div className="flex items-baseline gap-1 |
| 545 | <span className="text-xl font-bold text-foreground |
| 546 | {fmtMb(stats.memBytes |
| 547 | </span> |
| 548 | <span className="text-xs text-muted-foreground |
| 549 | </div> |
| 550 | </StatRow> |
| 551 | <StatRow icon={ |
| 552 | <div className="flex items-baseline gap-1 |
| 553 | <span className="text-xl font-bold text-foreground |
| 554 | <span className="text-xs text-muted-foreground |
| 555 | </div> |
| 556 | </StatRow> |
| 557 | <StatRow icon={ |
| 558 | <span className="text-xl font-bold text-foreground |
| 559 | </StatRow> |
| 560 | <StatRow icon={ |
| 561 | <span className="text-xl font-bold text-foreground |
| 562 | </StatRow> |
| 563 | </div> |
| 564 | </aside> |
| 565 | </div> |
| 566 | |
| 567 | <Dialog open={ |
| 568 | <DialogPopup showCloseButton={ |
| 569 | <DialogHeader> |
| 570 | <DialogTitle>{t( |
| 571 | <DialogDescription>{t( |
| 572 | </DialogHeader> |
| 573 | <DialogFooter> |
| 574 | <button |
| 575 | type="button" |
| 576 | onClick={() => |
| 577 | className="rounded-lg px-3 py-1.5 text-sm text-muted-foreground transition-colors hover:bg-muted" |
| 578 | > |
| 579 | {tc("cancel") |
| 580 | </button> |
| 581 | <button |
| 582 | type="button" |
| 583 | disabled={eulaAccepting} |
| 584 | onClick={() => |
| 585 | className="rounded-lg bg-blue-500/20 px-3 py-1.5 text-sm font-medium text-blue-400 transition-colors hover:bg-blue-500/30 disabled:cursor-not-allowed disabled:opacity-50" |
| 586 | > |
| 587 | {t("eulaAccept") |
| 588 | </button> |
| 589 | </DialogFooter> |
| 590 | </DialogPopup> |
| 591 | </Dialog> |
| 592 | |
| 593 | <Dialog open={ |
| 594 | <DialogPopup showCloseButton={ |
| 595 | <DialogHeader> |
| 596 | <DialogTitle>{t( |
| 597 | <DialogDescription>{t( |
| 598 | </DialogHeader> |
| 599 | {dockerImageOptions.length > |
| 600 | <div className="px-5 py-3 |
| 601 | <DropdownMenu> |
| 602 | <DropdownMenuTrigger className=" |
| 603 | <span className="truncate |
| 604 | <ChevronDown className=" |
| 605 | </DropdownMenuTrigger> |
| 606 | <DropdownMenuContent className=" |
| 607 | {dockerImageOptions.map(( |
| 608 | <DropdownMenuItem |
| 609 | key={tag} |
| 610 | onClick={() => |
| 611 | className="flex cursor-pointer items-center gap-2.5 text-sm" |
| 612 | > |
| 613 | <span className={` |
| 614 | {label} |
| 615 | </DropdownMenuItem> |
| 616 | ))} |
| 617 | </DropdownMenuContent> |
| 618 | </DropdownMenu> |
| 619 | </div> |
| 620 | )} |
| 621 | <DialogFooter> |
| 622 | <button |
| 623 | type="button" |
| 624 | onClick={() => |
| 625 | className="rounded-lg px-3 py-1.5 text-sm text-muted-foreground transition-colors hover:bg-muted" |
| 626 | > |
| 627 | {tc("cancel") |
| 628 | </button> |
| 629 | <button |
| 630 | type="button" |
| 631 | disabled={updateImageMutation.isPending |
| 632 | onClick={() => |
| 633 | className="rounded-lg bg-blue-500/20 px-3 py-1.5 text-sm font-medium text-blue-400 transition-colors hover:bg-blue-500/30 disabled:cursor-not-allowed disabled:opacity-50" |
| 634 | > |
| 635 | {t("javaApply") |
| 636 | </button> |
| 637 | </DialogFooter> |
| 638 | </DialogPopup> |
| 639 | </Dialog> |
| 640 | </> |
| 641 | ); |
| 642 | } |