A lightweight, macOS-style dock for Windows 11, with glass visuals, smooth magnification, and Genie effect.
feat(downloads): add API for fetching installer download count and update Hero section
CristianOlivera14c254c5a19ce
diff --git a/muelle/api/downloads.ts b/muelle/api/downloads.tsnew file mode 100644index 0000000..26b48c9--- /dev/null+++ b/muelle/api/downloads.ts@@ -0,0 +1,100 @@+const REPOSITORY = "crafter-station/Muelle";+const PAGE_SIZE = 100;+const MAX_PAGES = 100;+const CACHE_CONTROL =+ "public, max-age=3600, s-maxage=3600, stale-while-revalidate=300";++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === "object" && value !== null;+}++async function getInstallerDownloadCount(): Promise<number> {+ let total = 0;++ for (let page = 1; page <= MAX_PAGES; page += 1) {+ const url = new URL(`https://api.github.com/repos/${REPOSITORY}/releases`);+ url.searchParams.set("per_page", String(PAGE_SIZE));+ url.searchParams.set("page", String(page));++ const response = await fetch(url, {+ headers: {+ Accept: "application/vnd.github+json",+ "User-Agent": "Muelle-website",+ },+ signal: AbortSignal.timeout(10_000),+ });++ if (!response.ok) {+ throw new Error(`GitHub Releases API returned ${response.status}`);+ }++ const releases: unknown = await response.json();+ if (!Array.isArray(releases)) {+ throw new Error("GitHub Releases API returned an invalid response");+ }++ for (const release of releases) {+ if (!isRecord(release) || !Array.isArray(release.assets)) {+ throw new Error("GitHub Releases API returned an invalid release");+ }++ for (const asset of release.assets) {+ if (!isRecord(asset) || typeof asset.name !== "string") {+ throw new Error("GitHub Releases API returned an invalid asset");+ }++ // Exclude updater manifests and signatures; count downloadable installers only.+ if (!asset.name.toLowerCase().endsWith(".exe")) continue;+ if (+ typeof asset.download_count !== "number" ||+ !Number.isSafeInteger(asset.download_count) ||+ asset.download_count < 0+ ) {+ throw new Error("GitHub Releases API returned an invalid download count");+ }++ total += asset.download_count;+ if (!Number.isSafeInteger(total)) {+ throw new Error("Installer download total exceeds the safe integer range");+ }+ }+ }++ if (releases.length < PAGE_SIZE) return total;+ }++ throw new Error(`GitHub releases exceeded the ${MAX_PAGES}-page limit`);+}++export default {+ async fetch(request: Request): Promise<Response> {+ if (request.method !== "GET") {+ return new Response(null, {+ status: 405,+ headers: { Allow: "GET", "Cache-Control": "no-store" },+ });+ }++ try {+ const downloadCount = await getInstallerDownloadCount();+ return new Response(JSON.stringify({ downloadCount }), {+ headers: {+ "Cache-Control": CACHE_CONTROL,+ "Content-Type": "application/json; charset=utf-8",+ },+ });+ } catch (error) {+ console.error("Could not retrieve Muelle installer download count", error);+ return new Response(+ JSON.stringify({ error: "Download count temporarily unavailable" }),+ {+ status: 502,+ headers: {+ "Cache-Control": "no-store",+ "Content-Type": "application/json; charset=utf-8",+ },+ },+ );+ }+ },+};diff --git a/muelle/src/components/sections/Hero.astro b/muelle/src/components/sections/Hero.astroindex 0e22be9..d614561 100644--- a/muelle/src/components/sections/Hero.astro+++ b/muelle/src/components/sections/Hero.astro@@ -28,11 +28,19 @@ import { site } from "@/data/site";matters. Free, open source and light.</p>- <div data-h class="flex flex-wrap justify-center gap-3">- <Button href={site.download}>- Download for Windows- <Icon name="arrow-right" size={18} />- </Button>+ <div data-h class="flex flex-wrap items-center justify-center gap-3">+ <div class="inline-flex items-center gap-2">+ <Button href={site.download}>+ Download for Windows+ <Icon name="arrow-right" size={18} />+ </Button>+ <span+ data-installer-download-count+ aria-label="Total installer downloads"+ class="font-mono text-sm font-semibold text-mute"+ hidden+ ></span>+ </div><Button href="#dock" variant="secondary">Take a look</Button></div>@@ -47,4 +55,39 @@ import { site } from "@/data/site";import { initHeroMotion } from "@/scripts/hero";initHeroMotion();++ const count = document.querySelector<HTMLElement>(+ "[data-installer-download-count]",+ );++ if (count) {+ try {+ const response = await fetch("/api/downloads", {+ headers: { Accept: "application/json" },+ });+ if (!response.ok) {+ throw new Error(`Download count endpoint returned ${response.status}`);+ }++ const result: unknown = await response.json();+ if (+ !result ||+ typeof result !== "object" ||+ !("downloadCount" in result) ||+ typeof result.downloadCount !== "number" ||+ !Number.isSafeInteger(result.downloadCount) ||+ result.downloadCount < 0+ ) {+ throw new Error("Download count endpoint returned an invalid response");+ }++ count.textContent = new Intl.NumberFormat("en-US", {+ notation: "compact",+ maximumFractionDigits: 1,+ }).format(result.downloadCount);+ count.hidden = false;+ } catch (error) {+ console.error("Could not load Muelle installer download count", error);+ }+ }</script>