Updated page

This commit is contained in:
2025-09-26 14:09:14 +02:00
parent 9978faf69d
commit 38f196bd9a
3 changed files with 68 additions and 12 deletions

View File

@@ -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 <p>{`Loading ${path.split("/").at(-1)}'s guild...`}</p>
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 <p>{`Loading stats ${path.split("/").at(-1)}'s guild...`}</p>
case "id":
return <p>{`Loading stats for the guild id ${path.split("/").at(-1)}...`}</p>
case "name":
return <p>{`Loading stats for the guild name ${path.split("/").at(-1)}...`}</p>
case undefined:
return <p>{`Loading stats ${path.split("/").at(-1)}'s guild...`}</p>
default:
throw new Error(`Unknown type: ${type satisfies never}`)
}
}

View File

@@ -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]">) {
</div>
}
>
<SuspendedPage params={params} />
<SuspendedPage params={params} searchParams={searchParams} />
</Suspense>
)
}
async function SuspendedPage({ params }: Pick<PageProps<"/guild/[value]">, "params">) {
async function SuspendedPage({ params, searchParams }: Pick<PageProps<"/guild/[value]">, "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<PageProps<"/guild/[value]">, "para
)
}
const guild = await getGuild(mc.id)
const guild = await getGuild(mc.id, type)
if (!guild) {
switch (type) {
case "player":
return (
<div className="flex flex-col items-center min-h-screen">
<h1 className="pt-30">Player is not in a guild.</h1>
</div>
)
case "id":
return (
<div className="flex flex-col items-center min-h-screen">
<h1 className="pt-30">The guild with that id doesn&apos;t exist.</h1>
</div>
)
case "name":
return (
<div className="flex flex-col items-center min-h-screen">
<h1 className="pt-30">The guild with that name doesn&apos;t exist.</h1>
</div>
)
case undefined:
return (
<div className="flex flex-col items-center min-h-screen">
<h1 className="pt-30">Player is not in a guild.</h1>
</div>
)
default:
throw new Error(`Unknown type: ${type satisfies never}`)
}
}
return (

View File

@@ -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<string, string | string[] | undefined>) {
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 }
}