From b471e00699d9051b5fa35aac9acf23ac427a2716 Mon Sep 17 00:00:00 2001 From: anonymousratwastaken Date: Fri, 23 Oct 2026 19:40:16 +0100 Subject: [PATCH] Add live Minecraft server status chip Fetches Java server status from mcstatus.io through a cached API route and shows online state, player count, and version in a chip that refreshes every minute. Co-Authored-By: Claude Haiku 5.5 --- src/app/api/server-status/route.ts | 8 +++ src/components/server-status-chip.tsx | 99 +++++++++++++++++++++++++++ src/lib/server-status.ts | 48 +++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 src/app/api/server-status/route.ts create mode 100644 src/components/server-status-chip.tsx create mode 100644 src/lib/server-status.ts diff --git a/src/app/api/server-status/route.ts b/src/app/api/server-status/route.ts new file mode 100644 index 0000000..141dd09 --- /dev/null +++ b/src/app/api/server-status/route.ts @@ -0,0 +1,8 @@ +import { getJavaServerStatus } from "@/lib/server-status"; + +export async function GET() { + const status = await getJavaServerStatus(); + return Response.json(status, { + headers: { "Cache-Control": "public, max-age=30, s-maxage=60" }, + }); +} diff --git a/src/components/server-status-chip.tsx b/src/components/server-status-chip.tsx new file mode 100644 index 0000000..2f782d6 --- /dev/null +++ b/src/components/server-status-chip.tsx @@ -0,0 +1,99 @@ +"use client"; +import { useEffect, useState } from "react"; +import type { ServerStatus } from "@/lib/server-status"; + +const REFRESH_MS = 60_000; + +const DOT = { + online: "bg-green-400", + offline: "bg-red-400", + unknown: "bg-muted-foreground/60", +} as const; + +const TEXT = { + online: "Online", + offline: "Offline", + unknown: "Status unknown", +} as const; + +export function ServerStatusChip() { + const [status, setStatus] = useState(null); + const [open, setOpen] = useState(false); + + useEffect(() => { + let cancelled = false; + const load = async () => { + try { + const res = await fetch("/api/server-status"); + const data = (await res.json()) as ServerStatus; + if (!cancelled) setStatus(data); + } catch { + if (!cancelled) setStatus(null); + } + }; + load(); + const id = setInterval(load, REFRESH_MS); + return () => { + cancelled = true; + clearInterval(id); + }; + }, []); + + const state = status?.state ?? "unknown"; + const lastChecked = status + ? new Date(status.checkedAt).toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit", timeZone: "UTC" }) + : null; + + return ( +
+ + + {open && ( +
+ {status ? ( +
+
Address
+
{status.host}
+
Players
+
+ {state === "online" && status.players ? `${status.players.online} / ${status.players.max}` : "—"} +
+
Version
+
{state === "online" && status.version ? status.version : "—"}
+ {state === "online" && status.motd && ( + <> +
Message
+
{status.motd}
+ + )} +
+ ) : ( +

Fetching server status…

+ )} +

+ {state === "unknown" && status + ? "We couldn't reach the status service. Try again shortly." + : lastChecked + ? `Last checked ${lastChecked} UTC · refreshes every minute` + : null} +

+
+ )} +
+ ); +} diff --git a/src/lib/server-status.ts b/src/lib/server-status.ts new file mode 100644 index 0000000..39939c6 --- /dev/null +++ b/src/lib/server-status.ts @@ -0,0 +1,48 @@ +import { cfg } from "./cfg"; + +type McStatusJava = { + online: boolean; + host: string; + port: number; + ip_address: string | null; + version: { name_clean: string } | null; + players: { online: number; max: number } | null; + motd: { clean: string } | null; + retrieved_at: number; +}; + +export type ServerStatus = { + state: "online" | "offline" | "unknown"; + host: string; + version: string | null; + players: { online: number; max: number } | null; + motd: string | null; + checkedAt: number; +}; + +const STATUS_URL = "https://api.mcstatus.io/v2/status/java"; + +export async function getJavaServerStatus(): Promise { + const host = cfg.server.javaIp || "frg.network"; + const unknown: ServerStatus = { state: "unknown", host, version: null, players: null, motd: null, checkedAt: Date.now() }; + + try { + const res = await fetch(`${STATUS_URL}/${encodeURIComponent(host)}`, { + next: { revalidate: 60 }, + }); + if (!res.ok) return unknown; + + const data = (await res.json()) as McStatusJava; + return { + state: data.online ? "online" : "offline", + host, + version: data.version?.name_clean ?? null, + players: data.online && data.players ? { online: data.players.online, max: data.players.max } : null, + motd: data.motd?.clean ?? null, + checkedAt: data.retrieved_at ?? Date.now(), + }; + } catch (error) { + console.error("Server status lookup failed:", error); + return unknown; + } +}