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

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

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

@ -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

@ -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;
}