From b33ed9378bac08849795046bfc13e24a52ac6d60 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 16 Apr 2024 18:07:29 +0100 Subject: [PATCH] add unknown server and unknown player and impl player embed --- package.json | 2 +- pnpm-lock.yaml | 8 ++-- src/app/player/[id]/page.tsx | 43 +++++++++++++++---- src/app/server/[platform]/[hostname]/page.tsx | 9 ++-- src/common/embed-fallback.ts | 23 ++++++++++ 5 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 src/common/embed-fallback.ts diff --git a/package.json b/package.json index d602e59..6bc093e 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.1.0", "lucide-react": "^0.368.0", - "mcutils-library": "^1.1.0", + "mcutils-library": "^1.1.1", "next": "14.2.1", "next-themes": "^0.3.0", "react": "^18", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ddf07b..24e7b94 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,8 +21,8 @@ dependencies: specifier: ^0.368.0 version: 0.368.0(react@18.2.0) mcutils-library: - specifier: ^1.1.0 - version: 1.1.0(@babel/core@7.24.4)(@types/node@20.12.7) + specifier: ^1.1.1 + version: 1.1.1(@babel/core@7.24.4)(@types/node@20.12.7) next: specifier: 14.2.1 version: 14.2.1(@babel/core@7.24.4)(react-dom@18.2.0)(react@18.2.0) @@ -3516,8 +3516,8 @@ packages: tmpl: 1.0.5 dev: false - /mcutils-library@1.1.0(@babel/core@7.24.4)(@types/node@20.12.7): - resolution: {integrity: sha512-G3qeRybVMUlfd36+ioqG5YsMgX+zDy3aeLk0qRqkjZ3zXvOgZ1OhjOFP7lEUcsMQ5nmcgwCCBqzk/UpAGiiDXg==} + /mcutils-library@1.1.1(@babel/core@7.24.4)(@types/node@20.12.7): + resolution: {integrity: sha512-OxZfC5KbqIYWcWJK+bikaCD9peCM3acgFP0e3OLLb+STU2czyaLMIt2sQ3yrqoS81emfKpaScQ5qK4nSTBGI1Q==} dependencies: axios: 1.6.8 jest: 29.7.0(@types/node@20.12.7)(ts-node@10.9.2) diff --git a/src/app/player/[id]/page.tsx b/src/app/player/[id]/page.tsx index 18e93c0..73e9e53 100644 --- a/src/app/player/[id]/page.tsx +++ b/src/app/player/[id]/page.tsx @@ -1,13 +1,46 @@ +import { embedFallback } from "@/common/embed-fallback"; import { NotFound } from "@/components/not-found"; import { Card } from "@/components/ui/card"; import { getPlayer } from "mcutils-library"; import { Player } from "mcutils-library/dist/types/player/player"; import { Metadata } from "next"; -export const metadata: Metadata = { - title: "Lookup Player", +type Params = { + params: { + id: string; + }; }; +export async function generateMetadata({ params: { id } }: Params): Promise { + const player = await getData(id); + if (!player) { + return embedFallback({ title: "Unknown Player", description: "Player not found" }); + } + + const { username, uniqueId, skin } = player; + const headPartUrl = skin.parts.head; + + const description = ` + Username: ${username} + UUID: ${uniqueId}`; + + return { + title: `${username}`, + openGraph: { + title: `${username}`, + description: description, + images: [ + { + url: headPartUrl, + }, + ], + }, + twitter: { + card: "summary", + }, + }; +} + async function getData(id: string): Promise { try { const cachedPlayer = await getPlayer(id); @@ -17,12 +50,6 @@ async function getData(id: string): Promise { } } -type Params = { - params: { - id: string; - }; -}; - export default async function Page({ params }: Params) { const player = await getData(params.id); diff --git a/src/app/server/[platform]/[hostname]/page.tsx b/src/app/server/[platform]/[hostname]/page.tsx index 09f856e..b20fce2 100644 --- a/src/app/server/[platform]/[hostname]/page.tsx +++ b/src/app/server/[platform]/[hostname]/page.tsx @@ -1,3 +1,4 @@ +import { embedFallback } from "@/common/embed-fallback"; import { capitalizeFirstLetter } from "@/common/string-utils"; import { LookupServer } from "@/components/lookup-server"; import { NotFound } from "@/components/not-found"; @@ -18,15 +19,15 @@ type Params = { export async function generateMetadata({ params: { platform, hostname } }: Params): Promise { const server = await getData(platform, hostname); if (!server) { - return { - title: "Unknown Server", - }; + return embedFallback({ title: "Unknown Server", description: "Server not found" }); } const { hostname: serverHostname, players } = server; - let favicon = null; + + let favicon = null; // Server favicon if (platform === ServerPlatform.Java) { + // Java specific const javaServer = server as JavaMinecraftServer; favicon = javaServer.favicon && javaServer.favicon.url; } diff --git a/src/common/embed-fallback.ts b/src/common/embed-fallback.ts new file mode 100644 index 0000000..9944447 --- /dev/null +++ b/src/common/embed-fallback.ts @@ -0,0 +1,23 @@ +import { Metadata } from "next"; + +type Fallback = { + title: string; + description: string; +}; + +/** + * Generates metadata for a fallback embed. + * + * @param title the title of the embed + * @param description the description of the embed + * @returns the metadata for the embed + */ +export function embedFallback({ title, description }: Fallback): Metadata { + return { + title: `${title}`, + openGraph: { + title: `${title}`, + description: description, + }, + }; +}