cleanup and docs
All checks were successful
Deploy App / docker (ubuntu-latest) (push) Successful in 1m4s

This commit is contained in:
Lee
2024-04-18 07:06:16 +01:00
parent e0e6a72d92
commit 428a95c54d
18 changed files with 347 additions and 190 deletions

View File

@ -6,9 +6,25 @@ import { Separator } from "../components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "../components/ui/tooltip";
type Button = {
/**
* The title of the button.
*/
title: string;
/**
* The tooltip to display for this statistic.
*/
tooltip: string;
/**
* The URL to go to.
*/
url: string;
/**
* Whether clicking the button will
* open the link in a new tab.
*/
openInNewTab?: boolean;
};

View File

@ -1,18 +1,14 @@
/* eslint-disable @next/next/no-img-element */
import { Card } from "@/app/components/card";
import { CodeDialog } from "@/app/components/code-dialog";
import { CopyButton } from "@/app/components/copy-button";
import { ErrorCard } from "@/app/components/error-card";
import { LookupPlayer } from "@/app/components/player/lookup-player";
import { PlayerView } from "@/app/components/player/player-view";
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu";
import { Separator } from "@/app/components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/app/components/ui/tooltip";
import { Colors } from "@/common/colors";
import { generateEmbed } from "@/common/embed";
import { CachedPlayer, McUtilsAPIError, SkinPart, getPlayer } from "mcutils-library";
import { isValidPlayer } from "@/common/player";
import { CachedPlayer, McUtilsAPIError, getPlayer } from "mcutils-library";
import { Metadata, Viewport } from "next";
import Image from "next/image";
import Link from "next/link";
import { ReactElement } from "react";
import config from "../../../../../config.json";
@ -23,43 +19,28 @@ type Params = {
};
export async function generateViewport({ params: { id } }: Params): Promise<Viewport> {
try {
if (!id || id.length === 0) {
return {
themeColor: Colors.red,
};
}
await getPlayer(id); // Ensure the player is valid.
return {
themeColor: Colors.green,
};
} catch (err) {
// An error occurred
return {
themeColor: Colors.red,
};
}
const validPlayer = await isValidPlayer(id);
return {
themeColor: validPlayer ? Colors.green : Colors.red,
};
}
export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> {
// No id provided
if (!id || id.length === 0) {
return generateEmbed({
title: "Player Lookup",
description: "Click to lookup a player.",
});
}
try {
// No id provided
if (!id || id.length === 0) {
return generateEmbed({
title: "Player Lookup",
description: "Click to lookup a player.",
});
}
const player = await getPlayer(id);
const { username, uniqueId, skin } = player;
const { username, uniqueId, skin } = await getPlayer(id);
const headPartUrl = skin.parts.head;
const description = `UUID: ${uniqueId}\n\nClick to view more information about the player.`;
return generateEmbed({
title: `${username}`,
description: description,
description: `UUID: ${uniqueId}\n\nClick to view more information about the player.`,
image: headPartUrl,
});
} catch (err) {
@ -95,65 +76,7 @@ export default async function Page({ params: { id } }: Params): Promise<ReactEle
{player != undefined && (
<ContextMenu>
<ContextMenuTrigger>
<Card className="w-max xs:w-fit">
<div className="flex gap-4 flex-col xs:flex-row relative">
<div className="absolute 8xl:top-0 xs:bottom-0">
<CodeDialog
title="Player Data"
description="The player's data from the API"
code={JSON.stringify(player, undefined, 2)}
>
<button className="bg-background rounded-lg">
<p className="p-1">JSON</p>
</button>
</CodeDialog>
</div>
<div className="flex justify-center xs:justify-start">
<Image
className="w-[96px] h-[96px]"
src={player.skin.parts.head}
width={96}
height={96}
quality={100}
alt="The player's skin"
/>
</div>
<div className="flex flex-col gap-2">
<div>
<h2 className="text-xl text-primary font-semibold">{player.username}</h2>
<p>{player.uniqueId}</p>
</div>
<Separator />
<div className="flex flex-col gap-2">
<p className="text-lg">Skin Parts</p>
<div className="flex gap-2">
{Object.entries(player.skin.parts)
.filter(([part]) => part !== SkinPart.HEAD) // Don't show the head part again
.map(([part, url]) => {
return (
<Tooltip key={part}>
<TooltipTrigger>
<Link href={url} target="_blank">
<img className="h-[64px]" src={url} alt={`The player's ${part}`} loading="lazy" />
</Link>
</TooltipTrigger>
<TooltipContent>
<p>
Click to view {player.username}&apos;s {part}
</p>
</TooltipContent>
</Tooltip>
);
})}
</div>
</div>
</div>
</div>
</Card>
<PlayerView player={player} />
</ContextMenuTrigger>
<ContextMenuContent className="flex flex-col">
<CopyButton content={player.username}>

View File

@ -1,11 +1,11 @@
import { Card } from "@/app/components/card";
import { CopyButton } from "@/app/components/copy-button";
import { ErrorCard } from "@/app/components/error-card";
import { LookupServer } from "@/app/components/server/lookup-server";
import { ServerView } from "@/app/components/server/server-view";
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu";
import { Colors } from "@/common/colors";
import { generateEmbed } from "@/common/embed";
import { formatNumber } from "@/common/number-utils";
import { isValidServer } from "@/common/server";
import { capitalizeFirstLetter } from "@/common/string-utils";
import {
CachedBedrockMinecraftServer,
@ -15,7 +15,6 @@ import {
getServer,
} from "mcutils-library";
import { Metadata, Viewport } from "next";
import Image from "next/image";
import { ReactElement } from "react";
import config from "../../../../../../config.json";
@ -55,40 +54,29 @@ function checkPlatform(platform: ServerPlatform): boolean {
}
export async function generateViewport({ params: { platform, hostname } }: Params): Promise<Viewport> {
try {
if (!checkPlatform(platform) || !hostname || hostname.length === 0) {
return {
themeColor: Colors.red,
};
}
await getServer(platform, hostname); // Ensure the server is valid.
return {
themeColor: Colors.green,
};
} catch (err) {
// An error occurred
return {
themeColor: Colors.red,
};
}
const validPlayer = await isValidServer(platform, hostname);
return {
themeColor: validPlayer ? Colors.green : Colors.red,
};
}
export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> {
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.`,
});
}
try {
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.`,
});
}
const server = await getServer(platform, hostname);
const { hostname: serverHostname, players } = server as CachedJavaMinecraftServer | CachedBedrockMinecraftServer;
@ -115,21 +103,21 @@ export async function generateMetadata({ params: { platform, hostname } }: Param
export default async function Page({ params: { platform, hostname } }: Params): Promise<ReactElement> {
let error: string | undefined = undefined; // The error to display
let server: CachedJavaMinecraftServer | CachedBedrockMinecraftServer | undefined = undefined; // The server to display
let invalidPlatform = !checkPlatform(platform); // Whether the platform is invalid
let invalidPlatform: boolean = !checkPlatform(platform); // Whether the platform is invalid
let favicon: string | undefined; // The server's favicon
// Try and get the player to display
try {
if (invalidPlatform) {
error = "Invalid platform"; // Set the error message
} else {
if (invalidPlatform) {
error = "Invalid platform"; // Set the error message
} else {
// Try and get the player to display
try {
server = platform && hostname ? await getServer(platform, hostname) : undefined;
favicon = getFavicon(platform, server);
} catch (err) {
error = (err as McUtilsAPIError).message; // Set the error message
}
} catch (err) {
error = (err as McUtilsAPIError).message; // Set the error message
}
const favicon = getFavicon(platform, server);
return (
<div className="h-full flex flex-col items-center">
<div className="mb-4 text-center">
@ -143,39 +131,7 @@ export default async function Page({ params: { platform, hostname } }: Params):
{server != null && (
<ContextMenu>
<ContextMenuTrigger>
<Card className="w-max xs:w-fit relative">
<div className="flex gap-2 flex-col">
<div className="flex gap-4 flex-col xs:flex-row">
{favicon && (
<div className="flex justify-center xs:justify-start">
<Image
className="w-[64px] h-[64px]"
src={favicon}
width={64}
height={64}
quality={100}
alt="The server's favicon"
/>
</div>
)}
<div className="flex flex-col">
<h2 className="text-xl text-primary font-semibold">{server.hostname}</h2>
<div>
<p>
Players online: {formatNumber(server.players.online)}/{formatNumber(server.players.max)}
</p>
</div>
</div>
</div>
<div className="bg-background rounded-lg p-2 text-sm xs:text-lg">
{server.motd.html.map((line, index) => {
return <p key={index} dangerouslySetInnerHTML={{ __html: line }}></p>;
})}
</div>
</div>
</Card>
<ServerView server={server} favicon={favicon} />
</ContextMenuTrigger>
<ContextMenuContent className="flex flex-col">
<CopyButton content={server.hostname}>