2024-04-16 23:16:34 +01:00
|
|
|
/* eslint-disable @next/next/no-img-element */
|
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";
|
2024-04-16 19:12:26 +01:00
|
|
|
import { LookupPlayer } from "@/app/components/player/lookup-player";
|
2024-04-18 07:06:16 +01:00
|
|
|
import { PlayerView } from "@/app/components/player/player-view";
|
2024-04-18 02:07:02 +01:00
|
|
|
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu";
|
2024-04-18 06:33:30 +01:00
|
|
|
import { Colors } from "@/common/colors";
|
2024-04-16 18:14:15 +01:00
|
|
|
import { generateEmbed } from "@/common/embed";
|
2024-04-18 07:06:16 +01:00
|
|
|
import { isValidPlayer } from "@/common/player";
|
2024-04-18 07:32:21 +01:00
|
|
|
import config from "@root/config.json";
|
2024-04-19 15:22:03 +01:00
|
|
|
import { CachedPlayer, getPlayer, McUtilsAPIError } from "mcutils-library";
|
2024-04-18 06:33:30 +01:00
|
|
|
import { Metadata, Viewport } from "next";
|
2024-04-18 01:21:38 +01:00
|
|
|
import { ReactElement } from "react";
|
2024-04-16 17:54:22 +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-16 18:07:29 +01:00
|
|
|
type Params = {
|
|
|
|
params: {
|
|
|
|
id: string;
|
|
|
|
};
|
2024-04-16 17:54:22 +01:00
|
|
|
};
|
|
|
|
|
2024-04-18 06:33:30 +01:00
|
|
|
export async function generateViewport({ params: { id } }: Params): Promise<Viewport> {
|
2024-04-18 07:06:16 +01:00
|
|
|
const validPlayer = await isValidPlayer(id);
|
|
|
|
return {
|
|
|
|
themeColor: validPlayer ? Colors.green : Colors.red,
|
|
|
|
};
|
2024-04-18 06:33:30 +01:00
|
|
|
}
|
|
|
|
|
2024-04-16 18:07:29 +01:00
|
|
|
export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> {
|
2024-04-18 07:06:16 +01:00
|
|
|
// No id provided
|
|
|
|
if (!id || id.length === 0) {
|
|
|
|
return generateEmbed({
|
|
|
|
title: "Player Lookup",
|
|
|
|
description: "Click to lookup a player.",
|
|
|
|
});
|
|
|
|
}
|
2024-04-16 18:07:29 +01:00
|
|
|
|
2024-04-18 07:06:16 +01:00
|
|
|
try {
|
|
|
|
const { username, uniqueId, skin } = await getPlayer(id);
|
2024-04-17 17:21:15 +01:00
|
|
|
const headPartUrl = skin.parts.head;
|
2024-04-16 18:07:29 +01:00
|
|
|
|
2024-04-17 17:21:15 +01:00
|
|
|
return generateEmbed({
|
|
|
|
title: `${username}`,
|
2024-04-18 07:06:16 +01:00
|
|
|
description: `UUID: ${uniqueId}\n\nClick to view more information about the player.`,
|
2024-04-17 17:21:15 +01:00
|
|
|
image: headPartUrl,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2024-04-17 17:41:55 +01:00
|
|
|
// An error occurred
|
2024-04-17 17:21:15 +01:00
|
|
|
return generateEmbed({
|
|
|
|
title: "Player Not Found",
|
|
|
|
description: (err as McUtilsAPIError).message,
|
|
|
|
});
|
|
|
|
}
|
2024-04-16 17:54:22 +01:00
|
|
|
}
|
|
|
|
|
2024-04-18 01:21:38 +01:00
|
|
|
export default async function Page({ params: { id } }: Params): Promise<ReactElement> {
|
2024-04-17 17:21:15 +01:00
|
|
|
let error: string | undefined = undefined; // The error to display
|
|
|
|
let player: CachedPlayer | undefined = undefined; // The player to display
|
2024-04-16 23:45:54 +01:00
|
|
|
|
2024-04-17 17:21:15 +01:00
|
|
|
// Try and get the player to display
|
2024-04-16 23:45:54 +01:00
|
|
|
try {
|
2024-04-17 17:21:15 +01:00
|
|
|
player = id ? await getPlayer(id) : undefined;
|
2024-04-16 23:45:54 +01:00
|
|
|
} catch (err) {
|
2024-04-17 17:21:15 +01:00
|
|
|
error = (err as McUtilsAPIError).message; // Set the error message
|
2024-04-16 23:45:54 +01:00
|
|
|
}
|
2024-04-16 17:54:22 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="h-full flex flex-col items-center">
|
|
|
|
<div className="mb-4 text-center">
|
|
|
|
<h1 className="text-xl">Lookup a Player</h1>
|
|
|
|
<p>You can enter a players uuid or username to get information about the player.</p>
|
2024-04-16 19:03:11 +01:00
|
|
|
|
2024-04-18 07:59:03 +01:00
|
|
|
<LookupPlayer currentPlayer={id && id[0]} />
|
2024-04-16 17:54:22 +01:00
|
|
|
</div>
|
|
|
|
|
2024-04-17 17:21:15 +01:00
|
|
|
{error && <ErrorCard message={error} />}
|
|
|
|
{player != undefined && (
|
2024-04-18 02:07:02 +01:00
|
|
|
<ContextMenu>
|
|
|
|
<ContextMenuTrigger>
|
2024-04-18 07:06:16 +01:00
|
|
|
<PlayerView player={player} />
|
2024-04-18 02:07:02 +01:00
|
|
|
</ContextMenuTrigger>
|
|
|
|
<ContextMenuContent className="flex flex-col">
|
|
|
|
<CopyButton content={player.username}>
|
|
|
|
<ContextMenuItem>Copy Player Username</ContextMenuItem>
|
|
|
|
</CopyButton>
|
|
|
|
|
|
|
|
<CopyButton content={player.uniqueId}>
|
|
|
|
<ContextMenuItem>Copy Player UUID</ContextMenuItem>
|
|
|
|
</CopyButton>
|
|
|
|
|
|
|
|
<CopyButton content={`${config.siteUrl}/player/${id}`}>
|
|
|
|
<ContextMenuItem>Copy Share URL</ContextMenuItem>
|
|
|
|
</CopyButton>
|
|
|
|
</ContextMenuContent>
|
|
|
|
</ContextMenu>
|
2024-04-17 17:21:15 +01:00
|
|
|
)}
|
2024-04-16 17:54:22 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|