front.ts

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