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;171819export function registerFrontPages(app: Hono, ctx: PageContext): void {20 app.get("/", (c) => {21 const library = libraryRepos(ctx);22 const tagline = setting(ctx.kepr, "tagline") ?? "";23 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}6970717273let 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}87888990function 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}105106107108function 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 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}