cleanup metadata generation
All checks were successful
Deploy App / docker (ubuntu-latest) (push) Successful in 54s

This commit is contained in:
Lee 2024-04-16 18:14:15 +01:00
parent b33ed9378b
commit 5b76385caf
4 changed files with 50 additions and 56 deletions

View File

@ -1,4 +1,4 @@
import { embedFallback } from "@/common/embed-fallback";
import { generateEmbed } from "@/common/embed";
import { NotFound } from "@/components/not-found";
import { Card } from "@/components/ui/card";
import { getPlayer } from "mcutils-library";
@ -14,7 +14,7 @@ type Params = {
export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> {
const player = await getData(id);
if (!player) {
return embedFallback({ title: "Unknown Player", description: "Player not found" });
return generateEmbed({ title: "Unknown Player", description: "Player not found" });
}
const { username, uniqueId, skin } = player;
@ -24,21 +24,11 @@ export async function generateMetadata({ params: { id } }: Params): Promise<Meta
Username: ${username}
UUID: ${uniqueId}`;
return {
return generateEmbed({
title: `${username}`,
openGraph: {
title: `${username}`,
description: description,
images: [
{
url: headPartUrl,
},
],
},
twitter: {
card: "summary",
},
};
description: description,
image: headPartUrl,
});
}
async function getData(id: string): Promise<Player | null> {

View File

@ -1,4 +1,4 @@
import { embedFallback } from "@/common/embed-fallback";
import { generateEmbed } from "@/common/embed";
import { capitalizeFirstLetter } from "@/common/string-utils";
import { LookupServer } from "@/components/lookup-server";
import { NotFound } from "@/components/not-found";
@ -19,7 +19,7 @@ type Params = {
export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> {
const server = await getData(platform, hostname);
if (!server) {
return embedFallback({ title: "Unknown Server", description: "Server not found" });
return generateEmbed({ title: "Unknown Server", description: "Server not found" });
}
const { hostname: serverHostname, players } = server;
@ -37,21 +37,11 @@ export async function generateMetadata({ params: { platform, hostname } }: Param
Hostname: ${serverHostname}
${players.online}/${players.max} players online`;
return {
title: `${hostname}`,
openGraph: {
title: `${hostname}`,
description: description,
images: [
{
url: favicon || "",
},
],
},
twitter: {
card: "summary",
},
};
return generateEmbed({
title: `${serverHostname}`,
description: description,
image: favicon,
});
}
async function getData(platform: ServerPlatform, id: string): Promise<MinecraftServer | null> {

View File

@ -1,23 +0,0 @@
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,
},
};
}

37
src/common/embed.ts Normal file
View File

@ -0,0 +1,37 @@
import { Metadata } from "next";
type Embed = {
title: string;
description: string;
image?: string | null;
};
/**
* Generates metadata for a embed.
*
* @param title the title of the embed
* @param description the description of the embed
* @returns the metadata for the embed
*/
export function generateEmbed({ title, description, image }: Embed): Metadata {
const metadata: Metadata = {
title: `${title}`,
openGraph: {
title: `${title}`,
description: description,
},
twitter: {
card: "summary",
},
};
if (image) {
metadata.openGraph!.images = [
{
url: image || "",
},
];
}
return metadata;
}