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 <noreply@anthropic.com>
This commit is contained in:
parent
32c91afc00
commit
b471e00699
3 changed files with 155 additions and 0 deletions
8
src/app/api/server-status/route.ts
Normal file
8
src/app/api/server-status/route.ts
Normal file
|
|
@ -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" },
|
||||
});
|
||||
}
|
||||
99
src/components/server-status-chip.tsx
Normal file
99
src/components/server-status-chip.tsx
Normal file
|
|
@ -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<ServerStatus | null>(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 (
|
||||
<div className="inline-flex flex-col items-center">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
aria-expanded={open}
|
||||
aria-controls="server-status-panel"
|
||||
className="inline-flex items-center gap-2 rounded-full border border-border bg-background/60 backdrop-blur-sm px-3 py-1 text-xs font-medium text-muted-foreground hover:text-foreground transition-colors"
|
||||
>
|
||||
<span className={`inline-flex h-2 w-2 rounded-full ${DOT[state]}`} />
|
||||
<span>{status ? TEXT[state] : "Checking…"}</span>
|
||||
{state === "online" && status?.players && (
|
||||
<span className="text-muted-foreground/70">· {status.players.online} online</span>
|
||||
)}
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div
|
||||
id="server-status-panel"
|
||||
className="mt-2 w-72 rounded-xl border border-border bg-background/80 backdrop-blur-md p-4 text-left text-sm shadow-xl shadow-black/30"
|
||||
>
|
||||
{status ? (
|
||||
<dl className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-2 text-xs">
|
||||
<dt className="text-muted-foreground">Address</dt>
|
||||
<dd className="font-mono text-foreground">{status.host}</dd>
|
||||
<dt className="text-muted-foreground">Players</dt>
|
||||
<dd className="text-foreground">
|
||||
{state === "online" && status.players ? `${status.players.online} / ${status.players.max}` : "—"}
|
||||
</dd>
|
||||
<dt className="text-muted-foreground">Version</dt>
|
||||
<dd className="text-foreground">{state === "online" && status.version ? status.version : "—"}</dd>
|
||||
{state === "online" && status.motd && (
|
||||
<>
|
||||
<dt className="text-muted-foreground">Message</dt>
|
||||
<dd className="text-foreground">{status.motd}</dd>
|
||||
</>
|
||||
)}
|
||||
</dl>
|
||||
) : (
|
||||
<p className="text-xs text-muted-foreground">Fetching server status…</p>
|
||||
)}
|
||||
<p className="mt-3 text-[11px] text-muted-foreground/70">
|
||||
{state === "unknown" && status
|
||||
? "We couldn't reach the status service. Try again shortly."
|
||||
: lastChecked
|
||||
? `Last checked ${lastChecked} UTC · refreshes every minute`
|
||||
: null}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
48
src/lib/server-status.ts
Normal file
48
src/lib/server-status.ts
Normal file
|
|
@ -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<ServerStatus> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue