This commit is contained in:
100
src/app/(pages)/server/[platform]/[hostname]/page.tsx
Normal file
100
src/app/(pages)/server/[platform]/[hostname]/page.tsx
Normal file
@ -0,0 +1,100 @@
|
||||
import { NotFound } from "@/app/components/not-found";
|
||||
import { LookupServer } from "@/app/components/server/lookup-server";
|
||||
import { Card } from "@/app/components/ui/card";
|
||||
import { generateEmbed } from "@/common/embed";
|
||||
import { capitalizeFirstLetter } from "@/common/string-utils";
|
||||
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";
|
||||
import Image from "next/image";
|
||||
|
||||
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 == null) {
|
||||
return generateEmbed({ title: "Unknown Server", description: "Server not found" });
|
||||
}
|
||||
|
||||
const { hostname: serverHostname, players } = server;
|
||||
|
||||
let favicon = null; // Server favicon
|
||||
|
||||
// Java specific
|
||||
if (platform === ServerPlatform.Java) {
|
||||
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 generateEmbed({
|
||||
title: `${serverHostname}`,
|
||||
description: description,
|
||||
image: favicon,
|
||||
});
|
||||
}
|
||||
|
||||
async function getData(platform: ServerPlatform, id: string): Promise<MinecraftServer | null> {
|
||||
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);
|
||||
|
||||
let favicon = null; // Server favicon
|
||||
|
||||
// Java specific
|
||||
if (server && platform === ServerPlatform.Java) {
|
||||
const javaServer = server as JavaMinecraftServer;
|
||||
favicon = javaServer.favicon && javaServer.favicon.url;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col items-center">
|
||||
<div className="mb-4 text-center">
|
||||
<h1 className="text-xl">Lookup a {capitalizeFirstLetter(platform)} 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 gap-2 flex-col md:flex-row">
|
||||
{favicon && (
|
||||
<div className="flex justify-center md:justify-start">
|
||||
<Image className="w-[96px] h-[96px]" src={favicon} width={96} height={96} alt="The server's favicon" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex flex-col">
|
||||
<h2 className="text-xl">{server.hostname}</h2>
|
||||
<div className="text-gray-300">
|
||||
<p>
|
||||
Players online: {server.players.online}/{server.players.max}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user