Added online status to sidebar

This commit is contained in:
2025-09-13 23:28:47 +02:00
parent d754fc0b37
commit 781338a81b
7 changed files with 399 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
import { statusSchema } from "@/lib/schema/status"
import { cacheLife } from "next/dist/server/use-cache/cache-life"
import { env } from "../../env/server"
const playerApi = "https://api.hypixel.net/v2/status"
export async function getSession(uuid: string) {
"use cache"
if (process.env.NODE_ENV === "production") {
cacheLife({
stale: 1000 * 60,
revalidate: 1000 * 60 * 5,
expire: 1000 * 60 * 5
})
}
const res = await fetch(`${playerApi}?uuid=${uuid}`, {
headers: {
"API-Key": env.HYPIXEL_API_KEY
}
})
if (!res.ok) return null
const { success, data } = statusSchema.safeParse(await res.json())
if (!success) return null
return data.session
}

View File

@@ -0,0 +1,20 @@
import { GAMES, MODES } from "@/data/hypixel/hypixel"
export function getGame(val: string) {
const game = GAMES.find(g => g.id === val)
return game || null
}
export function getGameMode(gameType?: string, mode?: string) {
if (!gameType || !mode) return null
const game = getGame(gameType)
if (!game) return null
const modes = MODES as Record<string, undefined | Record<string, never> | Record<string, string | undefined>>
const gameModes = modes[gameType]
if (!gameModes) return null
const modeName = gameModes[mode]
return modeName || null
}

17
src/lib/schema/status.ts Normal file
View File

@@ -0,0 +1,17 @@
import z from "zod"
export const statusSchema = z.object({
session: z.discriminatedUnion("online", [
z.object({
online: z.literal(false)
}),
z.object({
online: z.literal(true),
gameType: z.string().optional(),
mode: z.string().optional(),
map: z.string().optional()
})
])
})
export type Session = z.infer<typeof statusSchema>