2024-04-16 18:07:29 +01:00
|
|
|
import { embedFallback } from "@/common/embed-fallback";
|
2024-04-16 17:54:22 +01:00
|
|
|
import { capitalizeFirstLetter } from "@/common/string-utils";
|
|
|
|
import { LookupServer } from "@/components/lookup-server";
|
|
|
|
import { NotFound } from "@/components/not-found";
|
|
|
|
import { Card } from "@/components/ui/card";
|
|
|
|
import { getServer } from "mcutils-library";
|
|
|
|
import JavaMinecraftServer from "mcutils-library/dist/types/server/javaServer";
|
|
|
|
import { ServerPlatform } from "mcutils-library/dist/types/server/platform";
|
|
|
|
import { MinecraftServer } from "mcutils-library/dist/types/server/server";
|
|
|
|
import { Metadata } from "next";
|
|
|
|
|
|
|
|
type Params = {
|
|
|
|
params: {
|
|
|
|
platform: ServerPlatform;
|
|
|
|
hostname: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> {
|
|
|
|
const server = await getData(platform, hostname);
|
|
|
|
if (!server) {
|
2024-04-16 18:07:29 +01:00
|
|
|
return embedFallback({ title: "Unknown Server", description: "Server not found" });
|
2024-04-16 17:54:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const { hostname: serverHostname, players } = server;
|
2024-04-16 18:07:29 +01:00
|
|
|
|
|
|
|
let favicon = null; // Server favicon
|
2024-04-16 17:54:22 +01:00
|
|
|
|
|
|
|
if (platform === ServerPlatform.Java) {
|
2024-04-16 18:07:29 +01:00
|
|
|
// Java specific
|
2024-04-16 17:54:22 +01:00
|
|
|
const javaServer = server as JavaMinecraftServer;
|
|
|
|
favicon = javaServer.favicon && javaServer.favicon.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
const description = `
|
|
|
|
${capitalizeFirstLetter(platform)} Server
|
|
|
|
Hostname: ${serverHostname}
|
|
|
|
${players.online}/${players.max} players online`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: `${hostname}`,
|
|
|
|
openGraph: {
|
|
|
|
title: `${hostname}`,
|
|
|
|
description: description,
|
|
|
|
images: [
|
|
|
|
{
|
|
|
|
url: favicon || "",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
twitter: {
|
|
|
|
card: "summary",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getData(platform: ServerPlatform, id: string): Promise<MinecraftServer | null> {
|
|
|
|
console.log(platform, id);
|
|
|
|
try {
|
|
|
|
const cachedServer = await getServer(platform, id);
|
|
|
|
return cachedServer.server;
|
|
|
|
} catch (error) {
|
|
|
|
return null; // Server not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function Page({ params: { platform, hostname } }: Params) {
|
|
|
|
const server = await getData(platform, hostname);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="h-full flex flex-col items-center">
|
|
|
|
<div className="mb-4 text-center">
|
|
|
|
<h1 className="text-xl">Lookup a Server</h1>
|
|
|
|
<p>You can enter a server hostname to get information about the server.</p>
|
|
|
|
|
|
|
|
<LookupServer />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
{server == null && <NotFound message="Server not found" />}
|
|
|
|
{server != null && (
|
|
|
|
<div className="flex flex-col items-center gap-2">
|
|
|
|
<p>Hostname: {server.hostname}</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|