Files
Frontend/src/app/(pages)/server/[platform]/[[...hostname]]/page.tsx

156 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-04-18 02:07:02 +01:00
import { CopyButton } from "@/app/components/copy-button";
2024-04-17 17:21:15 +01:00
import { ErrorCard } from "@/app/components/error-card";
import { LookupServer } from "@/app/components/server/lookup-server";
2024-04-18 07:06:16 +01:00
import { ServerView } from "@/app/components/server/server-view";
2024-04-18 02:07:02 +01:00
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu";
import { Colors } from "@/common/colors";
2024-04-17 17:21:15 +01:00
import { generateEmbed } from "@/common/embed";
2024-04-18 09:03:06 +01:00
import { formatNumber } from "@/common/number-utils";
2024-04-18 07:06:16 +01:00
import { isValidServer } from "@/common/server";
2024-04-17 17:21:15 +01:00
import { capitalizeFirstLetter } from "@/common/string-utils";
2024-04-18 07:32:21 +01:00
import config from "@root/config.json";
2024-04-17 17:21:15 +01:00
import {
CachedBedrockMinecraftServer,
CachedJavaMinecraftServer,
2024-04-19 15:22:03 +01:00
getServer,
2024-04-17 17:21:15 +01:00
McUtilsAPIError,
ServerPlatform,
} from "mcutils-library";
import { Metadata, Viewport } from "next";
2024-04-18 01:21:38 +01:00
import { ReactElement } from "react";
2024-04-17 17:21:15 +01:00
2024-04-19 21:19:14 +01:00
/**
* Force the page to be dynamic, so it will be regenerated on every request
*/
export const revalidate = 0;
2024-04-17 17:21:15 +01:00
type Params = {
params: {
platform: ServerPlatform;
hostname: string;
};
};
/**
* Gets the favicon for a server
*
* @param platform the platform of the server
* @param server the server to get the favicon from
* @returns the favicon url or null if there is no favicon
*/
function getFavicon(
2024-04-18 06:36:40 +01:00
platform: ServerPlatform | null,
server: CachedJavaMinecraftServer | CachedBedrockMinecraftServer | undefined,
2024-04-17 17:21:15 +01:00
): string | undefined {
2024-04-18 06:36:40 +01:00
if (server == null || platform === ServerPlatform.Bedrock) {
return config.apiUrl + "/server/icon/fallback";
2024-04-17 17:21:15 +01:00
}
server = server as CachedJavaMinecraftServer;
return server.favicon && server.favicon.url;
}
/**
* Checks if a platform is valid
*
* @param platform the platform to check
* @returns true if the platform is valid, false otherwise
*/
function checkPlatform(platform: ServerPlatform): boolean {
return platform === ServerPlatform.Java || platform === ServerPlatform.Bedrock;
}
export async function generateViewport({ params: { platform, hostname } }: Params): Promise<Viewport> {
2024-04-18 07:06:16 +01:00
const validPlayer = await isValidServer(platform, hostname);
return {
2024-04-18 08:59:13 +01:00
themeColor: validPlayer || !platform ? Colors.green : Colors.red,
2024-04-18 07:06:16 +01:00
};
}
2024-04-17 17:21:15 +01:00
export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> {
2024-04-18 07:06:16 +01:00
if (!checkPlatform(platform)) {
// Invalid platform
return generateEmbed({
title: "Server Not Found",
description: "Invalid platform",
});
}
if (!hostname || hostname.length === 0) {
// No hostname
return generateEmbed({
title: "Server Lookup",
description: `Click to lookup a ${capitalizeFirstLetter(platform)} server.`,
});
}
2024-04-17 17:21:15 +01:00
try {
const server = await getServer(platform, hostname);
2024-04-18 09:04:54 +01:00
const { hostname: serverHostname, players } = server;
2024-04-17 17:21:15 +01:00
const favicon = getFavicon(platform, server);
2024-04-17 17:21:15 +01:00
2024-04-18 09:03:06 +01:00
let description = `There is ${formatNumber(players.online)}/${formatNumber(players.max)} players connected!\n\n`;
2024-04-17 17:41:55 +01:00
description += "Click to view more information about the server.";
2024-04-17 17:21:15 +01:00
return generateEmbed({
2024-04-18 08:59:13 +01:00
title: `${capitalizeFirstLetter(platform)} Server: ${serverHostname}`,
2024-04-17 17:21:15 +01:00
description: description,
image: favicon,
});
} catch (err) {
2024-04-17 17:41:55 +01:00
// An error occurred
2024-04-17 17:21:15 +01:00
return generateEmbed({
title: "Server Not Found",
description: (err as McUtilsAPIError).message,
});
}
}
2024-04-18 01:21:38 +01:00
export default async function Page({ params: { platform, hostname } }: Params): Promise<ReactElement> {
2024-04-17 17:21:15 +01:00
let error: string | undefined = undefined; // The error to display
let server: CachedJavaMinecraftServer | CachedBedrockMinecraftServer | undefined = undefined; // The server to display
2024-04-18 07:06:16 +01:00
let invalidPlatform: boolean = !checkPlatform(platform); // Whether the platform is invalid
let favicon: string | undefined; // The server's favicon
if (invalidPlatform) {
error = "Invalid platform"; // Set the error message
} else {
// Try and get the player to display
try {
2024-04-17 17:21:15 +01:00
server = platform && hostname ? await getServer(platform, hostname) : undefined;
2024-04-18 07:06:16 +01:00
favicon = getFavicon(platform, server);
} catch (err) {
error = (err as McUtilsAPIError).message; // Set the error message
2024-04-17 17:21:15 +01:00
}
}
return (
<div className="h-full flex flex-col items-center">
<div className="mb-4 text-center">
<h1 className="text-xl">Lookup a {invalidPlatform ? "" : capitalizeFirstLetter(platform)} Server</h1>
<p>You can enter a server hostname to get information about the server.</p>
<LookupServer currentPlatform={platform.toLowerCase()} currentServer={hostname && hostname[0]} />
2024-04-17 17:21:15 +01:00
</div>
{error && <ErrorCard message={error} />}
{server != null && (
2024-04-18 02:07:02 +01:00
<ContextMenu>
<ContextMenuTrigger>
2024-04-18 07:06:16 +01:00
<ServerView server={server} favicon={favicon} />
2024-04-18 02:07:02 +01:00
</ContextMenuTrigger>
<ContextMenuContent className="flex flex-col">
<CopyButton content={server.hostname}>
<ContextMenuItem>Copy Server Hostname</ContextMenuItem>
</CopyButton>
{favicon && (
<CopyButton content={favicon}>
<ContextMenuItem>Copy Server Favicon URL</ContextMenuItem>
</CopyButton>
)}
</ContextMenuContent>
</ContextMenu>
2024-04-17 17:21:15 +01:00
)}
</div>
);
}