front.ts

4.6 KB · raw

1import type { Hono } from "hono";2import { headId, knownObjects, languageLines, tarballGeneration } from "../mirror/queries.ts";3import { setting } from "../server/db.ts";4import { type Html, html } from "./html.ts";5import { cacheFor, type PageContext, page, plural } from "./layout.ts";6import { type LibraryRepo, publicLibrary, recentSaves, repoCards } from "./library.ts";78const TICKER_ROWS = 7;9const TICKER_ITEMS = 10;10const LATEST_OFFERED = 4;1112/** §21.1's front page and §21.3's browse. */13export function registerFrontPages(app: Hono, ctx: PageContext): void {14	app.get("/", (c) => {15		const library = publicLibrary(ctx);16		const tagline = setting(ctx.kepr, "tagline") ?? "";17		// §21.1: distinct objects across every public repo's real saves.18		const known = knownThings(library);19		const latest = library20			.filter((repo) => repo.stats.updated !== null)21			.sort((a, b) => (b.stats.updated ?? 0) - (a.stats.updated ?? 0))22			.slice(0, LATEST_OFFERED);23		cacheFor(c, false);24		return c.html(25			page(ctx, {26				title: "",27				description: tagline,28				path: "/",29				body: html`${ticker(library)}30<section class="headline">31<div class="headline-text">32<h1>The library knows<br>${plural(known, "thing")}.</h1>33${tagline && html`<p class="headline-tagline">${tagline}</p>`}34</div>35${wordCloud(library)}36</section>37${38	latest.length > 0 &&39	html`<section class="featured"><h2 class="featured-label mono">latest offered</h2>${repoCards(latest)}</section>`40}41<p class="landing-self-host mono"><a href="/docs">run your own library →</a></p>`,42			}),43		);44	});45	app.get("/browse", (c) => {46		const library = publicLibrary(ctx);47		cacheFor(c, false);48		return c.html(49			page(ctx, {50				title: "browse",51				description: `Every public repository, ${plural(library.length, "repo")}.`,52				path: "/browse",53				body: html`<h1 class="file-page-title">browse</h1>54<p class="muted mono">${plural(library.length, "repo")}</p>55${56	library.length > 057		? repoCards(library)58		: html`<p class="muted mono save-empty">nothing public yet</p>`59}`,60			}),61		);62	});63}6465// §21.1's "things": the union of every public repo's objects, which only a sync changes. The key66// names each mirror's head and what rewrites an older save (strips, hidden paths, a cleared67// mirror), so a landed sync counts again and no request scans every object.68let counted: { key: string; count: number } | null = null;6970function knownThings(library: LibraryRepo[]): number {71	const key = library72		.map(73			(repo) =>74				`${repo.settings.name}\n${headId(repo.mirror) ?? ""}\n${tarballGeneration(repo.mirror)}`,75		)76		.join("\n");77	if (counted?.key === key) return counted.count;78	const count = new Set(library.flatMap((repo) => knownObjects(repo.mirror))).size;79	counted = { key, count };80	return count;81}8283// Rows of recent save messages drifting at different depths; the motion is CSS, and stops under84// reduced motion. Each row's messages are written twice so sliding by half its width loops.85function ticker(library: LibraryRepo[]): Html {86	const messages = recentSaves(library, TICKER_ROWS * TICKER_ITEMS).map(87		({ save }) => save.message.split("\n")[0] as string,88	);89	if (messages.length === 0) return html``;90	const rows = Array.from({ length: TICKER_ROWS }, (_, row) => {91		const items = Array.from(92			{ length: TICKER_ITEMS },93			(_, i) => messages[(row * 3 + i) % messages.length],94		);95		const spans = items.map((message) => html`<span>${message}</span>`);96		return html`<div class="ticker-row"><div class="ticker-track">${spans}${spans}</div></div>`;97	});98	return html`<div class="ticker" aria-hidden="true">${rows}</div>`;99}100101// §21.1: file types (the header's languages) mixed with categories, each weighed by how many102// public repos have it, as Kepr 2 weighed its cloud.103function wordCloud(library: LibraryRepo[]): Html {104	const weights = new Map<string, number>();105	for (const repo of library) {106		const labels = languageLines(repo.mirror).map(([language]) => language);107		if (repo.settings.category) labels.push(repo.settings.category);108		for (const label of new Set(labels)) weights.set(label, (weights.get(label) ?? 0) + 1);109	}110	if (weights.size === 0) return html``;111	const max = Math.max(...weights.values());112	// Ordered by a hash of the label, so the big words scatter instead of leading the cloud.113	const tags = [...weights].sort((a, b) => scatter(a[0]) - scatter(b[0]));114	return html`<div class="headline-cloud" aria-label="file types and categories">${tags.map(115		([label, weight]) =>116			html`<span class="tc-tag tc-${Math.ceil((weight / max) * 5)}">${label}</span>`,117	)}</div>`;118}119120function scatter(label: string): number {121	let hash = 0x9e3779b9;122	for (const char of label) hash = (Math.imul(31, hash) + (char.codePointAt(0) ?? 0)) | 0;123	return hash;124}