refactor: createGameStub now uses ensureSteamGame pipeline
This commit is contained in:
+10
-67
@@ -13,7 +13,7 @@ import {
|
|||||||
user,
|
user,
|
||||||
} from "@/lib/db/schema"
|
} from "@/lib/db/schema"
|
||||||
import { and, desc, eq, sql } from "drizzle-orm"
|
import { and, desc, eq, sql } from "drizzle-orm"
|
||||||
import { isSyncStale, syncSteamGame } from "@/lib/steam/sync"
|
import { isSyncStale, syncSteamGame, ensureSteamGame } from "@/lib/steam/sync"
|
||||||
import { GamePageClient } from "./game-page-client"
|
import { GamePageClient } from "./game-page-client"
|
||||||
|
|
||||||
// This page needs live data — skip static generation at build time
|
// This page needs live data — skip static generation at build time
|
||||||
@@ -74,73 +74,16 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createGameStub(steamAppId: number) {
|
async function createGameStub(steamAppId: number) {
|
||||||
const url = new URL("https://store.steampowered.com/api/appdetails/")
|
const result = await ensureSteamGame(steamAppId)
|
||||||
url.searchParams.set("appids", String(steamAppId))
|
if (!result.game) {
|
||||||
url.searchParams.set("cc", "US")
|
notFound()
|
||||||
url.searchParams.set("l", "en")
|
|
||||||
|
|
||||||
const res = await fetch(url.toString(), {
|
|
||||||
headers: { Accept: "application/json" },
|
|
||||||
})
|
|
||||||
|
|
||||||
let title = `Steam App ${steamAppId}`
|
|
||||||
let developer: string | null = null
|
|
||||||
let publisher: string | null = null
|
|
||||||
let genres: string[] | null = null
|
|
||||||
let headerImage: string | null = null
|
|
||||||
let capsuleImage: string | null = null
|
|
||||||
let description: string | null = null
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const data = (await res.json()) as Record<
|
|
||||||
string,
|
|
||||||
{
|
|
||||||
success: boolean
|
|
||||||
data: {
|
|
||||||
type?: string
|
|
||||||
name: string
|
|
||||||
developers?: string[]
|
|
||||||
publishers?: string[]
|
|
||||||
genres?: { description: string }[]
|
|
||||||
header_image?: string
|
|
||||||
short_description?: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
>
|
|
||||||
const entry = data[String(steamAppId)]
|
|
||||||
if (entry?.success && entry.data) {
|
|
||||||
if (entry.data.type && entry.data.type !== "game") {
|
|
||||||
notFound()
|
|
||||||
}
|
|
||||||
title = entry.data.name
|
|
||||||
developer = entry.data.developers?.[0] ?? null
|
|
||||||
publisher = entry.data.publishers?.[0] ?? null
|
|
||||||
genres = entry.data.genres?.map((g) => g.description) ?? []
|
|
||||||
headerImage = entry.data.header_image ?? null
|
|
||||||
capsuleImage = `https://cdn.akamai.steamstatic.com/steam/apps/${steamAppId}/library_600x900.jpg`
|
|
||||||
description = entry.data.short_description ?? null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// If the sync determined this is not a game (DLC, soundtrack, etc.),
|
||||||
const [game] = await db
|
// treat it as not found rather than showing a broken page
|
||||||
.insert(games)
|
if (result.game.syncStatus === "error" && result.error?.includes("not a game")) {
|
||||||
.values({
|
notFound()
|
||||||
steamAppId,
|
}
|
||||||
source: "steam",
|
return result.game
|
||||||
title,
|
|
||||||
developer,
|
|
||||||
publisher,
|
|
||||||
genres,
|
|
||||||
headerImage,
|
|
||||||
capsuleImage,
|
|
||||||
description,
|
|
||||||
storeUrl: `https://store.steampowered.com/app/${steamAppId}`,
|
|
||||||
lastSync: new Date(),
|
|
||||||
syncStatus: "synced",
|
|
||||||
})
|
|
||||||
.returning()
|
|
||||||
|
|
||||||
return game
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function GamePage({
|
export default async function GamePage({
|
||||||
|
|||||||
Reference in New Issue
Block a user