library.ts

2.2 KB · raw

1import type { Context } from "hono";2import { newestSaves, type RepoStats, repoStats, type Save } from "../mirror/queries.ts";3import { repoNames } from "../server/db.ts";4import { signedIn } from "../server/login.ts";5import { type Html, html } from "./html.ts";6import { openRepo, type PageContext, type PublicRepo, plural } from "./layout.ts";7import { repoIntro } from "./repo.ts";89export type LibraryRepo = PublicRepo & { stats: RepoStats; description: string };1011export type RecentSave = { repo: string; save: Save };1213export function libraryRepos(ctx: PageContext, visibility: 0 | 1 = 1): LibraryRepo[] {14	return repoNames(ctx.kepr, visibility).flatMap((name) => {15		const repo = openRepo(ctx, name, visibility);16		if (!repo) return [];17		const description = repoIntro(repo)?.text ?? "";18		return [{ ...repo, stats: repoStats(repo.mirror), description }];19	});20}2122export function privateRepoCards(c: Context, ctx: PageContext): Html | null {23	c.header("Vary", "Cookie", { append: true });24	if (!signedIn(ctx, c)) return null;25	c.header("Cache-Control", "no-store");26	const repos = libraryRepos(ctx, 0);27	return repos.length28		? html`<section class="featured"><h2 class="featured-label mono">private repos</h2>29${repoCards(repos)}</section>`30		: null;31}3233/** The newest saves across the library, checkpoints never among them. */34export function recentSaves(library: LibraryRepo[], limit: number): RecentSave[] {35	return library36		.flatMap((repo) =>37			newestSaves(repo.mirror, limit).map((save) => ({ repo: repo.settings.name, save })),38		)39		.sort((a, b) => b.save.created_at - a.save.created_at)40		.slice(0, limit);41}4243export function repoCards(library: LibraryRepo[]): Html {44	return html`<div class="card-grid">${library.map(repoCard)}</div>`;45}4647// Kepr 2's card: name, save count, the description, and the category.48function repoCard(repo: LibraryRepo): Html {49	const { name, category } = repo.settings;50	return html`<a class="repo-card" href="/${name}">51<div class="repo-card-head"><span class="repo-card-name">${52		name53	}</span><span class="repo-card-saves mono">${plural(repo.stats.saves, "save")}</span></div>54${repo.description && html`<p class="repo-card-desc">${repo.description}</p>`}55${category && html`<p class="repo-card-cat">${category}</p>`}56</a>`;57}