feat: add multi-strategy version fetchers, disable auto-fetch by default
- Add 4 version-fetch strategies (UpToDateCheck API, Store Page Scrape, Community Hub Scrape, Store API Heuristic) — no Steam API key needed - Create orchestrator that tries all strategies, prefers named version over build ID - Add test page at /test-version-fetchers for comparing strategies - Add standalone /api/version-test endpoint (direct Steam App ID, no DB) - Update /api/games/:id/steamdb-version to use new strategies - Disable auto-fetch on submit page by default (NEXT_PUBLIC_VERSION_AUTO_FETCH=true to enable) - SteamDB scraping blocked by Cloudflare; strategies preserved for later enablement
This commit is contained in:
@@ -131,12 +131,14 @@ export function GameEntryWizard({ gameId, gameVersions, defaultVersionId, editEn
|
||||
// Step 4: Notes
|
||||
const [userNotes, setUserNotes] = useState(editEntry?.userNotes ?? "")
|
||||
|
||||
// SteamDB version suggestion
|
||||
// Auto-detected version suggestion (multi-strategy)
|
||||
const [steamdbVersion, setSteamdbVersion] = useState<{
|
||||
versionString: string | null
|
||||
buildId: string | null
|
||||
source?: string
|
||||
} | null>(null)
|
||||
const [steamdbLoading, setSteamdbLoading] = useState(false)
|
||||
const [steamdbError, setSteamdbError] = useState<string | null>(null)
|
||||
|
||||
// Fetch hardware name when slug changes
|
||||
const handleHardwareChange = useCallback(async (slug: string) => {
|
||||
@@ -209,23 +211,107 @@ export function GameEntryWizard({ gameId, gameVersions, defaultVersionId, editEn
|
||||
|
||||
const fetchSteamDBVersion = useCallback(async () => {
|
||||
setSteamdbLoading(true)
|
||||
setSteamdbError(null)
|
||||
try {
|
||||
// Step 1: Try server-side strategies first
|
||||
const res = await fetch(`/api/games/${gameId}/steamdb-version`)
|
||||
if (!res.ok) return
|
||||
const data = await res.json()
|
||||
|
||||
// If server found something, use it
|
||||
if (data.versionString || data.buildId) {
|
||||
setSteamdbVersion({
|
||||
versionString: data.versionString,
|
||||
buildId: data.buildId,
|
||||
source: data.source,
|
||||
})
|
||||
}
|
||||
|
||||
// Step 2: If server suggests client-side fetch, run client strategies in browser
|
||||
if (data.needsClientFetch && data.clientStrategies?.length > 0) {
|
||||
await runClientStrategies(data.clientStrategies)
|
||||
}
|
||||
} catch {
|
||||
// Silently fail — SteamDB is best-effort
|
||||
// Silently fail — version detection is best-effort
|
||||
} finally {
|
||||
setSteamdbLoading(false)
|
||||
}
|
||||
}, [gameId])
|
||||
|
||||
// Run client-side strategies (uses browser IP to avoid server rate limits)
|
||||
const runClientStrategies = useCallback(async (strategyNames: string[]) => {
|
||||
// Dynamic import of client-side fetchers
|
||||
const { fetchStorePage } = await import("@/lib/version-fetchers/store-page")
|
||||
const { fetchCommunityHub } = await import("@/lib/version-fetchers/community-hub")
|
||||
const { fetchStoreApi } = await import("@/lib/version-fetchers/store-api")
|
||||
|
||||
const strategyMap: Record<string, (appId: number) => Promise<{ versionString: string | null; buildId: string | null; source: string; success: boolean }>> = {
|
||||
"Store Page Scrape": fetchStorePage,
|
||||
"Community Hub Scrape": fetchCommunityHub,
|
||||
"Store API Heuristic": fetchStoreApi,
|
||||
}
|
||||
|
||||
// We need the steamAppId — get it from a lightweight endpoint or from props
|
||||
const gameRes = await fetch(`/api/games/${gameId}`)
|
||||
if (!gameRes.ok) return
|
||||
const gameData = await gameRes.json()
|
||||
const steamAppId = gameData.steamAppId
|
||||
if (!steamAppId) return
|
||||
|
||||
// Run all requested client strategies in parallel
|
||||
const clientResults = await Promise.all(
|
||||
strategyNames.map(async (name) => {
|
||||
const fn = strategyMap[name]
|
||||
if (!fn) return null
|
||||
try {
|
||||
const result = await fn(steamAppId)
|
||||
return result
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
const validResults = clientResults.filter(Boolean) as Array<{
|
||||
versionString: string | null
|
||||
buildId: string | null
|
||||
source: string
|
||||
success: boolean
|
||||
}>
|
||||
|
||||
// Merge with server result — send to server for final merge
|
||||
if (validResults.length > 0) {
|
||||
try {
|
||||
const mergeRes = await fetch(`/api/games/${gameId}/fetch-version-client`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ clientResults: validResults }),
|
||||
})
|
||||
if (mergeRes.ok) {
|
||||
const merged = await mergeRes.json()
|
||||
if (merged.versionString || merged.buildId) {
|
||||
setSteamdbVersion({
|
||||
versionString: merged.versionString,
|
||||
buildId: merged.buildId,
|
||||
source: merged.source,
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// If merge fails, use best client result directly
|
||||
const bestClient = validResults.find((r) => r.versionString) ??
|
||||
validResults.find((r) => r.buildId)
|
||||
if (bestClient && (bestClient.versionString || bestClient.buildId)) {
|
||||
setSteamdbVersion({
|
||||
versionString: bestClient.versionString,
|
||||
buildId: bestClient.buildId,
|
||||
source: bestClient.source,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [gameId])
|
||||
|
||||
// Initialize existing screenshots when editing
|
||||
useEffect(() => {
|
||||
if (editEntry && (editEntry as any).screenshots && Array.isArray((editEntry as any).screenshots)) {
|
||||
@@ -242,9 +328,11 @@ export function GameEntryWizard({ gameId, gameVersions, defaultVersionId, editEn
|
||||
}
|
||||
}, [editEntry])
|
||||
|
||||
// Fetch SteamDB version on mount
|
||||
// Fetch auto-detected version on mount (disabled by default — set NEXT_PUBLIC_VERSION_AUTO_FETCH=true to enable)
|
||||
useEffect(() => {
|
||||
fetchSteamDBVersion()
|
||||
if (process.env.NEXT_PUBLIC_VERSION_AUTO_FETCH === "true") {
|
||||
fetchSteamDBVersion()
|
||||
}
|
||||
}, [fetchSteamDBVersion])
|
||||
|
||||
const canProceed = () => {
|
||||
@@ -458,6 +546,7 @@ export function GameEntryWizard({ gameId, gameVersions, defaultVersionId, editEn
|
||||
platformSupport={platformSupport}
|
||||
steamdbVersion={steamdbVersion}
|
||||
steamdbLoading={steamdbLoading}
|
||||
steamdbError={steamdbError}
|
||||
onRefreshSteamDB={fetchSteamDBVersion}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -14,6 +14,7 @@ export interface GameVersionInfo {
|
||||
export interface SteamDBVersion {
|
||||
versionString: string | null
|
||||
buildId: string | null
|
||||
source?: string
|
||||
}
|
||||
|
||||
interface SetupStepProps {
|
||||
@@ -37,6 +38,7 @@ interface SetupStepProps {
|
||||
}[]
|
||||
steamdbVersion: SteamDBVersion | null
|
||||
steamdbLoading: boolean
|
||||
steamdbError: string | null
|
||||
onRefreshSteamDB: () => void
|
||||
}
|
||||
|
||||
@@ -58,10 +60,12 @@ export function SetupStep({
|
||||
platformSupport,
|
||||
steamdbVersion,
|
||||
steamdbLoading,
|
||||
steamdbError,
|
||||
onRefreshSteamDB,
|
||||
}: SetupStepProps) {
|
||||
const isNewVersion = selectedVersionId === "__new__"
|
||||
const isSteamDBVersion = selectedVersionId === "__steamdb__"
|
||||
const autoFetchEnabled = process.env.NEXT_PUBLIC_VERSION_AUTO_FETCH === "true"
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
@@ -92,13 +96,13 @@ export function SetupStep({
|
||||
onChange={(e) => onVersionChange(e.target.value)}
|
||||
className="w-full appearance-none px-4 py-3 rounded-lg border border-border bg-text/5 text-text text-sm outline-none focus:border-primary focus:ring-2 focus:ring-primary/50 transition-colors cursor-pointer"
|
||||
>
|
||||
{/* SteamDB suggestion — appears at top when available */}
|
||||
{steamdbVersion && (steamdbVersion.versionString || steamdbVersion.buildId) && (
|
||||
{/* Auto-detected version suggestion — only when feature is enabled */}
|
||||
{autoFetchEnabled && steamdbVersion && (steamdbVersion.versionString || steamdbVersion.buildId) && (
|
||||
<option value="__steamdb__" className="bg-primary/10 text-primary">
|
||||
⬇ Latest from SteamDB: {steamdbVersion.versionString || `Build ${steamdbVersion.buildId}`} — recommended
|
||||
⬇ Latest ({steamdbVersion.source ?? "auto-detected"}): {steamdbVersion.versionString || `Build ${steamdbVersion.buildId}`} — recommended
|
||||
</option>
|
||||
)}
|
||||
{steamdbLoading && (
|
||||
{autoFetchEnabled && steamdbLoading && (
|
||||
<option disabled className="text-text/40">
|
||||
Fetching latest version from SteamDB...
|
||||
</option>
|
||||
@@ -121,7 +125,8 @@ export function SetupStep({
|
||||
</option>
|
||||
</select>
|
||||
|
||||
{/* Refresh button for SteamDB */}
|
||||
{/* Refresh button for auto-detection (only shown when feature enabled) */}
|
||||
{autoFetchEnabled && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onRefreshSteamDB}
|
||||
@@ -129,8 +134,12 @@ export function SetupStep({
|
||||
className="flex items-center gap-1 text-xs text-text/40 hover:text-primary transition-colors cursor-pointer mt-1 disabled:opacity-30"
|
||||
>
|
||||
<RefreshCwIcon className={`h-3 w-3 ${steamdbLoading ? "animate-spin" : ""}`} />
|
||||
Refresh from SteamDB
|
||||
Refresh auto-detected version
|
||||
</button>
|
||||
)}
|
||||
{steamdbError && (
|
||||
<p className="text-xs text-red-400 mt-1">{steamdbError}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{isNewVersion && (
|
||||
@@ -152,11 +161,13 @@ export function SetupStep({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isSteamDBVersion && steamdbVersion && (
|
||||
{autoFetchEnabled && isSteamDBVersion && steamdbVersion && (
|
||||
<div className="space-y-3 p-3 rounded-lg border border-primary/30 bg-primary/5">
|
||||
<div className="flex items-center gap-2">
|
||||
<DatabaseIcon className="h-4 w-4 text-primary" />
|
||||
<p className="text-xs font-medium text-primary">SteamDB Suggestion</p>
|
||||
<p className="text-xs font-medium text-primary">
|
||||
Auto-Detected Version ({steamdbVersion.source ?? "unknown source"})
|
||||
</p>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user