embed color based on if the lookup was successful
All checks were successful
Deploy App / docker (ubuntu-latest) (push) Successful in 1m2s

This commit is contained in:
Lee 2024-04-18 06:33:30 +01:00
parent 19e386b539
commit 6238400ffe
5 changed files with 50 additions and 8 deletions

@ -7,9 +7,10 @@ import { LookupPlayer } from "@/app/components/player/lookup-player";
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu";
import { Separator } from "@/app/components/ui/separator"; import { Separator } from "@/app/components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/app/components/ui/tooltip"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/app/components/ui/tooltip";
import { Colors } from "@/common/colors";
import { generateEmbed } from "@/common/embed"; import { generateEmbed } from "@/common/embed";
import { CachedPlayer, McUtilsAPIError, SkinPart, getPlayer } from "mcutils-library"; import { CachedPlayer, McUtilsAPIError, SkinPart, getPlayer } from "mcutils-library";
import { Metadata } from "next"; import { Metadata, Viewport } from "next";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
import { ReactElement } from "react"; import { ReactElement } from "react";
@ -21,6 +22,25 @@ 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,
};
}
}
export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> { export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> {
try { try {
// No id provided // No id provided

@ -3,6 +3,7 @@ import { CopyButton } from "@/app/components/copy-button";
import { ErrorCard } from "@/app/components/error-card"; import { ErrorCard } from "@/app/components/error-card";
import { LookupServer } from "@/app/components/server/lookup-server"; import { LookupServer } from "@/app/components/server/lookup-server";
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "@/app/components/ui/context-menu";
import { Colors } from "@/common/colors";
import { generateEmbed } from "@/common/embed"; import { generateEmbed } from "@/common/embed";
import { formatNumber } from "@/common/number-utils"; import { formatNumber } from "@/common/number-utils";
import { capitalizeFirstLetter } from "@/common/string-utils"; import { capitalizeFirstLetter } from "@/common/string-utils";
@ -13,7 +14,7 @@ import {
ServerPlatform, ServerPlatform,
getServer, getServer,
} from "mcutils-library"; } from "mcutils-library";
import { Metadata } from "next"; import { Metadata, Viewport } from "next";
import Image from "next/image"; import Image from "next/image";
import { ReactElement } from "react"; import { ReactElement } from "react";
import config from "../../../../../../config.json"; import config from "../../../../../../config.json";
@ -53,6 +54,25 @@ function checkPlatform(platform: ServerPlatform): boolean {
return platform === ServerPlatform.Java || platform === ServerPlatform.Bedrock; return platform === ServerPlatform.Java || platform === ServerPlatform.Bedrock;
} }
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,
};
}
}
export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> { export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> {
try { try {
if (!checkPlatform(platform)) { if (!checkPlatform(platform)) {

@ -24,7 +24,8 @@ export function LookupPlayer(): ReactElement {
} }
try { try {
await getPlayer(query); const player = await getPlayer(query);
router.push(`/player/${player.username}`);
} catch (err) { } catch (err) {
toast({ toast({
title: "Error", title: "Error",
@ -34,8 +35,6 @@ export function LookupPlayer(): ReactElement {
}); });
return; return;
} }
router.push(`/player/${query}`);
}; };
return ( return (

@ -30,7 +30,8 @@ export function LookupServer({ currentPlatform }: LookupServerProps): ReactEleme
} }
try { try {
await getServer(platform, query); const server = await getServer(platform, query);
router.push(`/server/${platform}/${server.hostname}`);
} catch (err) { } catch (err) {
toast({ toast({
title: "Error", title: "Error",
@ -40,8 +41,6 @@ export function LookupServer({ currentPlatform }: LookupServerProps): ReactEleme
}); });
return; return;
} }
router.push(`/server/${platform}/${query}`);
}; };
return ( return (

4
src/common/colors.ts Normal file

@ -0,0 +1,4 @@
export const Colors = {
green: "#0FFF50",
red: "#8B0000",
};