"use client"; import { useCallback, useEffect, useState } from "react"; import { Dialog, PixelButton, XpBar } from "../pixel"; import { Reveal } from "./reveal"; import { SectionHeader } from "./section-header"; import section from "./section.module.css"; import styles from "./xp-section.module.css"; const XP_PER_LEVEL = 1000; type Stats = { phase: "loading" } | { phase: "ready"; downloads: number } | { phase: "error"; message: string }; async function fetchDownloads(): Promise { const response = await fetch("/api/download-stats", { cache: "no-store" }); if (!response.ok) throw new Error(`The scoreboard answered HTTP ${response.status}.`); const body = (await response.json()) as { downloads?: unknown }; if (typeof body.downloads !== "number") throw new Error("The scoreboard sent a score we can't read."); return body.downloads; } export function XpSection() { const [stats, setStats] = useState({ phase: "loading" }); const load = useCallback(() => { setStats({ phase: "loading" }); fetchDownloads().then( (downloads) => setStats({ phase: "ready", downloads }), (error: Error) => setStats({ phase: "error", message: error.message }), ); }, []); useEffect(load, [load]); return (
{stats.phase === "loading" ?

Loading save data…

: null} {stats.phase === "ready" ? : null} {stats.phase === "error" ? (

GAME OVER for the counter, not for you. {stats.message}

Downloads still work. Continue?

Retry Continue to downloads
) : null}
); } function Level({ downloads }: { downloads: number }) { const level = Math.floor(downloads / XP_PER_LEVEL); return (

{downloads.toLocaleString("en-US")} downloads counted

); }