From c605310e46d48b9d85cb7035f27d50e858d9137d Mon Sep 17 00:00:00 2001 From: radiumcoders Date: Thu, 29 Oct 2026 12:10:04 +0530 Subject: [PATCH] Add a /stats page with components, stars, forks, contributors, and weekly commits, refreshed hourly. --- app/(marketing)/stats/page.tsx | 186 +++++++++++++++++++++++++++++++++ app/sitemap.ts | 1 + lib/site.ts | 1 + lib/stats.ts | 139 ++++++++++++++++++++++++ 4 files changed, 327 insertions(+) create mode 100644 app/(marketing)/stats/page.tsx create mode 100644 lib/stats.ts diff --git a/app/(marketing)/stats/page.tsx b/app/(marketing)/stats/page.tsx new file mode 100644 index 0000000..27625ee --- /dev/null +++ b/app/(marketing)/stats/page.tsx @@ -0,0 +1,186 @@ +import { buildPageMetadata } from "@/lib/seo" +import { cn } from "@/lib/utils" +import { getSiteStats, type CategoryCount, type WeekCommits } from "@/lib/stats" + +export const revalidate = 3600 + +export const metadata = buildPageMetadata({ + title: "Stats", + description: + "Open numbers for 23rd: components shipped, GitHub stars, forks, contributors, and commits over the past year.", + path: "/stats", + keywords: ["stats", "open source", "open metrics"], +}) + +const numberFormat = new Intl.NumberFormat("en") +const weekFormat = new Intl.DateTimeFormat("en", { + month: "short", + day: "numeric", + timeZone: "UTC", +}) +const monthFormat = new Intl.DateTimeFormat("en", { + month: "short", + timeZone: "UTC", +}) + +function StatTile({ + label, + value, + className, +}: { + label: string + value: number | null + className?: string +}) { + return ( +
+
{label}
+
+ {value === null ? "–" : numberFormat.format(value)} +
+
+ ) +} + +function CommitChart({ weeks }: { weeks: WeekCommits[] }) { + const max = Math.max(1, ...weeks.map((week) => week.total)) + + return ( +
+

+ Commits per week +

+
+ +
+ ) +} + +function CategoryBars({ categories }: { categories: CategoryCount[] }) { + const max = Math.max(1, ...categories.map((category) => category.count)) + + return ( +
+

+ Components by category +

+ +
+ ) +} + +export default async function StatsPage() { + const stats = await getSiteStats() + + return ( +
+
+
+

Stats

+

+ 23rd in numbers, refreshed every hour. +

+
+ +
+ + + + + +
+ + {stats.weeks ? : null} + + +
+
+ ) +} diff --git a/app/sitemap.ts b/app/sitemap.ts index 016c848..497072f 100644 --- a/app/sitemap.ts +++ b/app/sitemap.ts @@ -20,6 +20,7 @@ export default function sitemap(): MetadataRoute.Sitemap { add("/", 1) add("/sponsors", 0.6, "monthly") + add("/stats", 0.4, "daily") add("/terms", 0.2, "yearly") add("/privacy", 0.2, "yearly") diff --git a/lib/site.ts b/lib/site.ts index 4a2d1eb..fe5cab8 100644 --- a/lib/site.ts +++ b/lib/site.ts @@ -5,6 +5,7 @@ export const DOCS_HOME = "/docs/getting-started" /** Same secondary links shadscan puts in the site footer. */ export const FOOTER_LINKS = [ + { href: "/stats", label: "Stats" }, { href: "/privacy", label: "Privacy" }, { href: "/terms", label: "Terms" }, ] as const diff --git a/lib/stats.ts b/lib/stats.ts new file mode 100644 index 0000000..5fb21e9 --- /dev/null +++ b/lib/stats.ts @@ -0,0 +1,139 @@ +import componentsMeta from "@/content/docs/components/meta.json" + +const REPO = "radiumcoders/23rd.dev" +const REVALIDATE_SECONDS = 3600 + +export type CategoryCount = { name: string; count: number } +export type WeekCommits = { week: number; total: number } + +export type SiteStats = { + components: number + categories: CategoryCount[] + stars: number | null + forks: number | null + contributors: number | null + commitsPastYear: number | null + weeks: WeekCommits[] | null +} + +/** Groups the docs sidebar's "---Group---" separators into component counts. */ +export function getCategoryCounts(): CategoryCount[] { + const categories: CategoryCount[] = [] + for (const page of componentsMeta.pages) { + const group = /^---(.+)---$/.exec(page) + if (group) { + categories.push({ name: group[1], count: 0 }) + } else if (categories.length > 0) { + categories[categories.length - 1].count += 1 + } + } + return categories.filter((category) => category.count > 0) +} + +async function github(path: string): Promise { + const token = process.env.GITHUB_TOKEN + try { + const res = await fetch(`https://api.github.com/repos/${REPO}${path}`, { + headers: { + Accept: "application/vnd.github+json", + "User-Agent": "23rd.dev", + ...(token ? { Authorization: `Bearer ${token}` } : {}), + }, + next: { revalidate: REVALIDATE_SECONDS }, + signal: AbortSignal.timeout(5000), + }) + if (!res.ok) { + console.warn(`GitHub ${path}: ${res.status} ${res.statusText}`) + return null + } + return res + } catch (error) { + console.warn(`GitHub ${path}: request failed`, error) + return null + } +} + +async function getRepo() { + const res = await github("") + if (!res) return null + return (await res.json()) as { stargazers_count: number; forks_count: number } +} + +/** One contributor per page, so the last page number is the total. */ +async function getContributorCount(): Promise { + const res = await github("/contributors?per_page=1&anon=1") + if (!res) return null + const last = /[?&]page=(\d+)>; rel="last"/.exec(res.headers.get("link") ?? "") + if (last) return Number(last[1]) + const list = (await res.json()) as unknown[] + return list.length +} + +const WEEK_MS = 7 * 24 * 60 * 60 * 1000 +const WEEKS = 52 + +/** + * Buckets the past year's commits into Sunday-start weeks. Counted from the + * commit list because /stats/commit_activity answers 202 until GitHub has + * cached it, which leaves a cold build with no chart. + */ +async function getWeeklyCommits(): Promise { + const now = new Date() + const thisSunday = Date.UTC( + now.getUTCFullYear(), + now.getUTCMonth(), + now.getUTCDate() - now.getUTCDay() + ) + const firstWeek = thisSunday - (WEEKS - 1) * WEEK_MS + const since = new Date(firstWeek).toISOString() + const totals = new Array(WEEKS).fill(0) + + for (let page = 1; page <= 20; page++) { + const res = await github( + `/commits?since=${since}&per_page=100&page=${page}` + ) + if (!res) return null + const commits = (await res.json()) as { + commit: { author: { date: string } | null } + }[] + for (const { commit } of commits) { + if (!commit.author) continue + const index = Math.floor( + (Date.parse(commit.author.date) - firstWeek) / WEEK_MS + ) + if (index >= 0 && index < WEEKS) totals[index] += 1 + } + if (commits.length < 100) break + } + + // Start the chart at the first active week, so a young repo isn't mostly + // empty bars. + const firstActive = totals.findIndex((total) => total > 0) + const start = firstActive === -1 ? 0 : Math.min(firstActive, WEEKS - 8) + + return totals.slice(start).map((total, index) => ({ + week: (firstWeek + (start + index) * WEEK_MS) / 1000, + total, + })) +} + +export async function getSiteStats(): Promise { + const categories = getCategoryCounts() + const [repo, contributors, weeks] = await Promise.all([ + getRepo(), + getContributorCount(), + getWeeklyCommits(), + ]) + + return { + components: categories.reduce((sum, category) => sum + category.count, 0), + categories, + stars: repo?.stargazers_count ?? null, + forks: repo?.forks_count ?? null, + contributors, + commitsPastYear: weeks + ? weeks.reduce((sum, week) => sum + week.total, 0) + : null, + weeks, + } +}