From 38f196bd9af0f53180bf62fb64b233f58815fc4c Mon Sep 17 00:00:00 2001 From: Taken Date: Fri, 26 Sep 2025 14:09:14 +0200 Subject: [PATCH] Updated page --- src/app/(stats)/guild/[value]/_client.tsx | 19 ++++++++- src/app/(stats)/guild/[value]/page.tsx | 47 ++++++++++++++++++----- src/lib/utils.ts | 14 +++++++ 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/app/(stats)/guild/[value]/_client.tsx b/src/app/(stats)/guild/[value]/_client.tsx index 59a2f62..f34247e 100644 --- a/src/app/(stats)/guild/[value]/_client.tsx +++ b/src/app/(stats)/guild/[value]/_client.tsx @@ -1,9 +1,24 @@ "use client" -import { usePathname } from "next/navigation" +import { usePathname, useSearchParams } from "next/navigation" +import z from "zod" export function GuildPageLoadText() { const path = usePathname() + const params = useSearchParams() - return

{`Loading ${path.split("/").at(-1)}'s guild...`}

+ const { data: type } = z.literal("id").or(z.literal("name")).or(z.literal("player")).default("player").safeParse(params.get("type")) + + switch (type) { + case "player": + return

{`Loading stats ${path.split("/").at(-1)}'s guild...`}

+ case "id": + return

{`Loading stats for the guild id ${path.split("/").at(-1)}...`}

+ case "name": + return

{`Loading stats for the guild name ${path.split("/").at(-1)}...`}

+ case undefined: + return

{`Loading stats ${path.split("/").at(-1)}'s guild...`}

+ default: + throw new Error(`Unknown type: ${type satisfies never}`) + } } diff --git a/src/app/(stats)/guild/[value]/page.tsx b/src/app/(stats)/guild/[value]/page.tsx index 12105b3..e3abe0d 100644 --- a/src/app/(stats)/guild/[value]/page.tsx +++ b/src/app/(stats)/guild/[value]/page.tsx @@ -3,12 +3,13 @@ import { env } from "@/lib/env/server" import { getGuild } from "@/lib/hypixel/api/guild" import { getUuid } from "@/lib/hypixel/api/mojang" import { getPlayer } from "@/lib/hypixel/api/player" -import { cn } from "@/lib/utils" +import { cn, parseSearchParams } from "@/lib/utils" import { Loader2Icon, ShieldAlert } from "lucide-react" import { Suspense } from "react" +import z from "zod" import { GuildPageLoadText } from "./_client" -export default function GuildPage({ params }: PageProps<"/guild/[value]">) { +export default function GuildPage({ params, searchParams }: PageProps<"/guild/[value]">) { const maintenance = env.MAINTENANCE_MODE if (maintenance) { @@ -29,13 +30,16 @@ export default function GuildPage({ params }: PageProps<"/guild/[value]">) { } > - + ) } -async function SuspendedPage({ params }: Pick, "params">) { +async function SuspendedPage({ params, searchParams }: Pick, "params" | "searchParams">) { const { value } = await params + const ptype = parseSearchParams(await searchParams).getValue("type") + + const { data: type } = z.literal("id").or(z.literal("name")).or(z.literal("player")).default("player").safeParse(ptype) const mc = await getUuid(value) if (!mc) { @@ -56,14 +60,37 @@ async function SuspendedPage({ params }: Pick, "para ) } - const guild = await getGuild(mc.id) + const guild = await getGuild(mc.id, type) if (!guild) { - return ( -
-

Player is not in a guild.

-
- ) + switch (type) { + case "player": + return ( +
+

Player is not in a guild.

+
+ ) + case "id": + return ( +
+

The guild with that id doesn't exist.

+
+ ) + case "name": + return ( +
+

The guild with that name doesn't exist.

+
+ ) + case undefined: + return ( +
+

Player is not in a guild.

+
+ ) + default: + throw new Error(`Unknown type: ${type satisfies never}`) + } } return ( diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 2a4228d..b488f85 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -8,3 +8,17 @@ export function cn(...inputs: ClassValue[]) { export function capitalizeFirstLetter(str: string) { return str[0].toUpperCase() + str.slice(1, str.length) } + +export function parseSearchParams(sp: Record) { + function getValue(key: string): string | null { + const value = sp[key] + if (value === undefined) return null + + if (Array.isArray(value)) { + return value[0] || null + } + + return value + } + return { getValue } +}